AVR MCU TC0 fast PWM

Publisher:gamma13Latest update time:2013-01-10 Source: 21IC Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1.jpg

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

}

}

Reference address:AVR MCU TC0 fast PWM

Previous article:AVR microcontroller fuse position problem
Next article:AVR MCU C Language Programming Skills

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号