当前位置:   article > 正文

STM32测相位差(根据时间差)_stm32f4测量方波相位差

stm32f4测量方波相位差

         两路方波输入到stm32的两路定时器通道,通过检测高电平到来的时间差从而算出相位差,

公式  相位差=360*频率*(时间差)

        如果要测正弦波,可以通过电压比较电路转为方波

        

ddb1f5fd884b4a10b96a95a73d7f9ab1.png

定时器初始化及定时器中断代码:

  1. void TIM5_Cap_Init(uint16_t arr,uint16_t psc)
  2. {
  3. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
  4. GPIO_InitTypeDef GPIO_InitStructure;
  5. GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPD;
  6. GPIO_InitStructure.GPIO_Pin=GPIO_Pin_3|GPIO_Pin_2;
  7. GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  8. GPIO_Init(GPIOA,&GPIO_InitStructure);
  9. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5,ENABLE);
  10. TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
  11. TIM_TimeBaseStructInit(&TIM_TimeBaseInitStructure);
  12. TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;
  13. TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;
  14. TIM_TimeBaseInitStructure.TIM_Period=arr;
  15. TIM_TimeBaseInitStructure.TIM_Prescaler=psc;
  16. TIM_TimeBaseInit(TIM5,&TIM_TimeBaseInitStructure);
  17. TIM_ICInitTypeDef TIM_ICInitStructure;
  18. TIM_ICInitStructure.TIM_Channel=TIM_Channel_4;
  19. TIM_ICInitStructure.TIM_ICFilter=0x00;
  20. TIM_ICInitStructure.TIM_ICPolarity=TIM_ICPolarity_Rising;
  21. TIM_ICInitStructure.TIM_ICPrescaler=TIM_ICPSC_DIV1;
  22. TIM_ICInitStructure.TIM_ICSelection=TIM_ICSelection_DirectTI;
  23. TIM_ICInit(TIM5,&TIM_ICInitStructure);
  24. TIM_ICInitStructure.TIM_Channel=TIM_Channel_3;
  25. TIM_ICInitStructure.TIM_ICFilter=0x00;
  26. TIM_ICInitStructure.TIM_ICPolarity=TIM_ICPolarity_Rising;
  27. TIM_ICInitStructure.TIM_ICPrescaler=TIM_ICPSC_DIV1;
  28. TIM_ICInitStructure.TIM_ICSelection=TIM_ICSelection_DirectTI;
  29. TIM_ICInit(TIM5,&TIM_ICInitStructure);
  30. NVIC_InitTypeDef NVIC_InitStructure;
  31. NVIC_InitStructure.NVIC_IRQChannel=TIM5_IRQn;
  32. NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
  33. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;
  34. NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
  35. NVIC_Init(&NVIC_InitStructure);
  36. TIM_ITConfig(TIM5,TIM_IT_Update|TIM_IT_CC4|TIM_IT_CC3,ENABLE);
  37. TIM_Cmd(TIM5,ENABLE);
  38. }
  39. u8 TIM5CH4_Cap_State; //第一路信号捕获标志
  40. u16 TIM5CH4_Cap_Value; //第一路信号捕获值
  41. u8 TIM5CH3_Cap_State; //第二路信号捕获标志
  42. u16 TIM5CH3_Cap_Value; //第二路信号捕获值
  43. void TIM5_IRQHandler()
  44. {
  45. if((TIM5CH4_Cap_State&0x80)==0) //第一路信号未捕获到下降沿
  46. {
  47. if(TIM_GetITStatus(TIM5,TIM_IT_Update)==SET)
  48. {
  49. if(TIM5CH4_Cap_State&0x40) //第一路信号已捕获到上升沿
  50. {
  51. if((TIM5CH4_Cap_State&0x3f)==0x3f) //第一路信号上升沿到下降沿时间过长,捕获失败
  52. {
  53. TIM5CH4_Cap_State|=0x80;
  54. TIM5CH4_Cap_Value=0xffff;
  55. }
  56. else TIM5CH4_Cap_State++;
  57. }
  58. }
  59. if(TIM_GetITStatus(TIM5,TIM_IT_CC4)==SET) //捕获中断
  60. {
  61. if(TIM5CH4_Cap_State&0x40) //第一路信号已经捕获到上升沿,则此次为下降沿被捕获
  62. {
  63. TIM5CH4_Cap_State|=0x80; //标志一次检测完成
  64. TIM5CH4_Cap_Value=TIM_GetCapture4(TIM5); //取出定时器的值
  65. TIM_OC4PolarityConfig(TIM5,TIM_ICPolarity_Rising); //设置为上升沿捕获
  66. }
  67. else //第一路未捕获到上升沿,则此次为上升沿被捕获
  68. {
  69. TIM5CH4_Cap_State=0;
  70. TIM5CH4_Cap_Value=0;
  71. TIM_SetCounter(TIM5,0);
  72. TIM5CH4_Cap_State|=0X40; //标记捕获到上升沿
  73. TIM_OC4PolarityConfig(TIM5,TIM_ICPolarity_Falling); //设置为下降沿捕获
  74. }
  75. }
  76. }
  77. if((TIM5CH3_Cap_State&0x80)==0)
  78. {
  79. if(TIM_GetITStatus(TIM5,TIM_IT_Update)==SET)
  80. {
  81. if(TIM5CH3_Cap_State&0x40)
  82. {
  83. if((TIM5CH3_Cap_State&0x3f)==0x3f)
  84. {
  85. TIM5CH3_Cap_State|=0x80;
  86. TIM5CH3_Cap_Value=0xffff;
  87. }
  88. else TIM5CH3_Cap_State++;
  89. }
  90. }
  91. if(TIM_GetITStatus(TIM5,TIM_IT_CC3)==SET)
  92. {
  93. if(TIM5CH3_Cap_State&0x40)
  94. {
  95. TIM5CH3_Cap_State|=0x80;
  96. TIM5CH3_Cap_Value=TIM_GetCapture3(TIM5);
  97. TIM_OC3PolarityConfig(TIM5,TIM_ICPolarity_Rising);
  98. }
  99. else
  100. {
  101. TIM5CH3_Cap_State=0;
  102. TIM5CH3_Cap_Value=0;
  103. TIM_SetCounter(TIM5,0);
  104. TIM5CH3_Cap_State|=0X40;
  105. TIM_OC3PolarityConfig(TIM5,TIM_ICPolarity_Falling);
  106. }
  107. }
  108. }
  109. TIM_ClearITPendingBit(TIM5,TIM_IT_Update|TIM_IT_CC4|TIM_IT_CC3);
  110. }

在主程序中求其中一路信号的频率,再使用开头给出的公式可求得相位差

下面给出关于求相位差部分的主程序代码:

  1. if((TIM5CH4_Cap_State&0x80) && (TIM5CH3_Cap_State&0x80))
  2. {
  3. timer4_temp=TIM5CH4_Cap_State&0x3f;
  4. timer4_temp*=65536;
  5. timer4_temp+=TIM5CH4_Cap_Value;
  6. timer3_temp=TIM5CH3_Cap_State&0x3f;
  7. timer3_temp*=65536;
  8. timer3_temp+=TIM5CH3_Cap_Value;
  9. if(timer4_temp > timer3_temp)
  10. time_diff = timer4_temp - timer3_temp;
  11. else
  12. time_diff = timer3_temp - timer4_temp;
  13. phase_diff = 360*F*time_diff/1000000;
  14. LCD_ShowNum(410,5+24*1,phase_diff,4,24);
  15. LCD_ShowString(410+4*12,5+24*1,12,24,24,".");
  16. LCD_ShowNum(410+5*12,5+24*1,(u16)(phase_diff*10)%10,1,24);
  17. TIM5CH4_Cap_State = 0;
  18. TIM5CH3_Cap_State = 0;
  19. }

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号