转一个stm32单一计时器多路捕获PWM信号的方案

前端之家收集整理的这篇文章主要介绍了转一个stm32单一计时器多路捕获PWM信号的方案前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

地址:http://bbs.5imx3g.com/forum.PHP?mod=viewthread&tid=70

OP: Ok,I've got it (mostly) worked out thanks to a post here and a post elsewhere. The key is changing polarity of the signal in the IRQ handler. Here,I am assuming that the PWMs are phase aligned,so I only need to check for rising/falling edges of the Channel 1 signal. But this could easily be adapted to allow for PWMs that are not phase-aligned.
Here is my current code,reading 4 PWM signals with a single timer (the ccr[] array holds the current PWM pulse widths):

  1. // Hold onto the Channel 1 init structure -- we will use it to reverse
  2. // polarity on every edge interrupt.
  3. static TIM_ICInitTypeDef TIM_CH1_ICInitStructure;
  4. #define GPIO_AF_TIM2 GPIO_AF_2
  5. void ConfigPwmIn() {
  6. GPIO_InitTypeDef GPIO_InitStructure;
  7. TIM_ICInitTypeDef TIM_ICInitStructure;
  8. NVIC_InitTypeDef NVIC_InitStructure;
  9. TIM_DeInit(TIM2 );
  10. /* TIM2 clock enable */
  11. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
  12. /* GPIOC clock enable */
  13. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOD,ENABLE);
  14. /* TIM2 GPIO pin configuration : CH1=PD3,C2=PD4,CH3=PD7,CH4=PD6 */
  15. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_7 | GPIO_Pin_6;
  16. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  17. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  18. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  19. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  20. GPIO_Init(GPIOD,&GPIO_InitStructure);
  21. /* Connect pins to TIM3 AF2 */
  22. GPIO_PinAFConfig(GPIOD,GPIO_PinSource3,GPIO_AF_TIM2 );
  23. GPIO_PinAFConfig(GPIOD,GPIO_PinSource4,GPIO_PinSource7,GPIO_PinSource6,GPIO_AF_TIM2 );
  24. NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
  25. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  26. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  27. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  28. NVIC_Init(&NVIC_InitStructure);
  29. /* Enable capture*/
  30. TIM_CH1_ICInitStructure.TIM_Channel = TIM_Channel_1;
  31. TIM_CH1_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
  32. TIM_CH1_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
  33. TIM_CH1_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
  34. TIM_CH1_ICInitStructure.TIM_ICFilter = 0;
  35. TIM_ICInit(TIM2,&TIM_ICInitStructure);
  36. TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
  37. TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;
  38. TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
  39. TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
  40. TIM_ICInitStructure.TIM_ICFilter = 0;
  41. TIM_ICInit(TIM2,&TIM_ICInitStructure);
  42. TIM_ICInitStructure.TIM_Channel = TIM_Channel_3;
  43. TIM_ICInit(TIM2,&TIM_ICInitStructure);
  44. TIM_ICInitStructure.TIM_Channel = TIM_Channel_4;
  45. TIM_ICInit(TIM2,&TIM_ICInitStructure);
  46. /* Enable TIM2 */
  47. TIM_Cmd(TIM2,ENABLE);
  48. /* Enable CC1-4 interrupt */
  49. TIM_ITConfig(TIM2,TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3 | TIM_IT_CC4,ENABLE);
  50. /* Clear CC1 Flag*/
  51. TIM_ClearFlag(TIM2,TIM_FLAG_CC1 | TIM_FLAG_CC2 | TIM_FLAG_CC3 | TIM_FLAG_CC4 );
  52. }
  53. static volatile uint32_t ccr[4];
  54. static volatile char pulseState = 0;
  55. void TIM2_IRQHandler() {
  56. if (TIM2 ->SR & TIM_IT_CC1 ) {
  57. TIM2 ->SR &= (~TIM_IT_CC1 );
  58. if (pulseState == 0) {
  59. TIM_CH1_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;
  60. // Any time we get a rising edge on CH1,we reset the counter. All channels are
  61. // phase aligned,so they all use this as a reference.
  62. TIM_SetCounter(TIM2,0);
  63. } else {
  64. TIM_CH1_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
  65. // Pull the value on the falling edge.
  66. ccr[0] = TIM_GetCapture1(TIM2 );
  67. }
  68. pulseState = !pulseState;
  69. // Reverse polarity.
  70. TIM_ICInit(TIM2,&TIM_CH1_ICInitStructure);
  71. }
  72. if (TIM2 ->SR & TIM_IT_CC2 ) {
  73. TIM2 ->SR &= (~TIM_IT_CC2 );
  74. ccr[1] = TIM_GetCapture2(TIM2 );
  75. }
  76. if (TIM2 ->SR & TIM_IT_CC3 ) {
  77. TIM2 ->SR &= (~TIM_IT_CC3 );
  78. ccr[2] = TIM_GetCapture3(TIM2 );
  79. }
  80. if (TIM2 ->SR & TIM_IT_CC4 ) {
  81. TIM2 ->SR &= (~TIM_IT_CC4 );
  82. ccr[3] = TIM_GetCapture4(TIM2 );
  83. }
  84. }
复制代码
原文链接:/javaschema/286269.html

猜你在找的设计模式相关文章