标题:
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,野火最小系统板。具体程序见附件。
单片机源程序如下:
#include "stm32f4xx.h"
#include "./led/bsp_led.h"
#include "./timing/bsp_basic_tim.h"
#include "./PWMencode/bsp_pwmencode_tim.h"
#include "./pwm/bsp_pwmoutput_tim.h"
#include "./oled/bsp_oled.h"
#include "./usart/bsp_usart.h"
#include "./adc/bsp_adc.h"
uint32_t Task_Delay[NumOfTask]={0};
__IO int16_t ADC_ConvertedValue;
#define OLED__
int main(void)
{
LED_GPIO_Init();
#ifdef OLED__
OLED_Init();
USARTx_Config();
#else /*OLED__*/
USARTx_Config();
#endif /*OLED__*/
Rheostat_Init();
Timing_Config();
PWMencode_Config();
PWM_Output_Init();
while(1);
}
复制代码
#include "./PWMencode/bsp_PWMencode_tim.h"
#include "stm32f4xx_tim.h"
#include "./led/bsp_led.h"
int circle_count = 0;
//void ADVANCE_Time_IRQ(void)
//{
// GPIO_ToggleBits(LED_R_GPIO_PORT, LED_R_GPIO_PIN);
// if((GENERAL_TIMx->CR1>>4 & 0x01)==0) //DIR==0
// circle_count++;
// else if((GENERAL_TIMx->CR1>>4 & 0x01)==1)//DIR==1
// circle_count--;
//}
short Get_Encode(void)
{
short Count;
Count = GENERAL_TIMx->CNT;
TIM_SetCounter(GENERAL_TIMx, 0);
return Count;
}
void GPIO_PWMencode_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(GENERAL_ICPWM1_GPIO_CLK | GENERAL_ICPWM2_GPIO_CLK, ENABLE);//使能引脚的时钟
/* GPIO初始化 */
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = GENERAL_ICPWM1_PIN;
GPIO_Init(GPIOA , &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GENERAL_ICPWM2_PIN;
GPIO_Init(GPIOA , &GPIO_InitStructure);
/* 连接到 GPIO_AF_TIM2*/
GPIO_PinAFConfig(GENERAL_ICPWM1_GPIO_PORT , GENERAL_ICPWM1_PINSOURCE , GENERAL_ICPWM_GPIO_AF);
GPIO_PinAFConfig(GENERAL_ICPWM2_GPIO_PORT , GENERAL_ICPWM2_PINSOURCE , GENERAL_ICPWM_GPIO_AF);
}
//void NVIC_PWMencode_Config(void)
//{
// NVIC_InitTypeDef NVIC_InitStructure;
// NVIC_InitStructure.NVIC_IRQChannel = GENERAL_TIMx_IRQn;
// NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x01;
// NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01;
// NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
// NVIC_Init(&NVIC_InitStructure);
//}
void TIMx_PWMencode_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
RCC_GENERAL_TIMx_CONFIG(GENERAL_TIMx_CLK, ENABLE);//使能定时器2的时钟
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Prescaler = 0x0; // 预分频器
TIM_TimeBaseStructure.TIM_Period = 65535; //设定计数器自动重装值
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //选择时钟分频:不分频
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数
TIM_TimeBaseInit(GENERAL_TIMx, &TIM_TimeBaseStructure);
TIM_EncoderInterfaceConfig(GENERAL_TIMx, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);//使用编码器模式3,上升沿捕捉
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_ICFilter = 10; //配置输入滤波器
TIM_ICInit(GENERAL_TIMx, &TIM_ICInitStructure);
TIM_SetCounter(GENERAL_TIMx,0);
// TIM_ClearFlag(GENERAL_TIMx, TIM_IT_Update);
// TIM_ITConfig(GENERAL_TIMx, TIM_IT_Update, ENABLE);
TIM_Cmd(GENERAL_TIMx, ENABLE);
}
void PWMencode_Config(void)
{
// NVIC_PWMencode_Config();
GPIO_PWMencode_Init();
TIMx_PWMencode_Config();
}
复制代码
所有资料51hei提供下载:
正交编码.7z
(417.64 KB, 下载次数: 35)
2019-8-3 01:28 上传
点击文件名下载附件
下载积分: 黑币 -5
作者:
ferry21
时间:
2019-12-4 09:43
请教一下楼主 怎么计数编码器圈数
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1