Timing / counter PWM design points and application design reference

Timer / counter PWM design points

According to the PWM ((Pulse Width Modulation) is a very effective mode to control the analog circuit using the digital output of the microprocessor)), in the use of timer/counter design output of ATmega128 The following points should be noted when PWM:

1. First, according to the actual situation, determine the PWM frequency range that needs to be output. This frequency is related to the controlled object. If the output PWM wave is used to control the brightness of the lamp, because the human eye can not distinguish the frequency above 42Hz, the frequency of the PWM should be higher than 42Hz. Otherwise, the human eye will perceive the flickering of the lamp.

2. Fast PWM can be compared to a relatively high frequency PWM output, but the accuracy of the duty cycle adjustment is slightly worse. At this time, the counter only works in one-way forward counting mode. The upper limit value of the counter determines the PWM frequency, and the value of the compare match register determines the size of the duty cycle. The PWM frequency is calculated as:

3. Then determine the frequency range of the PWM according to the needs of ATmega128 (which is the highest configuration of ATMEL's 8-bit microcontroller series, a very wide range of applications) timer / counter PWM work. The AVR timing/counter PWM mode can be divided into two categories, fast PWM and frequency (phase) adjustment PWM.

PWM frequency = system clock frequency / (divisor coefficient * (1 + counter upper limit value)) AVR microcontroller is an enhanced built-in Flash RISC developed by Atmel Corporation in 1997 (Reduced according to PWM ((PWM: (PULSE Width ModulaTIon) is a very effective mode to control the analog circuit using the digital output of the microprocessor.) The following points should be noted when using the timer/counter design output PWM of the ATmega128:

1. First, according to the actual situation, determine the PWM frequency range that needs to be output. This frequency is related to the controlled object. If the output PWM wave is used to control the brightness of the lamp, because the human eye can not distinguish the frequency above 42Hz, the frequency of the PWM should be higher than 42Hz. Otherwise, the human eye will perceive the flickering of the lamp.

2. Fast PWM can be compared to a relatively high frequency PWM output, but the accuracy of the duty cycle adjustment is slightly worse. At this time, the counter only works in one-way forward counting mode. The upper limit value of the counter determines the PWM frequency, and the value of the compare match register determines the size of the duty cycle. The PWM frequency is calculated as:

3. Then determine the frequency range of the PWM according to the needs of ATmega128 (Atmel's 8-bit microcontroller is the highest configuration of a microcontroller, an extremely wide range of applications) timer / counter PWM work. The AVR timing/counter PWM mode can be divided into two categories, fast PWM and frequency (phase) adjustment PWM.

PWM frequency = system clock frequency / (divisor coefficient * (1 + counter upper limit value)) AVR microcontroller is a RISC (Reduced InSTrucTIon Set CPU) RISC (Reduced InSTrucTIon Set CPU) developed by ATMEL Corporation in the reduced instruction set high-speed 8-bit SCM.

4. Frequency (Phase) Adjustment The PWM mode has a high accuracy of duty cycle adjustment, but the output frequency is relatively low because the counter only works in the bidirectional counting mode. Similarly, the upper limit value of the counter determines the frequency of the PWM. The value of the compare match register determines the size of the duty cycle. The PWM frequency is calculated as:

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

5. Fast PWM mode is suitable for applications requiring high output PWM frequency, but with a fixed frequency and low duty cycle adjustment accuracy.

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

7. In PWM mode, the upper limit of the counter has a fixed 0xFF (8-bit T/C); 0xFF, 0x1FF, 0x3FF (16-bit T/C).

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

This will cause the PWM's duty cycle and phase to change accordingly (Phase and Frequency Correct).

PWM Application Design Reference

PWM mode can be divided into fast PPWM is a method of digitally encoding analog signal levels. Through the use of a high resolution counter, the duty cycle of the square wave is modulated to encode the level of a particular analog signal. The PWM signal is still digital because at any given moment, full-scale DC power is either completely ON or completely OFF. The voltage or current source is added to the analog load in a repeating pulse sequence that is either ON or OFF. When it is on, it is when the DC power supply is added to the load, and when the power is off, the power supply is disconnected. As long as the bandwidth is sufficient, any analog value can be coded using PWM. WM and frequency (phase) adjustment PWM two categories.

Set the period of the on-chip timer/counter that supplies the modulated square wave

Set the on-time in the PWM control register

Sets the direction of the PWM output. This output is a general-purpose I/O pin

Start timer

Enable PWM controller PWM mode can be divided into two categories: fast PWM and frequency (phase) adjustment PWM.

In the following example, the PWM method is used to generate a sine wave of about 1 KHz with an amplitude of 0-VCC/2.

First, establish 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 quantified by 7 bits (127 corresponds to the highest amplitude Vcc/2):

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

If 128 samples are used in one sine wave period, the frequency of the 1 kHz sine wave PWM is 128 KHz. 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 can be 2 kHz. Consider improving the PWM output accuracy as much as possible. The actual design uses a PWM frequency of 16KHz. This means that in the 128-point sine wave sample table, a point is taken every 8 o'clock as the PWM output.

The 8-bit T/C0 operating mode of the ATmega128 used in the program is the phase-adjusted PWM mode output. The system clock is 8 MHz and the frequency division factor is 1, which can generate the highest PWM frequency:

8000000Hz / 510 =

15686Hz. Every 16 outputs form a period sine wave, the frequency of the sine wave is 980.4Hz. The PWM is output from the OC0 (PB4) pin. The reference procedure is as follows (ICCAVR).

//ICC-AVR applicaTIon builder : 2004-08

// Target : M128

// Crystal: 8.0000Mhz

#include "iom128v.h"

#include macros.h

#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 pointer adjustment

OCR0 = auc_SinParam[X_LUT]; // Sampling point pointer to compare match register

}

Void main(void)

{

DDRB |= 0x10; // PB4(OC0) output

TCCR0 = 0x71; // Phase-adjusted PWM mode, division factor = 1, positive control OC0

TIMSK = 0x01; // T/C0 overflow interrupt enable

SEI(); // Enable global interrupt

While(1)

{...};

}

Each time the service of the counter overflow interrupt takes out a sample value of a sine wave to the compare match register, it is used to adjust the pulse width of the next PWM, so that 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. To obtain a more accurate 1 kHz sine wave, use timer/counter T/C1, select operating mode 10, and set ICR1=250 to the upper limit of the counter.

Manual Pulse Generator

A manual pulse generator (MPG) is a device normally associated with computer numerically controlled machinery or other devices involved in positioning. It usually consists of a rotating knob that generates electrical pulses that are sent to an equipment controller. The controller will then move the piece of equipment a predetermined distance for each pulse.
The CNC handheld controller MPG Pendant with x1, x10, x100 selectable. It is equipped with our popular machined MPG unit, 4,5,6 axis and scale selector, emergency stop and reset button.

Manual Pulse Generator,Handwheel MPG CNC,Electric Pulse Generator,Signal Pulse Generator

Jilin Lander Intelligent Technology Co., Ltd , https://www.jllandertech.com