標(biāo)題: STM32F429IGT6正交編碼器輸入源程序 [打印本頁(yè)]

作者: ms53zs    時(shí)間: 2019-8-3 00:19
標(biāo)題: STM32F429IGT6正交編碼器輸入源程序
關(guān)于STM32的正交編碼找了很久,網(wǎng)上許多錯(cuò)誤的代碼,花了大概一天時(shí)間,弄懂了怎么寫這個(gè)程序,發(fā)現(xiàn)其中有許多引腳不可以作為正交的輸入引腳(可能存在引腳復(fù)用之類的情況),做了一些不完全的測(cè)試(未測(cè)試一些未引出引腳以及JTAG引腳),具體可用引腳為:
定時(shí)器:       TIM2        TIM5        TIM3        
通道1:        PA5/PA15    PA0         PA6/PB4     
通道2:        PA1/PB3     PA1         PA7         
測(cè)試平臺(tái)為STM32F429IGT6,野火最小系統(tǒng)板。具體程序見(jiàn)附件。

單片機(jī)源程序如下:
  1. #include "stm32f4xx.h"
  2. #include "./led/bsp_led.h"
  3. #include "./timing/bsp_basic_tim.h"
  4. #include "./PWMencode/bsp_pwmencode_tim.h"
  5. #include "./pwm/bsp_pwmoutput_tim.h"
  6. #include "./oled/bsp_oled.h"
  7. #include "./usart/bsp_usart.h"
  8. #include "./adc/bsp_adc.h"

  9. uint32_t Task_Delay[NumOfTask]={0};

  10. __IO int16_t ADC_ConvertedValue;

  11. #define OLED__

  12. int main(void)
  13. {
  14.         LED_GPIO_Init();
  15.         
  16.         #ifdef        OLED__
  17.         OLED_Init();
  18.         USARTx_Config();
  19.         #else        /*OLED__*/
  20.         USARTx_Config();
  21.         #endif        /*OLED__*/
  22.         
  23.         Rheostat_Init();
  24.         Timing_Config();
  25.         PWMencode_Config();
  26.         PWM_Output_Init();
  27.         while(1);
  28. }
復(fù)制代碼
  1. #include "./PWMencode/bsp_PWMencode_tim.h"
  2. #include "stm32f4xx_tim.h"
  3. #include "./led/bsp_led.h"

  4. int circle_count = 0;

  5. //void ADVANCE_Time_IRQ(void)
  6. //{
  7. //        GPIO_ToggleBits(LED_R_GPIO_PORT, LED_R_GPIO_PIN);
  8. //        if((GENERAL_TIMx->CR1>>4 & 0x01)==0) //DIR==0
  9. //                circle_count++;
  10. //        else if((GENERAL_TIMx->CR1>>4 & 0x01)==1)//DIR==1
  11. //                circle_count--;
  12. //}

  13. short Get_Encode(void)
  14. {
  15.         short Count;
  16.         
  17.         Count = GENERAL_TIMx->CNT;
  18.         TIM_SetCounter(GENERAL_TIMx, 0);
  19.         
  20.         return Count;
  21. }

  22. void GPIO_PWMencode_Init(void)
  23. {
  24.         GPIO_InitTypeDef GPIO_InitStructure;
  25.         RCC_AHB1PeriphClockCmd(GENERAL_ICPWM1_GPIO_CLK | GENERAL_ICPWM2_GPIO_CLK, ENABLE);//使能引腳的時(shí)鐘
  26.         
  27.         /* GPIO初始化 */
  28.         GPIO_InitStructure.GPIO_OType                = GPIO_OType_PP;
  29.         GPIO_InitStructure.GPIO_PuPd                = GPIO_PuPd_NOPULL;
  30.         GPIO_InitStructure.GPIO_Speed                = GPIO_Speed_100MHz;
  31.         GPIO_InitStructure.GPIO_Mode                = GPIO_Mode_AF;
  32.         
  33.         GPIO_InitStructure.GPIO_Pin                        = GENERAL_ICPWM1_PIN;  
  34.         GPIO_Init(GPIOA , &GPIO_InitStructure);
  35.         GPIO_InitStructure.GPIO_Pin                        = GENERAL_ICPWM2_PIN;  
  36.         GPIO_Init(GPIOA , &GPIO_InitStructure);
  37.         /*  連接到 GPIO_AF_TIM2*/
  38.         GPIO_PinAFConfig(GENERAL_ICPWM1_GPIO_PORT , GENERAL_ICPWM1_PINSOURCE , GENERAL_ICPWM_GPIO_AF);
  39.         GPIO_PinAFConfig(GENERAL_ICPWM2_GPIO_PORT , GENERAL_ICPWM2_PINSOURCE , GENERAL_ICPWM_GPIO_AF);
  40. }

  41. //void NVIC_PWMencode_Config(void)
  42. //{
  43. //        NVIC_InitTypeDef NVIC_InitStructure;
  44. //        NVIC_InitStructure.NVIC_IRQChannel                                                = GENERAL_TIMx_IRQn;
  45. //        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority        = 0x01;
  46. //        NVIC_InitStructure.NVIC_IRQChannelSubPriority                        = 0x01;
  47. //        NVIC_InitStructure.NVIC_IRQChannelCmd                                        = ENABLE;
  48. //        NVIC_Init(&NVIC_InitStructure);
  49. //}

  50. void TIMx_PWMencode_Config(void)
  51. {
  52.         TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  53.         TIM_ICInitTypeDef                TIM_ICInitStructure;
  54.         RCC_GENERAL_TIMx_CONFIG(GENERAL_TIMx_CLK, ENABLE);//使能定時(shí)器2的時(shí)鐘
  55.         
  56.         TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
  57.         TIM_TimeBaseStructure.TIM_Prescaler = 0x0;              // 預(yù)分頻器
  58.         TIM_TimeBaseStructure.TIM_Period = 65535;             //設(shè)定計(jì)數(shù)器自動(dòng)重裝值
  59.         TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;    //選擇時(shí)鐘分頻:不分頻
  60.         TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIM向上計(jì)數(shù)  
  61.         TIM_TimeBaseInit(GENERAL_TIMx, &TIM_TimeBaseStructure);
  62.         TIM_EncoderInterfaceConfig(GENERAL_TIMx, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);//使用編碼器模式3,上升沿捕捉
  63.         TIM_ICStructInit(&TIM_ICInitStructure);
  64.         TIM_ICInitStructure.TIM_ICFilter = 10;        //配置輸入濾波器
  65.         TIM_ICInit(GENERAL_TIMx, &TIM_ICInitStructure);
  66.         TIM_SetCounter(GENERAL_TIMx,0);
  67. //        TIM_ClearFlag(GENERAL_TIMx, TIM_IT_Update);
  68. //        TIM_ITConfig(GENERAL_TIMx, TIM_IT_Update, ENABLE);
  69.         TIM_Cmd(GENERAL_TIMx, ENABLE);
  70.         
  71. }

  72. void PWMencode_Config(void)
  73. {
  74. //        NVIC_PWMencode_Config();
  75.         GPIO_PWMencode_Init();
  76.         TIMx_PWMencode_Config();
  77. }
復(fù)制代碼

所有資料51hei提供下載:
正交編碼.7z (417.64 KB, 下載次數(shù): 35)


作者: ferry21    時(shí)間: 2019-12-4 09:43
請(qǐng)教一下樓主 怎么計(jì)數(shù)編碼器圈數(shù)




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1