As shown in the figure, each TIMER of STM32 has an orthogonal encoder input interface. TI1 and TI2 are input filtered, and edge detection generates TI1FP1. TI2FP2 is connected to the encoder module. By configuring the working mode of the encoder, the encoder can be counted forward/reverse.
As shown in the figure below, the encoder uses two-phase signals A and B, but I only need to count the TI1 signal (the first line). I just discovered this error. It turns out that counting both signals results in more than 100 pulses (100-line photoelectric encoder) in one rotation of the encoder disk. By comparing the two level signals through the STM32 encoder module, it is easy to calculate the operation of the encoder.
The following is my debugged OK code:
void Encoder_Configration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
//PC6 A phase PC7 B phase
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC,&GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Prescaler = 0x0; // No prescaling
TIM_TimeBaseStructure.TIM_Period = 10000;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure);
TIM_EncoderInterfaceConfig(TIM8, TIM_EncoderMode_TI12,
TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_ICFilter = 6;//ICx_FILTER;
TIM_ICInit(TIM8, &TIM_ICInitStructure);
// Clear all pending interrupts
TIM_ClearFlag(TIM8, TIM_FLAG_Update);
TIM_ITConfig(TIM8, TIM_IT_Update, ENABLE);
//Reset counter
TIM2->CNT = 0;
TIM_Cmd(TIM8, ENABLE);
}
n_Counter = TIM_GetCounter(TIM8);
Diled_Disp_Num((float)n_Counter);
Another point worth noting is that the STM32 timer is 16 bits, which means it can only count to 65535. There are two ways to do this. One is to use two timers in a chained manner to expand 16 bits to 32 bits. Another simple method is to enable the overflow interrupt of the timer. Each interrupt represents that the encoder has run a specific angle.
For example, if the encoder has 400 lines, set the ARR register to 400. Each overflow interrupt represents that the motor has turned a circle, and so on.
In addition, the detection of input pulses is actually similar, except that an external trigger module is used inside the STM32 to implement it. As shown in the purple box in Figure 1, the encoder module should be an upgrade of this module. The following is the configuration code:
void TIM3_ETR_GetDropCounts_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
//test PA0 TIM8_ETR
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Prescaler = 0x00;
TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); // Time base configuration
TIM_ETRClockMode2Config(TIM3, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 0);
TIM_SetCounter(TIM3, 0);
TIM_Cmd(TIM3, ENABLE);
}
Previous article:STM32——JLINK downloader flash firmware
Next article:Problems encountered when porting cjson to stm32
Recommended ReadingLatest update time:2024-11-16 14:48
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Setting trial time limit
- Migrate from M2M to IIoT
- Brushless drive solution for circuit board
- 【Showing goods】The second wave
- EEWORLD University ---- HVI Series: Mastering the Art and Fundamentals of High Voltage Gate Driver Design
- How to enable TMU of 28377d.
- "【TGF4042 Signal Generator】" Potential Compensation Test
- MCU Programming_Interrupt
- Using TI power timing controller in 5G MIMO application
- MCU development full-time/part-time and sales assistant