1. Encoder mode theoretical reserve
Usually in order to improve accuracy we will choose to count both the rising and falling edges!
There is also a very important picture recorded here
The most confusing part is probably the relative signal level in the second column, so let's discuss this in detail.
In fact, it is not difficult to understand. As we said above, in order to improve accuracy, we usually count both the rising and falling edges of phases A and B. Then, we can count four times in one cycle. The increase in the number of counts means an increase in accuracy!
In encoder mode, if it is in forward rotation, then these four counts should all be added. Similarly, if it is in reverse rotation, then these four counts should all be subtracted. So the question is, how to judge forward and reverse rotation?
Isn't it just based on the relative level!!!
By carefully comparing the diagram, you can clearly understand the level of A relative to B and the counting direction in the table above in the case of forward or reverse rotation!!!
2. STM32 actual code
By the way, it must be pointed out that the encoder mode can only correspond to one timer's CH1/CH2 channel, which means it just connects to phase A and B! Perfect!!!
/*TIM2 initialized as encoder interface*/
void Encoder_Init_TIM2(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //Enable the clock of timer 4
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //Enable PA port clock
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1; //Port configuration
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //Floating input
GPIO_Init(GPIOA, &GPIO_InitStructure); //Initialize GPIOA according to the set parameters
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Prescaler = 0x0; // Prescaler
TIM_TimeBaseStructure.TIM_Period = ENCODER_TIM_PERIOD; //Set the counter to automatically reload value
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //Select clock division: no division
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; ////TIM counts up
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_EncoderInterfaceConfig(TIM2, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising); //Use encoder mode 3
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_ICFilter = 10;
TIM_ICInit(TIM2, &TIM_ICInitStructure);
TIM_ClearFlag(TIM2, TIM_FLAG_Update); //Clear the update flag of TIM
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
//Reset counter
TIM_SetCounter(TIM2,0);
TIM_Cmd(TIM2, ENABLE);
}
/*TIM4 initialized as encoder interface*/
void Encoder_Init_TIM4(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //Enable the clock of timer 4
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //Enable PB port clock
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7; //Port configuration
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //Floating input
GPIO_Init(GPIOB, &GPIO_InitStructure); //Initialize GPIOB according to the set parameters
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Prescaler = 0x0; // Prescaler
TIM_TimeBaseStructure.TIM_Period = ENCODER_TIM_PERIOD; //Set the counter to automatically reload value
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //Select clock division: no division
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; ////TIM counts up
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
TIM_EncoderInterfaceConfig(TIM4, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising); //Use encoder mode 3
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_ICFilter = 10;
TIM_ICInit(TIM4, &TIM_ICInitStructure);
TIM_ClearFlag(TIM4, TIM_FLAG_Update); //Clear the update flag of TIM
TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);
//Reset counter
TIM_SetCounter(TIM4,0);
TIM_Cmd(TIM4, ENABLE);
}
/*Unit time encoder count input timer output speed value*/
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;
}
void TIM4_IRQHandler(void)
{
if(TIM4->SR&0X0001)//overflow interrupt
{
}
TIM4->SR&=~(1<<0); //Clear interrupt flag
}
void TIM2_IRQHandler(void)
{
if(TIM2->SR&0X0001)//overflow interrupt
{
}
TIM2->SR&=~(1<<0); //Clear interrupt flag
}
Previous article:STM32 ADC gets battery voltage
Next article:STM32 printf usage
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Slope compensation issues
- Share the OS transplantation and application of Lingdong Micro MCU based on MM32 MCU-AMetal SPI operation
- Do you still buy electronic components offline? It’s Double 11, what goodies are in your shopping cart?
- 【TI Recommended Course】#Amplifier Protection Series#
- 01 First look at the development board
- Battery and System Health Monitoring Reference Design for Battery-Powered Smart Flow Meters
- Who has used the voltage-to-PWM chip GP9101?
- Analog Multiplexer and Switch Configuration
- Build a "core" building together: front-line engineers talk about national chips, valid all year round (11 floors have been built)
- Principle of MCU reset