PWM: Pulse Width Modulation. In the figure, T is the pulse period, t is the high level time, and the ratio of t to T (t/T) is called the duty cycle. Pulse width modulation refers to adjusting the size of t, that is, changing the duty cycle of the pulse
. The larger the duty cycle value, the higher the output voltage. Changing the duty cycle changes the output voltage, which is often used to implement D/A, adjust voltage or current, change the speed of the motor, etc.
Fast PWM mode: Its counting method is that TCNT0 starts counting from 0 to 255, counts by 1 and returns to 0, and then continues to count by 1. Compared with the phase PWM correction mode (counting from 0 to 255, and then counting from 255 to 0), there is only one slope, so the PWM output frequency is high. In fast PWM mode, the maximum value of the counter determines the frequency of PWM, and the comparison register OCR0 determines the size of the duty cycle. T/C0 is an 8-bit counter, and the output PWM frequency = system clock frequency/(division factor*255).
PWM generation process: After the timer is started, when the values of TNCT0 and OCR0 are equal, the output pin OC0 (PB3) is cleared to 0, and when TNCT0 reaches 255, OC0 is set. Changing the value in OCR0 during program execution changes the duty cycle of OC0 output.
//Function: Control the light-emitting diode on the PB3 pin from bright to dark, and then from dark to bright (code from Easy to Play AVR MCU C Language CD)
#include
#define uchar unsigned char
#define uint unsigned int
unsigned int count;
/*********The following is the delay function*********/
void Delay_ms(uint xms)
{
int i,j;
for(i=0;i
}
/********The following is the port initialization function********/
void port_init()
{
DDRB|=(1<
/********Timer 0 initialization********/
void timer0_init()
{
TCCR0=(1<
// When compare match, OC0 is set, and when count reaches 0XFF, OC0 is cleared
OCR0=0; //Initial value of compare match register,
SREG=0x80; //Enable global interrupt
}
/*********The following is the main function*********/
void main(void)
{
port_init();
timer0_init();
while(1)
{
for(count=0;count<256;count++) //When OCR=0, the LED is the brightest, and then gradually dims
{
OCR0=count; //Assign compare match register value
Delay_ms(20); //Delay for a period of time to observe the effect
}
Delay_ms(3000); // When the LED is the darkest, delay for a while
for(count=255;count>0;count--) //When OCR=255, the LED is the darkest, and then gradually brightens
{
OCR0= count;
Delay_ms(20);
}
Delay_ms(3000); //When the LED is the brightest, delay for a while
}
}
Previous article:3. AVR timer/counter 0 --TC0 -- phase correction PWM mode
Next article:1. avr timer/counter 0 --TC0 --CTC mode output PWM
Recommended ReadingLatest update time:2024-11-16 12:27
- 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
- Summary of BlueNRG series tutorials, documents, training materials and FAQs (continuously updated)
- Dahua Black Body Ultra-Detailed Disassembly
- Three-electrode electrochemical sensor problems
- Problems with lighting LEDs when connecting a microcontroller to ESP8266
- TypeC and Micro charging head two-choice circuit. When TYPEC and MICRO charging heads are inserted at the same time, only MICRO can charge...
- [SAMR21 New Gameplay] 22. PWM
- RSL10-002GEVB Bluetooth Function Test
- A table to understand the difference between 5G and Wi-Fi 6
- MSP430 Application Tips 4: How to Create an MSP430Ware Project
- OC5021B-OC5020B-OC5022B-OC5028B-LED car light solution (supports PWM dimming or linear dimming)