欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136

標題: STM32F429IGT6正交編碼器輸入源程序 [打印本頁]

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

單片機源程序如下:
  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. }
復制代碼
  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);//使能引腳的時鐘
  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);//使能定時器2的時鐘
  55.         
  56.         TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
  57.         TIM_TimeBaseStructure.TIM_Prescaler = 0x0;              // 預分頻器
  58.         TIM_TimeBaseStructure.TIM_Period = 65535;             //設定計數器自動重裝值
  59.         TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;    //選擇時鐘分頻:不分頻
  60.         TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIM向上計數  
  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. }
復制代碼

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


作者: ferry21    時間: 2019-12-4 09:43
請教一下樓主 怎么計數編碼器圈數




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