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.
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
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- Thonny Python Editor upgraded to 4.0.0 Beta1
- control
- DSP water light source program
- Embracing the era of big data and interpreting 5G communication clock synchronization technology
- 【CH579M-R1】+W25Q16 storage module reading and writing
- Playing with Zynq serial 16——Ubuntu16.04 64bit installation
- HFSS RF simulation design examples
- Is this the worst impedance processing you have ever encountered?
- PCB panelization
- RPG game ported using RP2040 and MicroPython