video blog from the poetry of absolute
http://www.absolutepoetry.org/-Poetry- Video-Cell-
Salutoni :-)
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:
push, we could then "settle" for a value almost equal to that assigned and configure timer1 so FastPWM resolution fixed 9bit.
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):
a non-inverting mode inverting ____________ ____________
We have the following task:
generate, with the 16 bit timer1 by a symmetrical square wave signal (DC = 50%) having a frequency of 1 kHz, using as a system clock to fc = 4MHz
The switching of the logic level of an output pin , at regular intervals, can be seen as a square wave:
V (OUT)
5V
k
N
Imposta la commutazione hardware del pin OC1A all'azzeramento del timer (un clock dopo 1999)L'impostazione della commutazione hardware, avviene impostando i bit
Imposta prescaler a 1 (nessun prescaler);
int main
void) {
DDRB =
\u0026lt;
; / / Set the pin PB1 (OC1A) as output TCCR1B so CTC (on OCR1A) OCR1A. In
=
1999; / / load register 1999 in OCR1A
TCCR1A
on, the timer is started!
while
(
1)
{/ * do nothing ... resetting the timer (after 1999) will be switching hardware OC1A * /
}}
simulate vmlab with our software (as explained above) and the result is just a square wave of 1kHz.
To measure the frequency, click on the track and then click Analyze