pwm1.zip
CTC mode allows you to generate rectangular waveform, but only symmetrical (equal time on or off duty cycle to 50%). rectangular waveforms with duty cycle other than 50% can be generated with a different operating mode of the timer: the PWM.
On Timer1 can use three types of PWM:
1) Fast
2) A phase correction
3) A correction phase and frequency. The
1) is the fastest, you can reach higher frequencies of the remaining two, varying the duty cycle period (or frequency) remains constant, but varies the phase, the
V (OUT) 5V
_________ _________
fixed, then the timer counts from 0 to a number N equal to 2 n
-1:
N = 255 (8bit),
N = 511 (9bit)
N = 1023 (10bit) The
duration of the period Ts depends on these values, the clock period, the prescaler, in the familiar formula already seen:
Ts = Tc * k * (N +1) With our frequency fc of 4MHz (Tc = 0.25 us) for various values \u200b\u200bof the prescaler, we obtain the following TS values \u200b\u200b(expressed in ms) for the various resolutions of the fixed timer:
How
can be easily ascertained, have no value Ts = 1ms. Only N = 511 (9bit) and Prescaler 8 we get closer to 1ms (1.024 ms). If precision is not required
push, we could then "settle" for a value almost equal to that assigned and configure timer1 so FastPWM resolution fixed 9bit.
Assuming, however, you want an exact value of the signal period equal to 1ms, then we must resort to
variable resolution. Set the mode PWM controller with variable resolution determines the resolution between 2-bit (so the timer counts up to N = 3) and the maximum allowed by the timer: 16bit in the case of Timer1 (and therefore counts up to N = 65535).
So, ultimately, if we set the variable resolution value N can be chosen between 3 and 65535.
What is a possible value of N for Ts = 1 ms, we obtain from the inverse formula:Supponendo di volere usare il canale A, la situazione è raffigurata di seguito:
N = [Ts / (k * Tc)] - 1
For k = 1, 8, 64, 256, 1024, and Ts = 0.25 us (fc = 4MHz), substituting, we have:
----------
N 65535. We use
k = 1 (no prescaler) and therefore N = 3999
This value is loaded into the register ICR1
(
1).
defined the necessary number for the whole period Ts, now remains to determine the number (including in this case between 0 and 3999) at which you have the switching pin (or OC1A OC1B) where Ton = 0, 25ms.
If we create our waveform using the channel A, then the output signal on pin OC1A, we will load this value in the register OCR1A, conversely, if we use the channel B, then the waveform on pin OC1B, upload this value in the register OCR1B.
Finché il dato del timer1 (ovvero il valore del registro TCNT1) è inferiore al numero NA, l'uscita OC1A assume valore alto (5V). Allorquando il valore di TCNT1 eguaglia quello contenuto nel registro OCR1A, al successivo impulso di clock si ha una commutazione del pin OC1A a 0V. Infine, al raggiungimento del successivo valore N, il pin OC1A commuta a 5V.WGM13 and WGM12 bits are in the second configuration register: TCCR1B, while the remaining WGM11 bit found in the first configuration register TCCR1A. So we write:
Da precisare che questa è la modalità non invertente
. E' possibile configurare la generazione della forma d'onda anche in modalità invertente, per la quale, ovviamente, switching levels are inverted and then invert the waveform. remains to determine the number of NA at the time which has elapsed since the last reset Timer1 is precisely Ton.
It 's very simple. If
to employ a time Ts, the timer must count to N, evidently for a time Ton smaller the timer will count to a number NA \u0026lt;N:
(NA+1) * Tc * k
mentre per il periodo Ts, già si è visto che: Ts = (N+1) * Tc * k
Facendo il rapporto Ton/Ts ricaviamo il duty cicle e da qui il numero NA:
NA = [ d.c. * (N +1) ] -1 Nel nostro caso, essendo
d.c.=0,25 e N=3999 , ricaviamo NA=999
Abbiamo tutti i dati per scrivere il software, ma prima, ovviamente, scriviamo un possibile pseudocodice:
Imposta PB1 come pin d'uscita; Carica 3999 nel registro ICR1; Carica 999 in the register OCR1A;
FastPWM Sets mode with variable resolution (and value defined in ICR1 N);
Set waveform is not inverted;
Set prescaler: 1 (No prescaling)
Loading a value of register (with avr-libc) is trivial and needs no explanation, because already seen.
dell'ATmega168 Using the datasheet, you go to see what configuration registers of TIMER1 act and which bits to set, to obtain the desired settings.
FastPWM mode with variable resolution (and the value is defined in ICR1 N) is implemented by activating bit WGM13, WGM12 and WGM11, as described in Table 15-4 of d / sheet (as n. 14):
TCCR1B
................................................. ..............................
0 0 Other use of pin OC1A/OC1B (no PWM)
0 1 Other use or just OC1A (see d / sheet) 1 0 1 1
a non-inverting mode inverting
a non-inverting mode inverting So, we will activate only bit COM1A1 :
TCCR1A
TCCR1B WGM12);
// FastPWM con ICR1=N (Tab.15-4)
The simulation results are shown below. In the Annex the
Measurement Ton (0.25 ms)
measure Ts (1ms)
NOTES
-----------------------
1) Alternatively you can use the Register OCR1A (see d / sheet)
TCCR1A SIMULATION WITH VMLAB
0 comments:
Post a Comment