#include "us100.h"
u8 TIM1CH3_CAPTURE_STA=0;
u32 TIM1CH3_CAPTURE_VAL;
uint32_t num;
void TIM1_CH3_Cap_Init(u32 arr,u16 psc)
{
TIM_ICInitTypeDef TIM1_ICInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_TIM1);
TIM_TimeBaseStructure.TIM_Prescaler=psc;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period=arr;
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure);
TIM1_ICInitStructure.TIM_Channel =TIM_Channel_3;
TIM1_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM1_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM1_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM1_ICInitStructure.TIM_ICFilter = 0x00;
TIM_ICInit(TIM1, &TIM1_ICInitStructure);
TIM_ITConfig(TIM1,TIM_IT_Update|TIM_IT_CC3,ENABLE);
TIM_Cmd(TIM1,ENABLE );
NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn ;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority =0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void TIM1_CC_IRQHandler(void)
{
if((TIM1CH3_CAPTURE_STA&0X80)==0)
{
if(TIM_GetITStatus(TIM1, TIM_IT_Update) != RESET)
{
if(TIM1CH3_CAPTURE_STA&0X40)
{
if((TIM1CH3_CAPTURE_STA&0X3F)==0X3F)
{
TIM1CH3_CAPTURE_STA|=0X80;
TIM1CH3_CAPTURE_VAL=0XFFFFFFFF;
}else TIM1CH3_CAPTURE_STA++;
}
}
if(TIM_GetITStatus(TIM1, TIM_IT_CC3) != RESET)
{
if(TIM1CH3_CAPTURE_STA&0X40)
{
TIM1CH3_CAPTURE_STA|=0X80;
TIM1CH3_CAPTURE_VAL=TIM_GetCapture3(TIM1);
TIM_OC1PolarityConfig(TIM1,TIM_ICPolarity_Rising);
}else
{
TIM1CH3_CAPTURE_STA=0;
TIM1CH3_CAPTURE_VAL=0;
TIM1CH3_CAPTURE_STA|=0X40;
TIM_Cmd(TIM1,ENABLE );
TIM_SetCounter(TIM1,0);
TIM_OC1PolarityConfig(TIM1,TIM_ICPolarity_Falling);
TIM_Cmd(TIM1,ENABLE );
}
}
}
TIM_ClearITPendingBit(TIM1, TIM_IT_CC3|TIM_IT_Update);
}
//初始化PA9
void GPIO(void)
{
GPIO_InitTypeDef gpio;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
gpio.GPIO_Pin = GPIO_Pin_9;
gpio.GPIO_Mode = GPIO_Mode_OUT;
gpio.GPIO_OType = GPIO_OType_PP;
gpio.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOA,&gpio);
}
//延迟函数
void delay_us(unsigned int t)
{
int i;
for( i=0;i<t;i++)
{
int a=40;
while(a--);
}
}
//获取距离函数
int32_t get_distance()
{
PA9_ON();
delay_us(20);
PA9_OFF();
if(TIM1CH3_CAPTURE_STA&0x80)
{
num=TIM1CH3_CAPTURE_STA&0x3F;
num*=65536;
num+=TIM1CH3_CAPTURE_VAL;
TIM1CH3_CAPTURE_STA=0;
}
return num;
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
TIM_SetCounter(TIM1,0);
TIM_OC1PolarityConfig(TIM1,TIM_ICPolarity_Falling);
TIM_Cmd(TIM1,ENABLE );
代码中这一段,第一句应该是 TIM_Cmd(TIM1,DISABLE ); 这样在试试
一周热门 更多>