This week, I worked on the encoder module for the computer mouse. It took me a whole week.
The process was really funny. We planned to use the CH3 and CH4 channels of TIM3 to collect the PWM of the left motor to measure speed and distance, and use the CH2 and CH1 of TIM4 to measure the right motor.
When writing the code, we first worked on the left motor. It was not until Friday that we found that the CH3 and CH4 channels of the general timer could not be used for PWM input. What a mess! ! The following is one of our configurations, which I would like to share with you!
We use GPIO_B_7, the second channel of TIMER4
void MY_CONFIG(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
/* TIM4 clock source enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
/* Enable GPIOA, clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//TIM4 output initialization
TIM_DeInit(TIM4);
TIM_TimeBaseStructure.TIM_Period = 100; //Count value = 100-1
TIM_TimeBaseStructure.TIM_Prescaler = 0; //Pre-scaling
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //Clock division TIM_TimeBaseStructure.TIM_CounterMode
= TIM_CounterMode_Down; //Mode
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //Basic initialization
TIM_SetCounter(TIM4, 100); //Set TIM4 counter register value
//Set TIMx auto-reload register value
TIM_ARRPreloadConfig(TIM4, ENABLE);
TIM_SelectInputTrigger(TIM4, TIM_TS_TI2FP2); //If it is channel 1, it is TIM_TS_TI1FP1
//TIM_SelectInputTrigger(TIM2, TIM_TS_TI1F_ED);
TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_External1);
//TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_Reset);
TIM_SetCompare2(TIM4, 100); //If one channel is used, then: TIM_SetComper1(TIM4,100);
TIM_Cmd(TIM4, ENABLE); //Start TIM4
TIM_ClearFlag(TIM4, TIM_IT_CC2); //If one channel is used, then here and the following TIM_IT_CC2 are changed to TIM_IT_CC1
TIM_ITConfig(TIM4, TIM_IT_CC2, ENABLE)
//Interrupt configuration
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQChannel;; //
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; //Preemption priority 0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //Response priority 0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //Enable interrupt
NVIC_Init(&NVIC_InitStructure); //Write settings
}
void TIM4_IRQHandler(void)
{
TIM_ClearFlag(TIM4, TIM_IT_CC2);
}
It's sad, I have to redraw the board. But I learned a lot about the general timer of stm32, keep it up
Previous article:STM32 Quadrature Encoder Interface
Next article:STM32 timer encoder mode
Recommended ReadingLatest update time:2024-11-23 15:13
- Popular Resources
- Popular amplifiers
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- STMicroelectronics discloses its 2027-2028 financial model and path to achieve its 2030 goals
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs
- [Non-contact automatic disinfection system] Material unpacking - a little surprise
- What microcontroller can be connected to the network via optical fiber using the CAN bus?
- >>See here "New Trend Report: How to Effectively Respond to Current Challenges in the Field of Test and Measurement" and download to win prizes!
- itop4412 development board-QtE4.7-UVC camera usage examples
- Basic knowledge of electronics
- Some problems with nested make
- TIOBE Index October 2022
- Transfer: Analysis of key technologies of embedded systems
- 【GD32450I-EVAL】LittleVGL display part transplantation
- Loss Analysis of MOSFET in Flyback Critical Operation Mode