happy. I was very happy, but suddenly I found that the timer of stm32 is 16 bits, and the count of my grating ruler will exceed 65535, so I consulted several experts on the 21ic forum, and finally determined the solution. The
working process is to configure TIM3 to orthogonal encoder mode, and set a 10ms interrupt, read the count value every 10ms, and the premise of 10ms is that the counter does not overflow within 10ms (thanks to lxyppc of 21ic for this idea) )
The following is some code: (These codes are modified from ST official routines, but my project uses the V3 firmware library, and their routines seem to be 0.3, so some places have been changed)
The following is to initialize TIM3 to quadrature encoder mode
void ENC_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
/* Encoder unit connected to TIM3, 4X mode */
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* TIM3 clock source enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
/* Enable GPIOA, clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_StructInit(&GPIO_InitStructure);
/* Configure PA.06,07 as encoder input */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Enable the T IM3 Update Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = TIMx_PRE_EMPTION_PRIORITY;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = TIMx_SUB_PRIORITY;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* Timer configuration in Encoder mode */
TIM_DeInit(ENCODER_TIMER);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Prescaler = 0x0; // No prescaling
TIM _TimeBaseStructure.TIM_Period = ENCODER_TIM_PERIOD;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(ENCODER_TIMER, &TIM_TimeBaseStructure);
TIM_EncoderInterfaceConfig(EN CODER_TIMER, TIM_EncoderMode_TI12,
TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_ICFilter = ICx_FILTER;
TIM_ICInit(ENCODER_TIMER, &TIM_ICInitStructure);
// Clear all pending interrupts
TIM_ClearFlag(ENCODER_TIMER, TIM_FLAG_Update);
TIM_ITConfig(ENCODER_TIMER, TIM_IT_Update, ENABLE);
//Reset counter
TIM2->CNT = COUNTER_RESET;
// ENC_Clear_Speed_Buffer();
TIM_Cmd(ENCODER_TIMER, ENABLE);
}
The following is to obtain a count value, this algorithm comes from lxyppc, you can avoid the situation of exceeding 16 bits. For details, see the discussion at http://bbs.21ic.com/viewthread.php?tid=110623
s16 ENC_Get_Electrical_Angle(void)
{
static u16 lastCount = 0;
u16 curCount = ENCODER_TIMER->CNT;
s32 dAngle = curCount - lastCount;
if(dAngle >= MAX_COUNT){
dAngle -= ENCODER_TIM_PERIOD;
}else if(dAngle < -MAX_COUNT){
dAngle += ENCODER_TIM_PERIOD;
}
lastCount = curCount;
return (s16)dAngle;
}
The following is the system tick initialization and interrupt function
void TB_Init(void)
{
/* Setup SysTick Timer for 10 msec interrupts */
if (SysTick_Config(SystemFrequency / 100))
{
/* Capture error */
while (1);
}
}
void SysTick_Handler(void)
{
/*if (hTimebase_display_500us != 0)
{
hTimebase_display_500us --;
}
*/
if (hSpeedMeas_Timebase_500us !=0)
{
hSpeedMeas_Timebase_500us--;
}
else
{
hSpeedMeas_Timebase_500us = SPEED_SAMPLING_TIME;
CurrentCount += ENC_Get_Electrical_Angle();
//ENC_Calc_Average_Speed must be called ONLY every SPEED_MEAS_TIMEBASE ms
//ENC_Calc_Average_Speed();
}
}
The above code has passed the test, the firmware library version is: V3.1.2
ST official routines and Chinese documentation: http://www.stmicroelectronics.com.cn/stonline/mcu/images/STM32F10xxx_Encoder_AN%28CH%29.pdf
http://images.stmicroelectronics.com.cn/stonline/mcu/images/STM32F10xxx_Encoder_AN(CH).zip
Finally, I have to complain, why doesn’t ST make the timer 32-bit? Can it increase the cost by 1 cent?
Previous article:I/O driver based on AT91RM9200 (linux)
Next article:STM32 SysTick system clock super simple timer SysTick
Recommended ReadingLatest update time:2024-11-16 14:40
- Popular Resources
- Popular amplifiers
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
- There is a problem in using tlv5616, it's very strange
- Purgatory Legend-Battle of Doubler Circuits
- Bluetooth remote control serial port problem
- [Serial] [Starlight Lightning STM32F407 Development Board] Chapter 13 FLASH Data Storage Experiment
- DSP28335 serial port interruption problem
- How to make an echo on atmel/AVR microcontroller Atmega 128? Is there any relevant information?
- Download to get a gift | Collection of the best courseware: Tektronix Semiconductor Device Research Exchange and Keithley Test and Measurement Annual Seminar
- MSP430 Main Memory Bootloader Introduction
- I want to use POE to power some gadgets, but I don’t know what power chip to use?
- STM8L152 drives LCD experimental ultra-low power consumption code 7.2UA