2. avr timer/counter 0 --TC0 -- fast PWM output

Publisher:SHow111timeLatest update time:2017-11-27 Source: eefocusKeywords:avr Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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{ 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 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

}


Keywords:avr Reference address:2. avr timer/counter 0 --TC0 -- fast PWM output

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

AVR microcontroller learning (I)
1. Basics Basic elements of microcontrollers     CPU core (such as: 51-core    AVR core M430 core)     ROM (READ ONLY Memory)     RAM (random  access  Memory  )     On-chip peripherals     bus    CPU core: It is equivalent to the CPU of a computer. It executes instructions from ROM. There are severa
[Microcontroller]
AVR microcontroller learning (I)
AVR microcontroller analog comparator initialization configuration and description
The avr analog comparator compares the value of the positive AIN0 with the value of the negative AIN1.  When the voltage on the analog comparator is high, the output of the analog comparator ACO is set. The output of the comparator can be used to trigger the input capture function of timer/  counter  1. In addition,
[Microcontroller]
Analysis of the reset function inside the AVR microcontroller
Reset of AVR microcontroller: watchdog reset, power-on reset, power-off reset. I see that everyone is not very clear about the reset function, especially the reset function inside the AVR microcontroller. Now I will briefly talk about it here, and I hope you can have a general understanding at home. There are four t
[Microcontroller]
Analysis of the reset function inside the AVR microcontroller
The specific application of AVR microcontroller in life
    This article discusses the specific applications of single-chip microcomputers in life, tells the development history and characteristics of single-chip microcomputers, and looks forward to the prospects of single-chip microcomputers.     With the continuous development of urban public transportation, unmanned tic
[Microcontroller]
Summary of Several Working Modes of Timers in AVR
AVR has three timer counters, of which timer counter 0 and timer counter 2 are 8 bits, and timer counter 1 is 16 bits. When I was learning about AVR timers, I was confused by a lot of registers at first. Later, I carefully read the relevant registers in the datasheet and then re-read the content of the timer to sort i
[Microcontroller]
AVR MCU driver NOKIA3310 sample program
#include          #include          #include           void main(void)         {             PORTB&=209;           DDRB|=46; //Set the MCU's 4 LCD pins to output 0            while(1)                 {                    lcd_init(); //LCD initialization                           lcd_cls(); //Clear the screen,
[Microcontroller]
AVR MCU software button debounce and confirmation
The program process is very simple. After the system is powered on, LED0-LED3 will light up and LED4-LED7 will go out. When SW8 is pressed for the first time, LED4-LED7 will light up and LED0-LED3 will go out. When SW8 is pressed again, LED0-LED3 will light up and LED4-LED7 will go out. There will be some jitter w
[Microcontroller]
AVR MCU software button debounce and confirmation
Design of Solar Controller Based on AVR Microcontroller
  With the deepening of energy crisis and environmental pollution, the research and utilization of solar energy has received widespread attention. Solar energy is an inexhaustible renewable energy source for human beings. It is also a clean energy source that does not produce any environmental pollution. In the effecti
[Microcontroller]
Design of Solar Controller Based on AVR Microcontroller
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号