Here, channel 1 and channel 2 of TIM3 are used as level capture of AB items.
Since STM32 has its own hardware encoder interface, it can be used as long as it is set up. It is very powerful. The following program has passed the test.
void TIM3_Mode_Config(void)
{
//u16 CCR1_Val = 2500;
//u16 CCR2_Val = 1000;
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
//TIM_OCInitTypeDef TIM_OCInitStructure;
/*------------------------------------------------ ----------------*/
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);
/*------------------------------------------------ ----------------*/
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //Enable TIM3
TIM_DeInit(TIM3);
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Period =0xffff; //
TIM_TimeBaseStructure.TIM_Prescaler =0; //Set prescaler:
TIM_TimeBaseStructure.TIM_ClockDivision =TIM_CKD_DIV1; //Set the clock division factor: no division
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //Upward counting mode
//TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned1;
/*Initialize TIM2 timer*/
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
/*------------------------------------------------ ------------------*/
//Encoding configuration encoding mode
TIM_EncoderInterfaceConfig(TIM3, TIM_EncoderMode_TI12,
TIM_ICPolarity_Rising, TIM_ICPolarity_Rising); //TIM_ICPolarity_Rising rising edge capture
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_ICFilter = 6; //Comparison filter
TIM_ICInit(TIM3, &TIM_ICInitStructure);
//TIM_ARRPreloadConfig(TIM3, ENABLE);
// Clear all pending interrupts
TIM_ClearFlag(TIM3, TIM_FLAG_Update);
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE); //Enable interrupt
//Reset counter
TIM3->CNT =0;
TIM_Cmd(TIM3, ENABLE); //Enable timer 3
}
void TIM_Init(void)
{
TIM3_Mode_Config();
}
In the main program, send periodically through the serial port.
int main(void){
SystemInit(); // 72m clock
SysTick_Init();
TIM_Init();
NVIC_Config();
GPIO_74HC595_Config();
while (1)
{
encoder_num=TIM_GetCounter(TIM3);
// dis_595(encoder_num,encoder_num);
}
}
Previous article:STM32 orthogonal signal processing of code disk
Next article:Encoder configuration of stm32
Recommended ReadingLatest update time:2024-11-16 14:49
- 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
- 500 yuan for infrared counting microcontroller
- Why can the serial port receive 9600 but lose bytes at 115200?
- What is the chip with 4BMN silk screen? It has 5 pins.
- I encountered a problem when testing the CAN communication isolation chip a few days ago. I hope you can give me some advice.
- Reminiscing about the past! A brief discussion on the century-long history of radio development
- Network port debugging issues
- I bought a few gold-sealed transistors on Taobao. The silk screen can be wiped off with bare hands.
- Inventory of diodes and eight major circuit protection components
- The difference between a two-pipe wired controller and a four-pipe thermostat
- Build a laser tripwire alarm using the MSP430 LaunchPad