我用tim4_ch1通道,PD12还可以,但是换成TIM9_CH1,PE5就没有输出结果了,大家帮我看看,是我的 NVIC_InitStructure.NVIC_IRQChannel = TIM1_BRK_TIM9_IRQn;这一步不对吗?还是哪里错了。
TIM_ICInitTypeDef TIM9_ICInitStructure;
//¶¨ê±Æ÷3í¨μà1êäèë2¶»ñÅäÖÃ
//arr£o×Ô¶ˉÖØ×°Öμ(TIM2,TIM5êÇ32λμÄ!!)
//psc£oê±ÖóÔ¤·ÖÆμêy
void TIM9_CH1_Cap_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM9,ENABLE); //TIM5ê±Öóê1Äü
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE); //ê1ÄüPORTAê±Öó
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //GPIOA0
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//¸′óÃ1|Äü
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //Ëù¶è100MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //íÆíì¸′óÃêä3ö
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; //ÏÂà-
GPIO_Init(GPIOE,&GPIO_InitStructure); //3õê¼»ˉPA0
GPIO_PinAFConfig(GPIOE,GPIO_PinSource5,GPIO_AF_TIM9); //PA0¸′óÃ붨ê±Æ÷5
TIM_TimeBaseStructure.TIM_Prescaler=psc; //¶¨ê±Æ÷·ÖÆμ
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //ÏòéϼÆêyÄ£ê½
TIM_TimeBaseStructure.TIM_Period=arr; //×Ô¶ˉÖØ×°ÔØÖμ
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM9,&TIM_TimeBaseStructure);
//3õê¼»ˉTIM5êäèë2¶»ñ2Îêy
TIM9_ICInitStructure.TIM_Channel = TIM_Channel_1; //CC1S=01 Ñ¡Ôñêäèë¶Ë IC1ó3éäμ½TI1éÏ
TIM9_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //éÏéyÑØ2¶»ñ
TIM9_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //ó3éäμ½TI1éÏ
TIM9_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //ÅäÖÃêäèë·ÖÆμ,2»·ÖÆμ
TIM9_ICInitStructure.TIM_ICFilter = 0x00;//IC1F=0000 ÅäÖÃêäèëÂË2¨Æ÷ 2»ÂË2¨
TIM_ICInit(TIM9, &TIM9_ICInitStructure);
TIM_ITConfig(TIM9,TIM_IT_Update|TIM_IT_CC1,ENABLE);//ÔêDí¸üDÂÖD¶Ï ,ÔêDíCC1IE2¶»ñÖD¶Ï
TIM_Cmd(TIM9,ENABLE ); //ê1Äü¶¨ê±Æ÷5
NVIC_InitStructure.NVIC_IRQChannel = TIM1_BRK_TIM9_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;//ÇàÕ¼óÅÏ輶3
NVIC_InitStructure.NVIC_IRQChannelSubPriority =0; //×óóÅÏ輶3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μàê1Äü
NVIC_Init(&NVIC_InitStructure); //¸ù¾YÖ¸¶¨μÄ2Îêy3õê¼»ˉVIC¼Ä′æÆ÷¡¢
}
u8 TIM9CH1_CAPTURE_STA=0; //êäèë2¶»ñ×′ì¬
u16 TIM9CH1_CAPTURE_VAL; //êäèë2¶»ñÖμ(TIM2/TIM5êÇ32λ)
void TIM9_IRQHandler(void)
{
if((TIM9CH1_CAPTURE_STA&0X80)==0)//»1Î′3é1|2¶»ñ
{
if(TIM_GetITStatus(TIM9, TIM_IT_Update) != RESET)//òç3ö
{
if(TIM9CH1_CAPTURE_STA&0X40)//òѾ-2¶»ñμ½¸ßμçƽáË
{
if((TIM9CH1_CAPTURE_STA&0X3F)==0X3F)//¸ßμçƽì«3¤áË
{
TIM9CH1_CAPTURE_STA|=0X80; //±ê¼Ç3é1|2¶»ñáËò»′Î
TIM9CH1_CAPTURE_VAL=0XFFFF;
}else TIM9CH1_CAPTURE_STA++;
}
}
if(TIM_GetITStatus(TIM9, TIM_IT_CC1) != RESET)//2¶»ñ1·¢éú2¶»ñê¼t
{
// if(TIM5CH1_CAPTURE_STA&0X40) //2¶»ñμ½ò»¸öϽμÑØ
// {
TIM9CH1_CAPTURE_STA|=0X80; //±ê¼Ç3é1|2¶»ñμ½ò»′θßμçƽÂö¿í
TIM9CH1_CAPTURE_VAL=TIM_GetCapture1(TIM9);//»ñè¡μ±Ç°μÄ2¶»ñÖμ.
// TIM_OC1PolarityConfig(TIM5,TIM_ICPolarity_Rising); //CC1P=0 éèÖÃÎaéÏéyÑØ2¶»ñ
// }else //»1Î′¿aê¼,μúò»′Î2¶»ñéÏéyÑØ
// {
// TIM5CH1_CAPTURE_STA=0; //Çå¿Õ
// TIM5CH1_CAPTURE_VAL=0;
// TIM5CH1_CAPTURE_STA|=0X40; //±ê¼Ç2¶»ñμ½áËéÏéyÑØ
// TIM_Cmd(TIM5,DISABLE ); //1رն¨ê±Æ÷5
TIM_SetCounter(TIM9,0);
// TIM_OC1PolarityConfig(TIM5,TIM_ICPolarity_Falling); //CC1P=1 éèÖÃÎaϽμÑØ2¶»ñ
// TIM_Cmd(TIM5,ENABLE ); //ê1Äü¶¨ê±Æ÷5
// }
}
}
TIM_ClearITPendingBit(TIM9, TIM_IT_CC1|TIM_IT_Update); //Çå3yÖD¶Ï±ê־λ
}
extern u8 TIM9CH1_CAPTURE_STA; //êäèë2¶»ñ×′ì¬
extern u16 TIM9CH1_CAPTURE_VAL; //êäèë2¶»ñÖμ
int main(void)
{
float temp=0;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//éèÖÃÏμí3ÖD¶ÏóÅÏ輶·Ö×é2
delay_init(168); //3õê¼»ˉÑóê±oˉêy
uart_init(115200);//3õê¼»ˉ′®¿ú2¨ìØÂêÎa115200
TIM14_PWM_Init(500-1,84-1); //84M/84=1MhzμļÆêyÆμÂê¼Æêyμ½500,PWMÆμÂêÎa1M/500=2Khz
TIM9_CH1_Cap_Init(0XFFFF,84-1); //òÔ1MhzμÄÆμÂê¼Æêy
while(1)
{
delay_ms(10);
TIM_SetCompare1(TIM14,TIM_GetCapture1(TIM14)+1);
if(TIM_GetCapture1(TIM14)==300)TIM_SetCompare1(TIM14,0);
if(TIM9CH1_CAPTURE_STA&0X80) //3é1|2¶»ñμ½áËò»′θßμçƽ
{
temp=TIM9CH1_CAPTURE_STA&0X3F;
temp*=0XFFFF; //òç3öê±¼ä×üoí
temp+=TIM9CH1_CAPTURE_VAL; //μÃμ½×üμĸßμçƽ걼ä
printf("CNT= %0.2f r/min
",1/(temp/1000000)*60); //′òó¡×üμĸßμãƽ걼ä
TIM9CH1_CAPTURE_STA=0; //¿aÆôÏÂò»′Î2¶»ñ
}
}
}
一周热门 更多>