1.编码器接口简介
- Encoder Interface 编码器接口
- 编码器接口可接收增量(正交)编码器的信号,根据编码器旋转产生的正交信号脉冲,自动控制CNT自增或自减,从而指示编码器的位置、旋转方向和旋转速度
- 每个高级定时器和通用定时器都拥有1个编码器接口
- 两个输入引脚借用了输入捕获的通道1和通道2(CH1和CH2引脚)
2.编码器接口基本结构
2.1 工作模式
2.2 编码器接口基本结构
初始化步骤
第一步,RCC开启时钟,开启GPIO和定时器的时钟。
第二步,配置GPIO,6-8中把PA6和PA7配置为输入模式。
第三步,配置时基单元,这里的预分频器一般选择不分配,自动重装,一般给最大65535,只需要个CNT执行计数就行了。
第四步,配置输入捕获单元,这里输入捕获单元只有滤波器和极性两个参数有用。
第五步,配置编码器接口模式,调用库函数。
最后,调用TIM_Cmd,启动定时器。
3.实操
3.1 库函数
TIM_ICInitTypeDef TIM_ICInitStructure; //定义结构体变量 TIM_ICStructInit(&TIM_ICInitStructure); //给结构体赋一个初始值 TIM_ICInitStructure.TIM_Channel = TIM_Channel_1; TIM_ICInitStructure.TIM_ICFilter = 0xF; //TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; TIM_ICInit(TIM3, &TIM_ICInitStructure); //调用ICInit配置电路,写入了硬件,之后换个值继续写入 TIM_ICInitStructure.TIM_Channel = TIM_Channel_2; TIM_ICInitStructure.TIM_ICFilter = 0xF; //TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; TIM_ICInit(TIM3, &TIM_ICInitStructure); TIM_EncoderInterfaceConfig(TIM3, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);//第三 第四与上面相同
//定时器编码器接口配置 void TIM_EncoderInterfaceConfig(TIM_TypeDef* TIMx, uint16_t TIM_EncoderMode,uint16_t TIM_IC1Polarity, uint16_t TIM_IC2Polarity);
3.2 编码器接口测速
3.2.1 实验现象
<iframe frameborder="no" height="240" scrolling="no" src="https://player.bilibili.com/player.html?cid=1349432829&aid=876555884&page=1&as_wide=1&high_quality=1&danmaku=0" style="position: absolute; width: 100%; height: 100%; left: 0; top: 0" width="320"></iframe>
3.2.2 代码
Encoder.c
#include "stm32f10x.h" // Device header void Encoder_Init(void) { RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInitStructure.TIM_Period = 65536 - 1; //ARR TIM_TimeBaseInitStructure.TIM_Prescaler = 1 - 1; //PSC TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0; TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStructure); TIM_ICInitTypeDef TIM_ICInitStructure;//配置输入捕获单元 TIM_ICStructInit(&TIM_ICInitStructure); TIM_ICInitStructure.TIM_Channel = TIM_Channel_1; TIM_ICInitStructure.TIM_ICFilter = 0xF; TIM_ICInit(TIM3, &TIM_ICInitStructure); TIM_ICInitStructure.TIM_Channel = TIM_Channel_2; TIM_ICInitStructure.TIM_ICFilter = 0xF; TIM_ICInit(TIM3, &TIM_ICInitStructure); TIM_EncoderInterfaceConfig(TIM3, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);//配置编码器接口模式 TIM_Cmd(TIM3, ENABLE); } int16_t Encoder_Get(void) { int16_t Temp; Temp = TIM_GetCounter(TIM3);//获取当前计数器的值 TIM_SetCounter(TIM3, 0);//给计数器写入一个值 return Temp; }
main.c
#include "stm32f10x.h" // Device header #include "Delay.h" #include "OLED.h" #include "Timer.h" #include "Encoder.h" int16_t Speed; int main(void) { OLED_Init(); Timer_Init(); Encoder_Init(); OLED_ShowString(1, 1, "Speed:"); while (1) { OLED_ShowSignedNum(1, 7, Speed, 5); } } void TIM2_IRQHandler(void) { if (TIM_GetITStatus(TIM2, TIM_IT_Update) == SET) { Speed = Encoder_Get(); TIM_ClearITPendingBit(TIM2, TIM_IT_Update); } }
Timer.c
#include "stm32f10x.h" // Device header void Timer_Init(void) { RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); TIM_InternalClockConfig(TIM2); TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInitStructure.TIM_Period = 10000 - 1; TIM_TimeBaseInitStructure.TIM_Prescaler = 7200 - 1; TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStructure); TIM_ClearFlag(TIM2, TIM_FLAG_Update); TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); NVIC_InitTypeDef NVIC_InitStructure; NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_Init(&NVIC_InitStructure); TIM_Cmd(TIM2, ENABLE); } /* void TIM2_IRQHandler(void) { if (TIM_GetITStatus(TIM2, TIM_IT_Update) == SET) { TIM_ClearITPendingBit(TIM2, TIM_IT_Update); } } */标签:TimeBaseInitStructure,编码器,定时器,大S,NVIC,TIM,GPIO,ICInitStructure From: https://www.cnblogs.com/xsgcumt/p/17868162.html