In high frequency conditions, a good use of PWM is to control the output power by controlling the duty cycle, such as controlling the fan speed, the brightness of the LED light, etc. This time, we use the interrupt function of PWM to dynamically change the duty cycle of the pulse to achieve the effect of a breathing light.
1. Implementation ideas
PWM can choose to have the counter generate an interrupt at the end of the period (when aligned at the center of the period, it may also choose to generate an interrupt at the center of the period), and can dynamically adjust the duty cycle, period, polarity and other properties during operation. So you can dynamically change the duty cycle in the interrupt handler to change the brightness of the LED light.
This time, channel 0 and pin PA0 will also be used.
2. PWM Setting
A higher frequency clock is needed here, so the main clock is divided by 32 (12.5 kHz). The counter period is 400, that is, the output pulse frequency is 125000/400 = 312.5 Hz. At the same time, the corresponding interrupt needs to be enabled.
The main configuration code of PWM is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 | #define PERIOD_VALUE 400 /* Clock selection */ PWM->PWM_CH_NUM[0].PWM_CMR = PWM_CMR_CPRE_MCK_DIV_32; /* Enable interrupts */ PWM->PWM_IER1 = PWM_IER1_CHID0; /* Period and duty cycle*/ PWM->PWM_CH_NUM[0].PWM_CPRD= PWM_CPRD_CPRD(PERIOD_VALUE); PWM->PWM_CH_NUM[0].PWM_CDTY = PWM_CDTY_CDTY(0); /* Enable interrupt */ NVIC_ClearPendingIRQ(PWM_IRQn); NVIC_SetPriority(PWM_IRQn, 0); NVIC_EnableIRQ(PWM_IRQn); |
3. PWM interrupt processing
After each cycle, an interrupt is generated. Then in the interrupt handling function, change the duty cycle. It should be noted that when PWM is enabled, the duty cycle needs to be changed by writing to the PWM duty cycle modification register (PWM_CDTYUPD). By default, the modification takes effect in the next cycle.
For better results, you can set an interval between two breaths.
Note that the interrupt generated needs to be pulled low by reading PWM_ISR1. The interrupt handling function is posted in the complete code below.
Previous article:SAM4E MCU Tour - 6. LED Flashing Button Control
Next article:SAM4E MCU Tour - 4. LED Flashing PWM
Recommended ReadingLatest update time:2024-11-16 13:58
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
- EEWORLD University Hall----Compilation Principles
- Want to play? STC announces 32-bit 8051, which will be mass-produced in November 2021
- Common Problems of C2000 in Real-time Control Systems
- LLC parameter design, cannot achieve ZVS
- Soldering methods and precautions for chip crystal oscillators
- Guangzhou Power Chip Sales and FAE Recruitment
- I don't understand why the feedback network of the op amp is connected to the non-inverting terminal. What is the purpose of this?
- Matlab Neural Network Principles and Examples
- Setting the MSP430 main system clock for low power consumption
- TI XDC Tool Overview