找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1916|回复: 1
收起左侧

STM32F429IGT6正交编码器输入源程序

[复制链接]
ID:385196 发表于 2019-8-3 00:19 | 显示全部楼层 |阅读模式
关于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)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:98823 发表于 2019-12-4 09:43 | 显示全部楼层
请教一下楼主 怎么计数编码器圈数
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表