#include<printf.h>
#include<systick.h>
#include<time54cap.h>
#include<ceju.h>
#include<printf.h>
int main()
{
u8 a=10;
printf_init(115200);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
TIM4_Cap_Init(0XFFFF,72-1);
printf("¾ààëêÇ£o%d
", a);
while(1)
{
printf("7距离是:o%d
", cedistance7());
printf("6距离是:o%d
", cedistance6());
delay_ms(500);
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////
#include<time54cap.h>
void TIM4_Cap_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_ICInitTypeDef TIM4_ICInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7|GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_ResetBits(GPIOB,GPIO_Pin_7|GPIO_Pin_8);
TIM_TimeBaseStructure.TIM_Period = arr;
TIM_TimeBaseStructure.TIM_Prescaler =psc;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
TIM4_ICInitStructure.TIM_Channel = TIM_Channel_2;
TIM4_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM4_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM4_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM4_ICInitStructure.TIM_ICFilter = 0x00;
TIM_ICInit(TIM4, &TIM4_ICInitStructure);
TIM4_ICInitStructure.TIM_Channel = TIM_Channel_3;
TIM4_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM4_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM4_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM4_ICInitStructure.TIM_ICFilter = 0x00;
TIM_ICInit(TIM4, &TIM4_ICInitStructure);
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_ITConfig(TIM4, TIM_IT_CC2|TIM_IT_CC3,ENABLE);
TIM_Cmd(TIM4,ENABLE );
}
///////////////////////////////////////////////////////////////////////////////////////////////
#include<ceju.h>
extern u8 TIM4CH2_CAPTURE_STA;
extern u16 TIM4CH2_CAPTURE_VAL;
extern u8 TIM4CH3_CAPTURE_STA;
extern u16 TIM4CH3_CAPTURE_VAL;
u16 cedistance6()
{ u16 distance;
while((TIM4CH2_CAPTURE_STA&0X80)==0);
distance=(17*TIM4CH2_CAPTURE_VAL)/1000;
TIM4CH2_CAPTURE_STA=0;
return distance;
}
u16 cedistance7()
{ u16 distance;
while((TIM4CH3_CAPTURE_STA&0X80)==0);
distance=(17*TIM4CH3_CAPTURE_VAL)/1000;
TIM4CH3_CAPTURE_STA=0;
return distance;
}
//////////////////////////////////////////////////////////////////////////////////////////
#include "stm32f10x_it.h"
u8 TIM4CH2_CAPTURE_STA=0;
u16 TIM4CH2_CAPTURE_VAL;
u8 TIM4CH3_CAPTURE_STA=0;
u16 TIM4CH3_CAPTURE_VAL;
void TIM4_IRQHandler(void)
{
if((TIM4CH2_CAPTURE_STA&0X80)==0)
{
if (TIM_GetITStatus(TIM4, TIM_IT_CC2) != RESET)
{
if(TIM4CH2_CAPTURE_STA&0X40)
{
TIM4CH2_CAPTURE_STA|=0X80;
TIM4CH2_CAPTURE_VAL=TIM_GetCapture2(TIM4);
TIM_OC2PolarityConfig(TIM4,TIM_ICPolarity_Rising);
}else
{
TIM4CH2_CAPTURE_STA=0;
TIM4CH2_CAPTURE_VAL=0;
TIM_SetCounter(TIM4,0);
TIM4CH2_CAPTURE_STA|=0X40;
TIM_OC2PolarityConfig(TIM4,TIM_ICPolarity_Falling);
}
}
}
if((TIM4CH3_CAPTURE_STA&0X80)==0)
{
if (TIM_GetITStatus(TIM4, TIM_IT_CC3) != RESET)
{
if(TIM4CH3_CAPTURE_STA&0X40)
{
TIM4CH3_CAPTURE_STA|=0X80;
TIM4CH3_CAPTURE_VAL=TIM_GetCapture3(TIM4);
TIM_OC3PolarityConfig(TIM4,TIM_ICPolarity_Rising);
}else
{
TIM4CH3_CAPTURE_STA=0;
TIM4CH3_CAPTURE_VAL=0;
TIM_SetCounter(TIM4,0);
TIM4CH3_CAPTURE_STA|=0X40;
TIM_OC3PolarityConfig(TIM4,TIM_ICPolarity_Falling);
}
}
}
TIM_ClearITPendingBit(TIM4,TIM_IT_CC2|TIM_IT_CC3);
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>