SOUND CODE
//sched2 modified to include
NON-BLOCKING serial code
//used as
an example in the program organization doc.
#include <Mega32.h>
#include <stdio.h>
//for debugging using printf, etc
//timeout values for each
task
#define playsongt2 125
// NoteTable
#define B3 253
#define C4 238
#define CS4 225
#define D4 213
#define DS4 201
#define E4 190
#define F4 179
#define FS4 169
#define G4 159
#define GS4 150
#define A4 142
#define AS4 134
#define B4 127
#define C5 119
#define CS5 113
#define D5 106
#define DS5 101
#define E5 95
#define F5 89
#define FS5 84
#define G5 80
#define GS5 75
#define A5 71
#define AS5 67
#define B5 63
#define C6 60
#define CS6 56
#define D6 53
#define DS6 50
#define E6
47
#define F6
44
#define G6 40
//timer 1 constants
#define prescale1 1
#define clear_on_match
8
//the subroutines
void task1(void); //test for button press
//void task2(unsigned
char maxnote, int *song); //increment note to be played
void playsongfunction(void);
//void initialize(void);
//all the usual mcu
stuff
void InitSound(void); //Initialize sound
void playsong(unsigned char
feeling); //play desired song
//void play(void);
unsigned char reload, songNos, maxnote,delay; //timer
0 reload to set 1 mSec
//unsigned char time1, time2,
time3; //task scheduleing
timeout counters
unsigned char playsongtime;
unsigned char cmd_ready; //set when
command is received
unsigned char cmd; //'p'=play
's'=stop
unsigned char note; //current
note to be played...
//task
1 sets note to 0 to start song
int *song;
//****************************************************
int happysong[24]={24,C5,E5,G5,0,G5,0,G5,0,E5,G5,0,C5,E5,D5,0,D5,0,G5,0,G5,E5,E5,100};
int excitedsong[22]={22,C5,D5,E5,G5,0,G5,A5,G5,E5,C5,C5,D5,E5,0,E5,D5,0,D5,C5,C5,100};
int boredsong[16]={16,E5,E5,E5,G5,0,G5,G5,0,E5,E5,E5,D5,C5,C5,100};
int sadsong[9]={9,G5,G5,E5,E5,C5,C5,C5,100};
int angrysong[21]={20,G5,0,G5,0,G5,0,E5,E5,0,0,F5,0,F5,0,F5,0,D5,D5,100};
int naughtysong[18]= {18,
F5,A5,G5,A5,C6,0,C6,0,F5,A5,G5,A5,F5,0,F5,F5,100};
int gameoversong[25]={25,C5,C5,C5,0,C5,C5,C5,0,C5,DS5,DS5,D5,D5,C5,C5,B4,B4,C5,C5,C5,100};
//**********************************************************
//timer 0 overflow ISR
/*interrupt [TIM0_OVF] void
timer0_overflow(void)
{
//reload to force 1 mSec
overflow
TCNT0=reload;
//Decrement the three times if they are not
already zero
// if (time1>0) --time1;
if (playsongtime>0) --playsongtime;
} */
/*
//**********************************************************
//timer 1 compare-match A ISR
interrupt [TIM1_COMPA] void cmpA_overflow(void)
{
PORTD = ~PORTD; //toggle the port to make a sound
}
//**********************************************************
void playsong(unsigned char
feeling)
{
switch(feeling)
{
case 1: //excited
maxnote=excitedsong[0];
song = excitedsong;
break;
case 2: //happy
maxnote
= happysong[0];
song = happysong;
break;
case 3: //bored
maxnote=1;
break;
case 4: //bored
maxnote=boredsong[0];
song = boredsong;
break;
case 5: //sad
maxnote=sadsong[0];
song = sadsong;
break;
case 6: //angry
maxnote=angrysong[0];
song = angrysong;
break;
case 7:
maxnote=naughtysong[0];
song=naughtysong;
break;
case 8:
maxnote=gameoversong[0];
song=gameoversong;
break;
}
note = 1;
}
//**********************************************************
//Task 2 -- detect start/} of scale and
increment the note
void playsongfunction(void)//(unsigned
char maxnote, int *song)
{
if(song[note]==100) //turn off song
note=maxnote;
playsongtime=playsongt2;
if (delay == 1) //reset
the task timer
{
delay = 0;
if (note < maxnote) //play
a note? or are we done?
{
TCNT2=0; //resets timer2
if(song[note]==0)
{
note++;
TCCR2=0x18; //turn off timer2 to get a pause betweem
mptes
}
else
{
OCR2=song[note++];
//and load the correct time out for
timer 2
TCCR2 = 0x1E
; //clear timer2 on
compare match, start timer 2, toggle OC2 pin
}
}
else
{
TCCR2=0x18; //turn off timer 2
}
}
else
delay++;
}
//**********************************************************
void InitSound(void){
delay = 0;
//set up the ports
DDRD=0xff;
// PORT D is an output
PORTD.7=0;
interrupt OC1A
TCCR2 =0x18; //disable
timer2, Set CTC bit, Set toggle OC2 on compare match bit
TCNT2 = 0;
//and zero
the timer
note=1;
playsongtime=playsongt2;
}