编码器学习STM32(3)

2018-12-29 23:16

33. FreqBuf[cnt] = (1000000L * 100) / 34. tmp16; 35. //*100为扩大显示量程

36. Freq_Value[0]=tmp16_CH1; } }

37. if(TIM_GetITStatus(TIM4, TIM_IT_CC2) == SET) { 38. TIM_ClearITPendingBit(TIM4, TIM_IT_CC2); 39. if(capture_number_CH2 == 0) { 40. capture_number_CH2 = 1;

41. last_time_CH2 = TIM_GetCapture2(TIM4); } 42. else if(capture_number_CH2 == 1) { 43. capture_number_CH2 = 0;

44. this_time_CH2 = TIM_GetCapture2(TIM4); 45. if(this_time_CH2 > last_time_CH2{

46. tmp16_CH2 = (this_time_CH2 - last_time_CH2); } else {

47. tmp16_CH2 = ((0xFFFF - last_time_CH2) + this_time_CH2); } //TIM2 48. counter clock = 1MHz

49. FreqBuf[cnt] = (1000000L * 100) / 50. tmp16; 51. //*100为扩大显示量程

52. Freq_Value[1]=tmp16_CH2; }}

53. if(TIM_GetITStatus(TIM4, TIM_IT_CC3) == SET) { 54. TIM_ClearITPendingBit(TIM4, TIM_IT_CC3); 55. if(capture_number_CH3 == 0) { 56. capture_number_CH3 = 1;

57. last_time_CH3 = TIM_GetCapture3(TIM4); } 58. else if(capture_number_CH3 == 1){ 59. capture_number_CH3 = 0;

60. this_time_CH3 = TIM_GetCapture3(TIM4); 61. if(this_time_CH3 > last_time_CH3) {

62. tmp16_CH3 = (this_time_CH3 - last_time_CH3); } else{

63. tmp16_CH3 = ((0xFFFF - last_time_CH3) + this_time_CH3); } //TIM2 64. counter clock = 1MHz //

65. FreqBuf[cnt] = (1000000L * 100) / 66. tmp16; 67. //*100为扩大显示量程

68. Freq_Value[2]=tmp16_CH3; } 69. }

70. if(TIM_GetITStatus(TIM4, TIM_IT_CC4) == SET) 71. {

72. TIM_ClearITPendingBit(TIM4, TIM_IT_CC4); 73. if(capture_number_CH4 == 0) { 74. capture_number_CH4 = 1;

75. last_time_CH4 = TIM_GetCapture4(TIM4); } 76. else if(capture_number_CH4 == 1){ 77. capture_number_CH4 = 0;

78. this_time_CH4 = TIM_GetCapture4(TIM4); 79. if(this_time_CH4 > last_time_CH4) {

80. tmp16_CH4 = (this_time_CH4 - last_time_CH4); } else {

81. tmp16_CH4 = ((0xFFFF - last_time_CH4) + this_time_CH4); } //TIM2 82. counter clock = 1MHz

83. FreqBuf[cnt] = (1000000L * 100) / 84. tmp16; 85. //*100为扩大显示量程

86. Freq_Value[3]=tmp16_CH4; }} // 87. GPIO_WriteBit(GPIOC, GPIO_Pin_13,

88. (BitAction)((1-GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_13))));}

复制代码

中断内四部分代码完全一样,只分析其中一段

输入捕获的原理是,定时器正常计数运行,当外部脉冲到来时,将定时器计数值存起来,当下次脉冲到来时,求出这两次计数值差值,即为这两段脉冲的周期。

例如,定时器计数到10,外部脉冲到来,使用last_time_CH1存储10,下次脉冲到来,此时定时器计数值运行到110,使用this_time_CH1存储110,之后做差,tmp16_CH1存储差值100,由于定时器运行于100KHZ,10us计数值增加一次,所以脉冲周期为100*10=1000us=1ms,即为1KHZ。

当然,定时器会溢出重装,此时需要将差值补偿运算,tmp16_CH1 = ((0xFFFF - last_time_CH1) + this_time_CH1); 可测量的范围取决于定时器运行的频率,如果外部频率慢到当定时器整个计数一周后也没有触发两次,会发生溢出,此时计数值已不准确。所以定时器时钟配置取决于外部脉冲频率,应配置得当使得脉冲频率范围不致溢出。

由于每次外部脉冲都会触发中断,尤其是四通道时,所以使用中断方式会略微占用CPU资源,使用DMA可以解决这一问题。 得到脉冲周期后,即可通过运算获得外部频率,进而测速。 例程(6)

This example shows how to use the TIM peripheral to measure the frequency and duty cycle of an external signal.

The TIMxCLK frequency is set to 72 MHz, the Prescaler is 0 so the TIM2 counter clock is 72 MHz. so the minimum frequency value to measure is 1100 Hz. TIM2 is configured in PWM Input Mode: the external signal is connected to TIM2 Channel2 used as input pin.

To measure the frequency and the duty cycle we use the TIM2 CC2 interrupt request, so In the TIM2_IRQHandler routine, the frequency and the duty cycle of the external signal are computed.

The \Frequency = TIM2 counter clock / TIM2_CCR2 in Hz,

The \Duty_Cycle = (TIM2_CCR1*100)/(TIM2_CCR2) in %.

/* Includes ------------------------------------------------------------------*/ #include \

/* Private typedef -----------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/ /* Private macro -------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_ICInitTypeDef TIM_ICInitStructure; ErrorStatus HSEStartUpStatus;

/* Private function prototypes -----------------------------------------------*/ void RCC_Configuration(void); void GPIO_Configuration(void); void NVIC_Configuration(void);

/********************************************************************************/ int main(void) {

#ifdef DEBUG debug(); #endif

/* System Clocks Configuration */ RCC_Configuration();

/* NVIC configuration */ NVIC_Configuration();

/* Configure the GPIO ports */ GPIO_Configuration();

/* TIM2 configuration: PWM Input mode ------------------------ The external signal is connected to TIM2 CH2 pin (PA.01), The Rising edge is used as active edge,

The TIM2 CCR2 is used to compute the frequency value The TIM2 CCR1 is used to compute the duty cycle value ------------------------------------------------------------ */

TIM_ICInitStructure.TIM_ICMode = TIM_ICMode_PWMI; // TIM使用输入PWM模式 TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;

TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; // TIM输入捕获上升沿 TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //TIM输入2选择对应地与IIC2相连

TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //TIM捕获在捕获输入上每探测到一个边沿执行一次

TIM_ICInitStructure.TIM_ICFilter = 0x0;//选择输入比较滤波器

TIM_ICInit(TIM2, &TIM_ICInitStructure); //根据TIM_ICInitStruct中指定的参数初始化外设TIM2

/* Select the TIM2 Input Trigger: TI2FP2 */ TIM_SelectInputTrigger(TIM2, TIM_TS_TI2FP2); //选择TIM2输入触发源---TIM经滤波定时器输入2

/* Select the slave Mode: Reset Mode */

TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset);

//选择TIM2从模式----选中触发信号(TRGI)的上升沿初始化计数器并更新触发寄存器

/* Enable the Master/Slave Mode */

TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable);//TIM主/从模式使能

/* TIM enable counter */ TIM_Cmd(TIM2, ENABLE);

/* Enable the CC2 Interrupt Request */

TIM_ITConfig(TIM2, TIM_IT_CC2, ENABLE);

while (1); }

/******************************************************************************* * Function Name : RCC_Configuration

* Description : Configures the different system clocks. * Input : None * Output : None * Return : None

*******************************************************************************/ void RCC_Configuration(void) {

/* RCC system reset(for debug purpose) */ RCC_DeInit();

/* Enable HSE */

RCC_HSEConfig(RCC_HSE_ON);

/* Wait till HSE is ready */

HSEStartUpStatus = RCC_WaitForHSEStartUp();

if(HSEStartUpStatus == SUCCESS) {

/* Enable Prefetch Buffer */

FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

/* Flash 2 wait state */

FLASH_SetLatency(FLASH_Latency_2);

/* HCLK = SYSCLK */

RCC_HCLKConfig(RCC_SYSCLK_Div1);

/* PCLK2 = HCLK */

RCC_PCLK2Config(RCC_HCLK_Div1);

/* PCLK1 = HCLK/2 */

RCC_PCLK1Config(RCC_HCLK_Div2);

/* PLLCLK = 8MHz * 9 = 72 MHz */

RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

/* Enable PLL */


编码器学习STM32(3).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:县农业局XX年食品安全工作总结

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: