使用STM32的计数器功能,实现8路的脉冲计数,我在程序中使用了,两个计数器 TIM2和TIM3,配置代码如下: void TIM2_Configuration(void) { TIM_ICInitTypeDef TIM_ICInitStructure; TIM_DeInit(TIM2); /* Enable capture*/ TIM_CH1_ICInitStructure.TIM_Channel = TIM_Channel_1; TIM_CH1_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; TIM_CH1_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; TIM_CH1_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; TIM_CH1_ICInitStructure.TIM_ICFilter = 0; TIM_ICInit(TIM2, &TIM_ICInitStructure); TIM_ICInitStructure.TIM_Channel = TIM_Channel_2; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling; TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; TIM_ICInitStructure.TIM_ICFilter = 0; TIM_ICInit(TIM2, &TIM_ICInitStructure); TIM_ICInitStructure.TIM_Channel = TIM_Channel_3; TIM_ICInit(TIM2, &TIM_ICInitStructure); TIM_ICInitStructure.TIM_Channel = TIM_Channel_4; TIM_ICInit(TIM2, &TIM_ICInitStructure); /* Enable TIM2 */ TIM_Cmd(TIM2, ENABLE); /* Enable CC1-4 interrupt */ TIM_ITConfig(TIM2, TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3 | TIM_IT_CC4, ENABLE); /* Clear CC1 Flag*/ TIM_ClearFlag(TIM2, TIM_FLAG_CC1 | TIM_FLAG_CC2 | TIM_FLAG_CC3 | TIM_FLAG_CC4 ); } void TIM3_Configuration(void) { TIM_ICInitTypeDef TIM_ICInitStructure; TIM_DeInit(TIM3); /* Enable capture*/ TIM_CH1_ICInitStructure.TIM_Channel = TIM_Channel_1; TIM_CH1_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; TIM_CH1_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; TIM_CH1_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; TIM_CH1_ICInitStructure.TIM_ICFilter = 0; TIM_ICInit(TIM3, &TIM_ICInitStructure); TIM_ICInitStructure.TIM_Channel = TIM_Channel_2; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling; TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; TIM_ICInitStructure.TIM_ICFilter = 0; TIM_ICInit(TIM3, &TIM_ICInitStructure); TIM_ICInitStructure.TIM_Channel = TIM_Channel_3; TIM_ICInit(TIM3, &TIM_ICInitStructure); TIM_ICInitStructure.TIM_Channel = TIM_Channel_4; TIM_ICInit(TIM3, &TIM_ICInitStructure); /* Enable TIM2 */ TIM_Cmd(TIM3, ENABLE); /* Enable CC1-4 interrupt */ TIM_ITConfig(TIM3, TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3 | TIM_IT_CC4, ENABLE); /* Clear CC1 Flag*/ TIM_ClearFlag(TIM3, TIM_FLAG_CC1 | TIM_FLAG_CC2 | TIM_FLAG_CC3 | TIM_FLAG_CC4 ); } 中断函数如下: void TIM2_IRQHandler(void) { if(TIM2 ->SR & TIM_IT_CC1 ) { TIM2 ->SR &= (~TIM_IT_CC1 ); if(pulseState1 == 0) { TIM_CH1_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling; // Any time we get a rising edge on CH1, we reset the counter. All channels are // phase aligned, so they all use this as a reference. TIM_SetCounter(TIM2, 0); } else { TIM_CH1_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; // Pull the value on the falling edge. ccr[0] = TIM_GetCapture1(TIM2 ); } pulseState1 = !pulseState1; // Reverse polarity. TIM_ICInit(TIM2, &TIM_CH1_ICInitStructure); } if(TIM2 ->SR & TIM_IT_CC2 ) { TIM2 ->SR &= (~TIM_IT_CC2 ); ccr[1] = TIM_GetCapture2(TIM2 ); } if(TIM2 ->SR & TIM_IT_CC3 ) { TIM2 ->SR &= (~TIM_IT_CC3 ); ccr[2] = TIM_GetCapture3(TIM2 ); } if(TIM2 ->SR & TIM_IT_CC4 ) { TIM2 ->SR &= (~TIM_IT_CC4 ); ccr[3] = TIM_GetCapture4(TIM2 ); } } void TIM3_IRQHandler(void) { if(TIM3 ->SR & TIM_IT_CC1) { TIM3 ->SR &= (~TIM_IT_CC1); if(pulseState2 == 0) { TIM_CH1_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling; // Any time we get a rising edge on CH1, we reset the counter. All channels are // phase aligned, so they all use this as a reference. TIM_SetCounter(TIM3, 0); } else { TIM_CH1_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; // Pull the value on the falling edge. ccr[4] = TIM_GetCapture1(TIM3); } pulseState2 = !pulseState2; // Reverse polarity. TIM_ICInit(TIM3, &TIM_CH1_ICInitStructure); } if(TIM3 ->SR & TIM_IT_CC2) { TIM3 ->SR &= (~TIM_IT_CC2 ); ccr[5] = TIM_GetCapture2(TIM3); } if(TIM3 ->SR & TIM_IT_CC3 ) { TIM3 ->SR &= (~TIM_IT_CC3 ); ccr[6] = TIM_GetCapture3(TIM3); } if(TIM3 ->SR & TIM_IT_CC4 ) { TIM3 ->SR &= (~TIM_IT_CC4 ); ccr[7] = TIM_GetCapture4(TIM3); } } 但是由于计数器优先级的问题,它的计数不是很完美,因为配置TIM2优先级高于TIM3,则8路脉冲同时到来的时候,只有TIM2计数,而TIM3没有计数。 请问各位大侠,有没有解决的办法??? 求助!!! |
【MCU实战经验】基于STM32F103C8T6的hart总线调试器设计
求教STM32F103进入STOP模式后用外部中断唤醒的问题
基于STM32F103RCT6的无源蜂鸣器音乐播放(生日快乐歌)
STM32F103c8t6有没有DAC 功能?
STM32F103x中文数据手册
新手求教,为何在我电脑上找不到STM32F1Xx.h文件
金龙107例程汇总(STM32F107)
万利STM32F107VC 原理图
STM32F103 ADC多通道检测必须要DMA吗?
【官方例程】STM32F107以太网官方例程
RE:【求助】 STM32F1的8路脉冲计数器
回复:【求助】 STM32F1的8路脉冲计数器
在TIM2 中添加TIM3 判断的部分
请问怎么添加呢?能否详细一些,本人是新手。
RE:【求助】 STM32F1的8路脉冲计数器
小弟不胜感激!!!