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 on to off, and then from off to on (the code comes from the 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
{ for(j=0;j<1140;j++) ; }
}
/********The following is the port initialization function********/
void port_init()
{
DDRB|=(1<
PORTB&=(0<
}
/********Timer 0 initialization********/
void timer0_init()
{
TCCR0=(1<
// T/C0 works in fast PWM mode without frequency division
//When the comparison matches, OC0 is set, and when the count reaches 0XFF, OC0 is cleared
OCR0=0; //Compare match register initial value,
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 brightest and then gradually dims
{
OCR0=count; //Compare match register assignment
Delay_ms(20); // Delay for a while to observe the effect
}
Delay_ms(3000); // When the LED is at its 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 brightest, delay for a while
}
}
Previous article:AVR microcontroller fuse position problem
Next article:AVR MCU C Language Programming Skills
- 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
- About the signal input mode of the power amplifier
- Another bottleneck.
- Easy-to-use and cheap microcontroller is here
- Inverting proportional operational amplifier circuit
- Upper computer displays waveform acquisition
- Evaluation Weekly Report 20211129: There are 4 days left for Anxinke NB-IoT and domestic FPGA applications, and the wireless charging mouse pad is waiting to be disassembled (Extra)
- Are there any pmos and nmos tubes with working current |Ids|=1.2A;|Vds|<0.2V, which don't matter if they are enhanced or not, and have low leakage current when turned off?
- EEWORLD University Hall----TI 77GHz millimeter wave radar product introduction and solution display
- KiCad 5.1.5 latest stable version Chinese tutorial
- Understand C language pointers in 1 minute