Output comparison: Turn on a TIMx counter, and then turn on one or several output comparators of TIMx (4 in total). After all are configured, the counter starts counting. When the value in the counter is equal to the value in the comparison register, an output comparison interrupt is generated. In the interrupt, the value in the counter is read out, added to the rollover period, and written to the comparison register, so that it has the same rollover period as the next event.
The general meaning is that after the counter is turned on, the count value continues to increase. When it increases to the value of the comparison register, the level flips and a comparison interrupt is generated. In the comparison interrupt, the counter value is read out and added to the period, and written into the comparison register together, so that they have the same flip period.
For example, if the TIM clock frequency is set to 12 MHZ, and the self-load value in the output compare register is 600 (high level or low level count value), the output PWM frequency is
frequency = 12MHZ/(600*2)=10KHZ.
But the initial value can be set by yourself, in other words, the phase can be determined by yourself.
1. TIM3 clock and corresponding IO clock are enabled.
TIM3 is mounted under APB1, so we enable TIM3 through the enable function under APB1 bus. The called function is:
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); // Enable timer 3 mounted under APB1 clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); // Enable channel 1 PB1 of timer 3 and enable PB clock
2. GPIO structure initialization Because it is a timer output, it is a multiplexed push-pull output AF-PP
GPIO_InitTypeDef GPIO_InitStructure;
//Step1. Set PB.1 port as OC4 output port of TIM3
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
3. Set the timer mode to output compare mode
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_TimeBaseStructure.TIM_Prescaler = 71; //The frequency division coefficient is 71, so the actual time base unit is 72M/(71+1)=1M
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //Count up
TIM_TimeBaseStructure.TIM_Period = 0xffff; //Self-loaded value is 65535 special value
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
TIM_OCStructInit(&TIM_OCInitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle; //Trigger event level flip
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; // Enable output
TIM_OCInitStructure.TIM_Pulse = t; //initial value 10000
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //Output polarity is high
TIM_OC4Init(TIM3,&TIM_OCInitStructure); //Channel 4
4. Because you want to enable the timer interrupt OC and set the interrupt priority
NVIC_InitTypeDef NVIC_InitStructure;
//Step 3. Enable TIM3 output compare match interrupt
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
5. Clear the interrupt flag and enable the interrupt to enable timer 3
TIM_ClearFlag(TIM3, TIM_FLAG_CC4); // Clear interrupt flag
TIM_ITConfig(TIM3, TIM_IT_CC4, ENABLE); //TIM3 interrupt source setting, enable capture comparison interrupt of corresponding channel
TIM_Cmd(TIM3, ENABLE); //First disable timer 3
6. Interrupt service function: void TIM3_IRQHandler(void)
7. First check whether the output comparison interrupt response is set
if(TIM_GetITStatus(TIM3,TIM_IT_CC4) == SET)
8. Clear interrupt flag
TIM_ClearITPendingBit(TIM3, TIM_IT_CC4 );
9. Extract the current count value of Timer_CNT
capture = TIM_GetCapture4(TIM3); //Extract the current count value of Timer3
10. Reassign CCR. Next timer interrupt time.
TIM_SetCompare4(TIM3, capture + t); //need to accumulate the current count value
Frequency output calculation method: PCLK2/(TIM_Prescaler+1)/(CCR)/2=f
Assuming CCR is 1000 72M/(71+1)/1000/2=500HZ
TIM_SetCompare4(TIM3, capture + t); Changing t means changing CCR.
Previous article:8 basic types of IO settings for stm32
Next article:STM32 standby wake-up operation
Recommended ReadingLatest update time:2024-11-16 16:40
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
- What should I do if MP1584EN often burns the chip?
- Ultrasonic water and gas meter design challenges and solutions
- Increase the frequency range of the RC voltage-controlled oscillator
- How do you guys choose whether to use a 4-layer board or a 2-layer board?
- Thank you for being there + thank you for your wife's protection
- The electrical engineer blew up the company because of a resignation dispute...
- Free sharing of analog high-definition AHD and TVI and CVI to HDMI and VGA and CVBS solution schematics
- Sharing joy with others is worse than enjoying it alone——TI training’s “recommended courses” function is now available for public testing. We invite you to make suggestions and participate in the competition!
- [Anxinke NB-IoT Development Board EC-01F-Kit] 1. Unboxing and Hardware Appreciation
- Seeking advice from hardware experts: PWM control transistor buck-boost output waveform