STM32 光电编码器

来源:互联网 发布:美图秀秀没有mac版吗 编辑:程序博客网 时间:2024/04/29 10:45

在这里使用TIM3的通道1和通道2做为A B项的电平捕捉。

由于STM32自带硬件编码器接口,因此只要设置好就可使用,非常强大,下面程序测试通过。

下载地址: http://download.csdn.net/detail/hongkangwl/6815327

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <pre code_snippet_id="144534" snippet_file_name="blog_20140106_1_4739860" name="code" class="cpp">void TIM3_Mode_Config(void)  
  2. {  
  3.     //u16 CCR1_Val = 2500;  
  4.     //u16 CCR2_Val = 1000;  
  5.     GPIO_InitTypeDef GPIO_InitStructure;  
  6.   TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;  
  7.     TIM_ICInitTypeDef TIM_ICInitStructure;  
  8.     //TIM_OCInitTypeDef  TIM_OCInitStructure;  
  9.       
  10. /*----------------------------------------------------------------*/  
  11.       
  12.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);  
  13.     
  14.       
  15.   GPIO_StructInit(&GPIO_InitStructure);  
  16.   /* Configure PA.06,07 as encoder input */  
  17.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;  
  18.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;  
  19.   GPIO_Init(GPIOA, &GPIO_InitStructure);  
  20.       
  21. /*----------------------------------------------------------------*/      
  22.   
  23.       
  24.   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //使能TIM3  
  25.     TIM_DeInit(TIM3);  
  26.     TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);  
  27.       
  28.   TIM_TimeBaseStructure.TIM_Period =0xffff;       //  
  29.   TIM_TimeBaseStructure.TIM_Prescaler =0;       //设置预分频:  
  30.   TIM_TimeBaseStructure.TIM_ClockDivision =TIM_CKD_DIV1 ;   //设置时钟分频系数:不分频  
  31.   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //向上计数模式  
  32.   //TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned1;   
  33.     /*初始化TIM2定时器 */  
  34.   TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);  
  35.       
  36.     /*-----------------------------------------------------------------*/  
  37.     //编码配置                        编码模式  
  38.     TIM_EncoderInterfaceConfig(TIM3, TIM_EncoderMode_TI12,   
  39.                              TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);  //TIM_ICPolarity_Rising上升沿捕获  
  40.   TIM_ICStructInit(&TIM_ICInitStructure);  
  41.   TIM_ICInitStructure.TIM_ICFilter = 6;         //比较滤波器  
  42.   TIM_ICInit(TIM3, &TIM_ICInitStructure);  
  43.     
  44.     //TIM_ARRPreloadConfig(TIM3, ENABLE);  
  45.  // Clear all pending interrupts  
  46.   TIM_ClearFlag(TIM3, TIM_FLAG_Update);  
  47.   TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);   //使能中断  
  48.   //Reset counter  
  49.   TIM3->CNT =0;  
  50.       
  51.       
  52.     TIM_Cmd(TIM3, ENABLE);   //使能定时器3  
  53. }  
  54. void TIM_Init(void)  
  55. {  
  56.   TIM3_Mode_Config();  
  57. }</pre><br>  
  58. <p></p>  
  59. <pre></pre>  
  60. <br>  
  61. 在主程序中通过串口定时发送。  
  62. <p></p>  
  63. <p></p><pre code_snippet_id="144534" snippet_file_name="blog_20140106_2_2166349" name="code" class="cpp"><pre code_snippet_id="144534" snippet_file_name="blog_20140106_2_2166349" name="code" class="cpp">int main(void)  
  64. {  
  65.   SystemInit();// 72m时钟  
  66.   SysTick_Init();  
  67.     TIM_Init();  
  68.     NVIC_Config();  
  69.     GPIO_74HC595_Config();  
  70.   while (1)  
  71.   {  
  72.         encoder_num=TIM_GetCounter(TIM3);  
  73.         
  74.           
  75.     //  dis_595(encoder_num,encoder_num);  
  76.           
  77.   }  
  78. }</pre><br>  
  79. <p></p>  
  80. <pre></pre>  
  81. <br>  
  82. <br>  
  83. <p></p>  
  84.   
  85. </pre>  
0 0
原创粉丝点击