把超声波的原程序的定时器TIM5改成了TIM2不能用了,懂得能帮忙看一下吗
下面是该过的程序
#include "capture.h"
/**PA0,ͨ¹ýÊäÈ벶»ñ£¬»ñÈ¡ÊäÈëÐźŵÄÂö³å¿í¶È(¸ßµçƽ³ÖÐøʱ¼ä)****/
void CAPTURE_Init(void)
{
GPIO_InitTypeDef GPIO_IniStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
GPIO_IniStructure.GPIO_Mode =GPIO_Mode_IPU;//PA0(ECHO»ØÏìÐźÅÊäÈë½Å)
GPIO_IniStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_Init(GPIOB,&GPIO_IniStructure);
GPIO_IniStructure.GPIO_Mode = GPIO_Mode_Out_PP;
//pc13(Trig)ÊäÈë´¥·¢ÐźŽţ¬³ÖÐø¸ø10usÒÔÉϸߵçƽ
GPIO_IniStructure.GPIO_Pin= GPIO_Pin_13;
GPIO_IniStructure.GPIO_Speed = GPIO_Speed_50MHz;
// GPIO_Init(GPIOA,&GPIO_IniStructure);
GPIO_Init(GPIOC,&GPIO_IniStructure);
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;//ÏòÉϼÆÊý
TIM_TimeBaseInitStructure.TIM_Period = 65535;//×Ô¶¯ÖØ×°ÔØÖµ
TIM_TimeBaseInitStructure.TIM_Prescaler = 71;//Ô¤·ÖƵֵ
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure);//T5,ÏòÉϼÆÊýÒ»´Î1us£¬×ܵļÆÊýÒç³öʱ¼ä65536us
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;//ÊäÈëͨµÀÑ¡Ôñ
TIM_ICInitStructure.TIM_ICFilter = 0;//¹Ì¶¨²ÉÑùƵÂʲÉÑùÒ»´Î,²»Æðµ½Ïû³ýÔÓ²¨µÄ×÷ÓÃ
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;//ͨµÀÉÏÉýÑز¶»ñ
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;//CC1ͨµÀ±»ÅäÖÃΪÊäÈ룬 IC1Ó³ÉäÔÚTI1ÉÏ
TIM_ICInitStructure.TIM_ICPrescaler= TIM_ICPSC_DIV1;//Ò»´ÎÉÏÉýÑØ£¨Ï½µÑØ£©¼´´¥·¢²¶»ñ
TIM_ICInit(TIM2,&TIM_ICInitStructure);
TIM_ITConfig(TIM2,TIM_IT_CC2|TIM_IT_Update,ENABLE);//Öд¥·¢ÀàÐÍ£º¸üУ¨Òç³ö£©¡¢²¶»ñ/±È½Ï
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_Init(&NVIC_InitStructure);//¿ªÖжϣ¬ÉèÖÃÖжϴ¥·¢ÀàÐÍ
TIM_Cmd(TIM2,ENABLE);//ʹÄܶ¨Ê±Æ÷£¬¿ªÊ¼¹¤×÷
}
//2¸öÈ«¾Ö±äÁ¿
u8 TIM2CH1_CAPTURE_STA=0; //ÊäÈ벶»ñ״̬
u16 TIM2CH1_CAPTURE_VAL; //ÊäÈ벶»ñÖµ
//¶¨Ê±Æ÷5ÖжϷþÎñ³ÌÐò
/***********************************************************************
ICͨµÀ³õʼ»¯ÉèÖÃΪÉÏÉýÑØ´¥·¢²¶»ñ£¬ÉèÖò¶»ñֵΪ0£¬ÉèÖüÆÊýÆ÷ֵҲΪ0 £¬ICͨµÀ¼«ÐÔÑ¡ÔñΪϽµÑØ
ÏÂÒ»´ÎϽµÑØ´¥·¢²¶»ñ£¬½«¼ÆÊýÆ÷µÄÖµ¸³¸ø²¶»ñÖµ¡£
µ«Òª×¢ÒâÒ»¸öÎÊÌ⣺µ¥´Î¸ßµçƽ³ÖÐøʱ¼ä¿ÉÄܳ¬¹ýÒ»´ÎÒç³öʱ¼ä£¬ËùÒÔÒª¿¼ÂǶ¨Ê±Æ÷Òç³ö´ÎÊý£¡£¡
ÖжϺ¯Êý×÷ÓãºÄ£ÄâÒ»¸öÈí¼þ8λ¼Ä´æÆ÷£¬µÍ6λ´æ´¢Òç³ö´ÎÊý¡£
bit7 bit6 bit5-0
²¶»ñ±êÖ¾ ²¶»ñ¸ßµçƽ±êÖ¾ ²¶»ñ¸ßµçƽºó¶¨Ê±Æ÷Òç³ö´ÎÊý
************************************************************************/
//¶¨Ê±Æ÷5ÖжϷþÎñ³ÌÐò
void TIM2_IRQHandler(void)
{
if((TIM2CH1_CAPTURE_STA&0X80)==0)//»¹Î´³É¹¦²¶»ñ
{ //×î´ó²âÁ¿·¶Î§4m¶ÔÓ¦EchoÊäÈë23.53msµÄ¸ßµçƽ,µ¥´ÎTIMÒç³öÐèÒª65.536ms
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
{
if(TIM2CH1_CAPTURE_STA&0X40)//ÒѾ­²¶»ñµ½¸ßµçƽÁË
{
if((TIM2CH1_CAPTURE_STA&0X3F)==0X3F)//¸ßµçƽ̫³¤ÁË
{
TIM2CH1_CAPTURE_STA|=0X80;//Ç¿ÖƱê¼Ç³É¹¦²¶»ñÁËÒ»´Î
TIM2CH1_CAPTURE_VAL=0XFFFF;
}else TIM2CH1_CAPTURE_STA++;
}
}
if (TIM_GetITStatus(TIM2, TIM_IT_CC2) != RESET)//²¶»ñ1·¢Éú²¶»ñʼþ
{
if(TIM2CH1_CAPTURE_STA&0X40)//Ç°Ò»´Î²¶»ñÁËÉÏÉýÑØ£¬´Ë´Î²¶»ñµ½Ò»¸öϽµÑØ
{
TIM2CH1_CAPTURE_STA|=0X80; //±ê¼Ç³É¹¦²¶»ñµ½Ò»´ÎÉÏÉýÑØ
TIM2CH1_CAPTURE_VAL=TIM_GetCapture1(TIM2);
TIM_OC2PolarityConfig(TIM2,TIM_ICPolarity_Rising); //CC1P=0 ÉèÖÃΪÉÏÉýÑز¶»ñ
}else //»¹Î´¿ªÊ¼,µÚÒ»´Î²¶»ñÉÏÉýÑØ
{
TIM2CH1_CAPTURE_STA=0; //Çå¿Õ
TIM2CH1_CAPTURE_VAL=0;
TIM_SetCounter(TIM2,0);//¶¨Ê±Æ÷´Ó0¿ªÊ¼ÖØмÆÊý
TIM2CH1_CAPTURE_STA|=0X40; //±ê¼Ç²¶»ñµ½ÁËÉÏÉýÑØ
TIM_OC2PolarityConfig(TIM2,TIM_ICPolarity_Falling); //CC1P=1 ÉèÖÃΪϽµÑز¶»ñ
}
}
}
TIM_ClearITPendingBit(TIM2, TIM_IT_CC2|TIM_IT_Update); //Çå³ýÖжϱê־λ
}
一周热门 更多>