I have been working on the past questions of the Blue Bridge Cup Embedded Development these days. In the mock test of the seventh final, there is a requirement that PA6 and PA7 generate two square waves with different phases and adjustable frequency. Later, I studied the Output comare mode of STM32.
Because I want to adjust the frequency, I choose the toggle mode to make the square wave out of phase. Just set it in the interrupt. First, the diagram in the manual
That's an old picture.
Let's take a look at the interrupt function in the OCToggle mode of the official StdLib3.5
void TIM1_BRK_TIM9_IRQHandler(void)
{
/* TIM9_CH1 toggling with frequency = 366.2 Hz */
if (TIM_GetITStatus(TIM9, TIM_IT_CC1) != RESET)
{
TIM_ClearITPendingBit(TIM9, TIM_IT_CC1 );
capture = TIM_GetCapture1(TIM9);
TIM_SetCompare1(TIM9, capture + CCR1Val );
}
/* TIM9_CH2 toggling with frequency = 732.4 Hz */
if (TIM_GetITStatus(TIM9, TIM_IT_CC2) != RESET)
{
TIM_ClearITPendingBit(TIM9, TIM_IT_CC2);
capture = TIM_GetCapture2(TIM9);
TIM_SetCompare2(TIM9, capture + CCR2Val);
}
}
OCToggle basically works like this:
If TIMx_CNT = TIM_CCR1, then the above interrupt will occur. In the interrupt, it will add the value of CCR1 to the CCR1Val value you set and assign it to CCR1. Each time an interrupt occurs, the level will be flipped (toggle)
Because the frequencies of the two channels are the same, the interruptions of the two channels should also occur close together.
So in order to maintain the phase difference between the two channels, when channel 1 is interrupted, add the corresponding value based on your CCR2Val value and assign it to CCR2. This is equivalent to delaying the timing of channel 2 for a while when channel 1 is in the middle.
Like this
void TIM3_IRQHandler(void)
{
if (TIM_GetITStatus(TIM3, TIM_IT_CC1) != RESET)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_CC1 );
capture = TIM_GetCapture1(TIM3);
TIM_SetCompare1(TIM3, capture + CCR1Val );
TIM_SetCompare2(TIM3, capture +200);
}
if (TIM_GetITStatus(TIM3, TIM_IT_CC2) != RESET)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);
capture = TIM_GetCapture2(TIM3);
TIM_SetCompare2(TIM3, capture + CCR2Val);
}
}
I set CCR1Val and CCR2Val to 1000, and after adding 200, CH2 has a phase shift
The principle is that different initial values can be assigned during initialization, so there is no need to open an interrupt for toggle
Previous article:STM32 serial port idle interrupt problem
Next article:Notes on using ST_Link with the STM32F103C8T6
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- How to choose DC-DC converters and LED drivers to eliminate PCB space constraints
- MicroPython Hands-on (21) - Magnetic Field Sensor on Control Board
- 【IoT Graduation Project】STM32 Development Board + Gizwits IoT + Realize Smart Air Conditioning Control
- Why do different simulation results appear when the parameters of this simulation routine are set the same?
- Improving the efficiency of wireless sensor nodes using voltage control
- Principles and requirements for the 2021 National Undergraduate Electronic Design Competition
- Keil strange compilation problem
- Watch Shuige's video for a chance to win prizes | USB3.0 Device Tx physical layer consistency test
- Chat with Vicor engineers about modular power solutions for drones
- EEWORLD University Hall----Live Replay: TI's Latest Application of Millimeter-Wave Radar in the Automotive Field