stm32普通IO口捕获PWM的高电平

思路:普通IO口配置上升沿下降沿触发中断,然后配置一个定时器(周期大于等于PWM周期),在IO口中断中读取定时器计数。
定时器不需要配置中断
    
    
  1. void TIM3_Configuration(void)
  2. {
  3. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  4. TIM_OCInitTypeDef TIM_OCInitStructure;
  5. /* TIM2 clock enable */
  6. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
  7. TIM_TimeBaseStructure.TIM_Prescaler = 72-1; //72M/72=1M
  8. TIM_TimeBaseStructure.TIM_Period = 20000; //20ms overflow
  9. TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; //
  10. TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //
  11. TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
  12. /*load */
  13. TIM_ARRPreloadConfig(TIM3, ENABLE);
  14. //clear IT bit
  15. TIM_ClearITPendingBit(TIM3,TIM_IT_Update);
  16. /* overflow interrupt*/
  17. //TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
  18. /* allow*/
  19. TIM_Cmd(TIM3, ENABLE);
  20. }
IO口配置
   
   
  1. GPIO_InitTypeDef GPIO_InitStructure;
  2. EXTI_InitTypeDef EXTI_InitStructure;
  3. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE);
  4. RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
  5. /*
  6. PE9 CH1 DJ // PE10 CH2 MD // PE11 CH6 adjust */
  7. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11;
  8. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
  9. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //50M
  10. GPIO_Init(GPIOE, &GPIO_InitStructure);
  11. GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource9);
  12. GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource10);
  13. GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource11);
  14. EXTI_InitStructure.EXTI_Line = EXTI_Line9 | EXTI_Line10 |EXTI_Line11;
  15. EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  16. EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
  17. EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  18. EXTI_Init(&EXTI_InitStructure);
IO中断配置
   
   
  1. NVIC_InitTypeDef NVIC_InitStructure;
  2. /* Configure one bit for preemption priority */
  3. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  4. /* PB6 PE5 interrupt */
  5. NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;
  6. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //highest priority
  7. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  8. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  9. NVIC_Init(&NVIC_InitStructure);
  10. /* PE3 interrupt */
  11. NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;
  12. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  13. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  14. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  15. NVIC_Init(&NVIC_InitStructure);
中断处理函数( counter_reload是20000, TIM_TimeBaseStructure . TIM_Period = 20000 ;
   
   
  1. void EXTI9_5_IRQHandler(void)
  2. {
  3. static u16 C_tmp;
  4. static u16 PE9_Counter;
  5. static u16 PE9_last_count;
  6. if(EXTI_GetITStatus(EXTI_Line9) != RESET)
  7. {
  8. EXTI_ClearITPendingBit(EXTI_Line9);
  9. C_tmp=TIM_GetCounter(TIM3);
  10. //Delay(1);
  11. if(!(GPIOE->IDR & GPIO_Pin_9))
  12. { // PE6=0 falling
  13. PE9_Counter=C_tmp;
  14. PE9_Counter=(PE9_Counter-PE9_last_count)>0?
  15. (PE9_Counter-PE9_last_count):(PE9_Counter-PE9_last_count+counter_reload);
  16. if (PE9_Counter <Min_duty || PE9_Counter >Max_duty) //1ms~2ms
  17. return;
  18. }
  19. else
  20. { //Rising
  21. PE9_last_count=C_tmp;
  22. }
  23. }
  24. }
  25. void EXTI15_10_IRQHandler(void)
  26. {
  27. static u16 C_tmp;
  28. static u16 PE10_Counter;
  29. static u16 PE10_last_count;
  30. static u16 PE11_Counter;
  31. static u16 PE11_last_count;
  32. if(EXTI_GetITStatus(EXTI_Line10) != RESET)
  33. {
  34. EXTI_ClearITPendingBit(EXTI_Line10);
  35. C_tmp=TIM_GetCounter(TIM3);
  36. //Delay(1);
  37. if(!(GPIOE->IDR & GPIO_Pin_10))
  38. { // PE6=0 falling
  39. PE10_Counter=C_tmp;
  40. PE10_Counter=(PE10_Counter-PE10_last_count)>0?
  41. (PE10_Counter-PE10_last_count):(PE10_Counter-PE10_last_count+counter_reload);
  42. if (PE10_Counter <Min_duty || PE10_Counter >Max_duty) //1ms~2ms
  43. return;
  44. }
  45. else
  46. { //Rising
  47. PE10_last_count=C_tmp;
  48. }
  49. }
  50. else if(EXTI_GetITStatus(EXTI_Line11) != RESET)
  51. {
  52. EXTI_ClearITPendingBit(EXTI_Line11);
  53. C_tmp=TIM_GetCounter(TIM3);
  54. if(!(GPIOE->IDR & GPIO_Pin_11))
  55. { // PE6=0 falling
  56. PE11_Counter=C_tmp;
  57. PE11_Counter=(PE11_Counter-PE11_last_count)>0?
  58. (PE11_Counter-PE11_last_count):(PE11_Counter-PE11_last_count+counter_reload);
  59. if (PE11_Counter <Min_duty || PE11_Counter >Max_duty) //1ms~2ms
  60. return;
  61. }
  62. else
  63. { //Rising
  64. PE11_last_count=C_tmp;
  65. }
  66. }
  67. }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值