Getting Started

(lily) #1

Chapter 8: C Pointers and Arrays


porary variable and shift it right by 8
t that we
te for the
en a 1 MHz
as used in the original Butterfly code.

= 7;

lly top value low byte and adjust it for the 2 MHz clock.

e Timer0 interrupt calls the Play_Tune function every 10
before:

re value

caler

temp_hi = pgm_read_word(pSong + Tone); // read out the PWM-value


The Tone is an integer, so we get it into a tem
bits and load that value into the high byte of the counter top. Excep
actually only shift it right by 7 bits to adjust it (cut it in half) to compensa
use of a 2MHz system clock in this applications (for the USART) wh
clock w


temp_hi >>


We clear the Timer1 count.


TCNT1H = 0;
TCNT1L = 0;


We load the counter top value high byte.


ICR1H = temp_hi;


Fina we load the counter


ICR1L = pgm_read_word(pSong + Tone);
ICR1L <<= 1;


Using the Timer0 interrupt to play a tune


As mentioned above th
ms. We set up the Timer0 much as we’ve seen


// Enable timer0 compare interrupt
TIMSK0 = (1<<OCIE0A);


// Sets the compa
OCR0A = 38;


// Set Clear on Timer Compare (CTC) mode, CLK/256 pres
TCCR0A = (1<<WGM01)|(0<<WGM00)|(4<<CS00);

Free download pdf