AVR timer/counter using PWM function design points and application examples

Publisher:CuriousTravelerLatest update time:2017-12-12 Source: eefocusKeywords:AVR Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Key points of timer/counter PWM design

According to the characteristics of PWM, the following points should be noted when using the ATmega128 timer/counter to design output PWM:

1. First, the PWM frequency range to be output should be determined according to the actual situation. This frequency is used to control the brightness of the lamp. Since the human eye cannot distinguish frequencies above 42Hz, the PWM frequency should be higher than 42Hz, otherwise the human eye will notice the flickering of the lamp.

2. Then determine the PWM working mode of the ATmega128 timer/counter according to the required PWM frequency range. The PWM mode of the AVR timer/counter can be divided into two categories: fast PWM and frequency (phase) adjustment PWM.

3. Fast PWM can obtain a relatively high frequency PWM output, but the duty cycle adjustment accuracy is slightly worse. At this time, the counter only works in a one-way forward counting mode. The upper limit value of the counter determines the frequency of PWM, and the value of the comparison match register determines the size of the duty cycle. The calculation formula of PWM frequency is:

PWM frequency = system clock frequency / ( frequency division coefficient * (1 + counter upper limit))

4. Fast PWM mode is suitable for applications that require high output PWM frequency, but fixed frequency, and low duty cycle adjustment accuracy.

5. The duty cycle adjustment accuracy of frequency (phase) adjustment PWM mode is high, but the output frequency is relatively low, because the counter only works in bidirectional counting mode at this time. Similarly, the upper limit of the counter determines the frequency of PWM. The calculation formula of PWM frequency is:

PWM frequency = system clock frequency / (frequency division coefficient * 2 * counter upper limit))

6. Phase adjustment PWM mode is suitable for applications that require low output PWM frequency, but fixed frequency, and high duty cycle adjustment accuracy. When adjusting the duty cycle, the phase of PWM also changes accordingly (Phase Correct).

7. Frequency and phase adjustment PWM mode is suitable for applications that require low output PWM frequency, need to change the output frequency, and high duty cycle adjustment accuracy. At this time, it should be noted that not only will the phase of PWM change accordingly when the duty cycle is adjusted, but once the counter upper limit value is changed, that is, the output frequency of PWM is changed, the duty cycle and phase of PWM will change accordingly (Phase and  Frequency  Correct).

8. In PWM mode, the upper limit value of the counter is fixed at 0xFF (8-bit T/C); 0xFF, 0x1FF, 0x3FF (16-bit T/C). Or it can be set by the user to 0x0000-0xFFFF, and the set value is in the ICP or OCRA register of the 16-bit T/C . The ratio of the value of the compare match register to the upper limit value of the counter is the duty cycle.

2. PWM application design reference

  The following is a design example, in which the PWM method is used to generate a sine wave of about 1KHz with an amplitude of 0-V CC /2.

  First, create a sine wave sample table according to the following formula. The sample table divides a sine wave cycle into 128 points, and each point is quantized by 7 bits (127 corresponds to the highest amplitude Vcc/2):

f(x) = 64 + 63 * sin(2πx/180) x∈[0…127]

   If 128 sample points are used in a sine wave cycle, then the frequency of the corresponding 1KHz sine wave PWM is 128KHz. In fact, according to the sampling theorem that the sampling frequency is at least twice the signal frequency, the theoretical value of the PWM frequency is 2KHz. Considering the maximum output accuracy of PWM, the actual design uses a PWM frequency of 16KHz, that is, 16 sine wave sample values ​​are output in one sine wave cycle (1KHz). This means that in the 128-point sine wave sample table, one point is taken out every 8 points as the PWM output.

  The program uses the 8-bit T/C0 of ATmega128, the working mode is phase-adjusted PWM mode output, the system clock is 8MHz, the frequency division factor is 1, and the maximum PWM frequency it can generate is: 8000000Hz / 510 = 15686Hz. Every 16 outputs constitute a cycle of sine wave, and the frequency of the sine wave is 980.4Hz. PWM is output by OC0 (PB4) pin. The reference program is as follows (ICCAVR).

//ICC-AVR application builder : 2004-08
// Target : M128
// Crystal: 8.0000Mhz

#include
#include

#pragma data:code
// 128-point sine wave sample table
const unsigned char auc_SinParam[128] = {
64,67,70,73,76,79,82,85,88,91,94,96,99,102,104,106,109,111,113,115,117,118,120,121,
123,124,125,126,126,127,127,127,127,127,127,127,126,126,125,124,123,121,120,118,
117,115,113,111,109,106,104,102,99,96,94, 91,88,85,82,79,76,73,70,67,64,60,57,54,51,48,
45,42,39,36,33,31,28,25,23,21,18,16,14,12,10,9,7,6,4,3,2,1,1,0,0,0,0,0,0,0,1,1,2,3,4,6,
7,9,10,12,14,16,18,21,23,25,28,31, 33,36,39,42,45,48,51,54,57,60};
#pragma data:data

unsigned char x_SW = 8,X_LUT = 0;

#pragma interrupt_handler timer0_ovf_isr:17
void timer0_ovf_isr(void)
{
X_LUT += x_SW; // New sample pointer
if (X_LUT > 127) X_LUT -= 128; // Sample point pointer adjustment
OCR0 = auc_SinParam[X_LUT]; // Sample point pointer to compare match register
}

void main(void)
{
DDRB |= 0x10; // PB4(OC0) output
TCCR0 = 0x71; // Phase adjustment PWM mode, frequency division coefficient = 1, forward control OC0
TIMSK = 0x01; // T/C0 overflow interrupt enable
SEI(); // Enable global interrupt
while(1)
{……};
}



  Each time the counter overflows the interrupt service, a sine wave sample value is taken out to the compare match register to adjust the pulse width of the next PWM. In this way, a PWM square wave modulated by a sine wave is output on the PB4 pin . When the output of PB4 passes through a low-pass filter , a 980.4Hz sine wave is obtained. If you want to get a more accurate 1KHz sine wave, you can use the timer/counter T/C1, select working mode 10, and set ICR1=250 as the upper limit of the counter.

  On the ATMEL website, a reference for the application design of using a timer/counter to implement dual-tone dialing (AVR314.pdf) is given, from which readers can learn how to better design and use the PWM function.

Keywords:AVR Reference address:AVR timer/counter using PWM function design points and application examples

Previous article:Design and product development of digital control constant current source circuit based on PWM function of AVR microcontroller
Next article:Method and application example of adding EPROM data into AVR assembler

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号