Sunday, September 5, 2010

Lumineyes Colour Change

AVR timers in five minutes - to generate waveforms (with how fast PWM) timers

quadra.zip

 
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
____________ ____________
T T

AVR (così come con altri micro) è possibile effettuare la commutazione del livello di uscita, non solo via software come si è fatto nella I parte (mediante un interrupt e un'operazione di ex-or) ma anche completamente via hardware, con tutti i vantaggi del caso. Se, quindi, il nostro compito è semplicemente quello di commutare il livello di un piedino di uscita a intervalli di tempo regolari ovvero, in altre parole,
generare un'onda quadra simmetrica
è preferibile utilizzare sempre il modo CTC già visto, ma con un meccanismo hardware invece di uno software. Nel modo CTC con meccanismo hardware non ci sono interrupt e relative routine di gestione da write software; CTC mode does do the job: when the data contained in the register of timer1 TCNT1 equals that of register OCR1A

(1)

, the next clock pulse is to reset the timer.

What changes is what to do if necessary zeroing: do not use more than one interrupt, but is (entirely in hardware) a switching of the logic level of a particular output pin called OC1A or, if configured differently our software, as we shall see, the zero point (Vout = 0V) or activation of the output (Vout = 5V).

Unlike the "software mechanism, which uses an interrupt, where you are free to choose not only what to do every T seconds, but the leg on which have switched, so using the CTC with the hardware mechanism is required to use a specific pin, shown in the datasheet with the reference OC1A (which just happened in their match ATmegax8 with PB1):


The signal period is



Ts = 1 / f = 1ms

T = Ts / 2 = 0.5ms

calculate the number N be loaded into the register
OCR1A
for different prescaling values: 1, 8, 64, 256 or 1024 through la nota formula:






  

k



N



1
1999
8
249 64
30,25



256

6,8125
0,9531



A questo punto prendiamo in considerazione solo fattori di prescaler per i quali abbiamo numeri N rappresentabili dal timer a 16 bit (numeri interi e non superiori a 65535), quindi scartiamo da subito k=64, k=256 e k=1024.

Rimangono infine k=1 e k=8, dei due preferiamo il più piccolo (nessun prescaler) in corrispondenza del quale
N=1999
.

Scriviamo il pseudocodice:


Imposta modo CTC (con confronto sul registro OCR1A) sul Timer1;
Carica OCR1A con il valore 1999;
 Imposta la commutazione hardware del pin OC1A all'azzeramento del timer (un clock dopo 1999) 
Imposta prescaler a 1 (nessun prescaler);


L'impostazione della commutazione hardware, avviene impostando i bit
COM1A1
e
COM1A0
del registro
TCCR1A
, che definiscono "cosa fare" sull'azzeramento
del timer: 1) switch the output, 2) reset the output, 3) set the output
COM1A1 COM1A0
Description --------------------- -------------------------------------------------- ------
0 0 Default: use the leg normally OC1A (such as input / output)
0 1 Switches the pin OC1A (0V-> 5V, 5V-> 0V) 1 0 Reset pin OC1A (0V) 1 1
Set OC1A pin (5V)


That in avr-libc code becomes:

TCCR1A COM1A0 ) ;
/ / Set up Toogle OC1A



For k = 1, from the table in datasheet dell'Atmega8, we get that we need only turn on the CS10 bit register TCCR1B:
 






TCCR1B

\u0026lt;

avr / io.h

>

 
int main


(
 

void) {






 DDRB = 

(

1
 
\u0026lt;

\u0026lt;

PB1)
 ;   / / Set the pin PB1 (OC1A) as output    TCCR1B   so CTC (on OCR1A)    OCR1A 
=

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
. In

zip attachment, in addition to the source, there is also a project file (QUADRA.PRJ) for VMLAB.
----
Note 1) E 'can be used as an alternative to OCR1A register, the register ICR1 (see datasheet for the different configuration)

0 comments:

Post a Comment