Getting Started

(lily) #1

Chapter 8: C Pointers and Arrays


to the piezo-element.

e initialize orm as follows:

l Register A, TCCR1A, so that the OC1A pin (PortB
nting and cleared when down counting.

M1A1);

M with a top value in

e set the Output Compare Register 1 High to 0 and Low to the value in the
lue of Volume will produce a higher volume.

a imer1

CCR1B,
m , and the Input
apture e.

he song calls for a pause we stop Timer1, otherwise we start it
one) == p) | (pgm_read_word(pSong + Tone) == P))
0); // stop Timer1, prescaler(1)
sbi(TCCR1B, CS10); // start Timer1, prescaler(1)

ne value from the song array.

Initializing the Timer1 for PWM


W Timer1 to generate a PWM wavef


We set Timer/Counter Contro
pin 5) will be set when up cou


TCCR1A = (1<<CO


We set the TCCR1B for phase and frequency correct PW
ICR1.


TCCR1B = (1<<WGM13);


We set the TCCR1B to start Timer 1 with no prescaling


sbi(TCCR1B, CS10);


W
Volume variable. A lower va


OCRA1H = 0;
OCRA1L = Volume;


Gener ting the tone using PWM from T


When Play_Tune is called periodically by Timer0, we set the Timer1 T
the Ti er/Counter1 High and Low registers, TCNT1H TCNT1L
C Register 1, ICR1H, which in this case is used to set the counter top valu


In Play_Tune:


If t


if((pgm_read_word(pSong + T
cbi(TCCR1B, CS1
else


We then load the to

Free download pdf