最近的学校电赛初赛有一个自控小车的题目,其中的编码器读取转速还是费了我一些时间学习的,这里把编码器的初始化等代码以及main函数的测试代码加了一些注释放出来作为参考。
#include "encoder.h" void encoder_left_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_ICInitTypeDef TIM_ICInitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);//开启TIM4时钟 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD,ENABLE);//开启GPIOB时钟 GPIO_PinAFConfig(GPIOD,GPIO_PinSource12,GPIO_AF_TIM4);//D12引脚复用 GPIO_PinAFConfig(GPIOD,GPIO_PinSource13,GPIO_AF_TIM4);//D13引脚服用 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12|GPIO_Pin_13; //GPIOD12,GPIOD13 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ; GPIO_Init(GPIOD,&GPIO_InitStructure); TIM_TimeBaseStructure.TIM_Period = 65535; //设置下一个更新事件装入活动的自动重装载寄存器周期的值 TIM_TimeBaseStructure.TIM_Prescaler = 0; //设置用来作为TIMx时钟频率除数的预分频值 不分频 TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式 TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //设置定时器为编码器模式 IT1 IT2为上升沿和下降沿都计数四倍频 TIM_EncoderInterfaceConfig(TIM4, TIM_EncoderMode_TI12,TIM_ICPolarity_BothEdge,TIM_ICPolarity_BothEdge); TIM_ICStructInit(&TIM_ICInitStructure); TIM_ICInitStructure.TIM_ICFilter = 0; //输入滤波器 TIM_ICInit(TIM4, &TIM_ICInitStructure); TIM_ClearFlag(TIM4, TIM_FLAG_Update); //清楚所有标志位 TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE); //允许中断更新 TIM4->CNT = 0; TIM_Cmd(TIM4, ENABLE); //使能TIM4 } void encoder_right_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_ICInitTypeDef TIM_ICInitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);//开启TIM3时钟 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE);//开启GPIOC时钟 GPIO_PinAFConfig(GPIOC,GPIO_PinSource6,GPIO_AF_TIM4);//PC6引脚复用 GPIO_PinAFConfig(GPIOC,GPIO_PinSource7,GPIO_AF_TIM4);//PC7引脚服用 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7; //GPIOC6,GPIOC7 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ; GPIO_Init(GPIOC,&GPIO_InitStructure); TIM_TimeBaseStructure.TIM_Period = 65535; //设置下一个更新事件装入活动的自动重装载寄存器周期的值 TIM_TimeBaseStructure.TIM_Prescaler = 0; //设置用来作为TIMx时钟频率除数的预分频值 不分频 TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式 TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //设置定时器为编码器模式 IT1 IT2为上升沿和下降沿都计数四倍频 TIM_EncoderInterfaceConfig(TIM3, TIM_EncoderMode_TI12,TIM_ICPolarity_BothEdge,TIM_ICPolarity_BothEdge); TIM_ICStructInit(&TIM_ICInitStructure); TIM_ICInitStructure.TIM_ICFilter = 0; //输入滤波器 TIM_ICInit(TIM3, &TIM_ICInitStructure); TIM_ClearFlag(TIM3, TIM_FLAG_Update); //清楚所有标志位 TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE); //允许中断更新 TIM4->CNT = 0; TIM_Cmd(TIM3, ENABLE); //使能TIM3 } void TIM4_IRQHandler(void) { if(TIM4->SR&0X0001)//溢出中断 { } TIM4->SR&=~(1<<0);//清除中断标志位 } void TIM3_IRQHandler(void) { if(TIM3->SR&0X0001)//溢出中断 { } TIM3->SR&=~(1<<0);//清除中断标志位 } //参数说明:左边:3,右边:4 int Read_Encoder(u8 TIMX) { int Encoder_TIM; switch(TIMX) { case 2: Encoder_TIM= (short)TIM2 -> CNT; TIM2 -> CNT=0;break; case 3: Encoder_TIM= (short)TIM3 -> CNT; TIM3 -> CNT=0;break; case 4: Encoder_TIM= (short)TIM4 -> CNT; TIM4 -> CNT=0;break; default: Encoder_TIM=0; } return Encoder_TIM; }
#include "sys.h" #include "usart.h" #include "delay.h" #include "led.h" #include "encoder.h" #include "lcd.h" int pwm(int percentage,GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin); int main(void) { int encoder_left=0;//编码器数值存储 int encoder_right=0;//编码器数值存储 char printstr[30]={0};//屏幕字符串输出暂存 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设定中断分组 delay_init(168);//延迟初始化 uart_init(115200);//串口初始化 LCD_Init(); //LCD屏幕初始化 POINT_COLOR=BLUE; //设定画笔颜色为蓝色 LCD_Clear(WHITE);//屏幕背景设为蓝色 encoder_left_Init();//左侧编码器初始化 encoder_right_Init();//右侧编码器初始化 while(1) { encoder_left=Read_Encoder(3);//读取左侧编码器数值 encoder_right=Read_Encoder(4);//读取右侧编码器数值 sprintf(printstr,"Left encoder value is:%d",encoder_left);//生成输出字符串 LCD_ShowString(30,50,200,16,16,printstr);//屏幕数据显示 sprintf(printstr,"Right encoder value is:%d",encoder_right);//同上 LCD_ShowString(30,70,200,16,16,printstr); delay_ms(100);//延时0.1秒 LCD_Clear(WHITE);//清屏 } }
发表评论