//**************************************************************************** // // Program lab4code.c // Written by James Conrad, (some code from TI - i.e. beep, delay, march) // Modified by: XXXXXXXXXXXXXXXXXX (you)! main function only // Date Modified: // Input: 4 slide switches - Port 2, pins 0 to 3 // Output: speaker - Port 1, pin 1 // Plays audible tunes - either the Star Wars Empire March, Super Mario theme, // Also Sprach Zarathustra, or an annoying beep. // //**************************************************************************** // This macro identifies the ports available and provides the delay function #include "msp430g2553.h" //These macro define the frequencies for the tunes #define e4 164 #define g3 196 #define bSH 207 #define aSH 233 #define a3 220 #define c 261 #define b3 246 #define d 294 #define eF 311 #define e 329 #define f 349 #define gF 370 #define g 391 #define gS 415 #define a 440 #define aS 455 #define b 466 #define cH 523 #define cSH 554 #define dH 587 #define dSH 622 #define eH 659 #define fH 698 #define fSH 740 #define gH 784 #define gSH 830 #define aH 880 #define bF5 932 #define c6 1046 // Prototype functions - always prototype function you use // that means, identify in inputs and outputs void delay_ms(unsigned int ms ); void delay_us(unsigned int us ); void beep(unsigned int note, unsigned int duration); void freq(int x); void march(); void mario(); void sprach(); //**************************************************************************** // // function main // Input: Port 2 switches // Output: none - but calls beep which plays a tune on Port 1.1 // Main driver of Lab 4 - reads switches, calls // Plays audible tunes - either the Star Wars Empire March or an annoying beep // //**************************************************************************** int main( void ){ // Disable the watchdog timer //Set the direction for the speaker port and switch ports //Loop forever //Read the slide switches, set the variables based on correct switch values //If all of switches are 0000, play the Empire March by calling march() //If all of switches are 0001, play the Sprach by calling sprach() //If all of switches are 0010, play the mario theme by calling mario() //Otherwise, play an annoying beep by calling freq(x), where x is the // Port 2 input } // end of while loop } // end of the main program //YOU SHOULD NOT CHANGE ANY CODE BELOW HERE //**************************************************************************** // // function delay_ms // Input: int ms // Output: none // delays for ms milliseconds // Requires to be linked to the subroutine __delay_cycles (is in msp430g2553.h) // //**************************************************************************** void delay_ms(unsigned int ms ){ unsigned int i; for (i = 0; i<= ms; i++) __delay_cycles(500); } //**************************************************************************** // // function delay_us // Input: int us // Output: none // delays for us microseconds // Requires to be linked to the subroutine __delay_cycles (is in msp430g2553.h) // //**************************************************************************** void delay_us(unsigned int us ){ unsigned int i; for (i = 0; i<= 2 * us; i++) __delay_cycles(1); } //**************************************************************************** // // function beep // Input: note value and duration of note. // Output: outputs a pulse on Port 1.1 // Requires to be in the same file as the subroutines delay_us // Note: Also reads Port 2 -if no switches are set (play the Empire March) // then add a short delay to improve the sound of the March // //**************************************************************************** void beep(unsigned int note, unsigned int duration){ unsigned int i; //This is the semiperiod of each note. unsigned int delay = 10000/note; //This is how much time we need to spend on the note. unsigned int time = (duration*100)/(delay*2); for (i=0;i2 && x<16) { //Make sound proportional to switches beep(FrequencyChange*x, 100); } } //**************************************************************************** // // function march // Input: none // Output: none (beep does this) // Plays the Star Wars Empire March tune // Requires to be in the same file as the subroutines beep and delay_ms // //**************************************************************************** void march() { beep( a, 500); beep( a, 500); beep( a, 500); beep( f, 350); beep( cH, 150); beep( a, 500); beep( f, 350); beep( cH, 150); beep( a, 650); delay_ms(450); //first bit beep( eH, 500); beep( eH, 500); beep( eH, 500); beep( fH, 350); beep( cH, 150); beep( gS, 500); beep( f, 350); beep( cH, 150); beep( a, 650); delay_ms(450); //second bit... beep( aH, 500); beep( a, 300); beep( a, 150); beep( aH, 400); beep( gSH, 200); beep( gH, 200); beep( fSH, 125); beep( fH, 125); beep( fSH, 250); delay_ms(250); beep( aS, 250); beep( dSH, 400); beep( dH, 200); beep( cSH, 200); //start of the interesting bit beep(cH, 125); beep(b, 125); beep(cH, 250); delay_ms(250); beep(f, 125); beep(gS, 500); beep(f, 375); beep(a, 125); beep(cH, 500); beep(a, 375); beep(cH, 125); beep(eH, 650); //more interesting stuff (this doesn't quite get it right somehow) beep(aH, 500); beep(a, 300); beep(a, 150); beep(aH, 400); beep(gSH, 200); beep(gH, 200); beep(fSH, 125); beep(fH, 125); beep(fSH, 250); delay_ms(250); beep(aS, 250); beep(dSH, 400); beep(dH, 200); beep(cSH, 200); //repeat... repeat beep(cH, 125); beep(b, 125); beep(cH, 250); delay_ms(250); beep(f, 250); beep(gS, 500); beep(f, 375); beep(cH, 125); beep(a, 500); beep(f, 375); beep(cH, 125); beep(a, 650); } //**************************************************************************** // // function mario // Input: none // Output: none (beep does this) // Plays the Super Mario theme song tune // Requires to be in the same file as the subroutines beep and delay_ms // //**************************************************************************** void mario(){ beep (e,100); beep (e,100); beep (e,100); beep (c,100); beep (e,100); beep (g,100); beep (g3,100); beep (c,300); beep (g3,300); beep (e4,300); beep (a3,300); beep (b3,100); beep (aSH,200); beep (a3,100); beep (g3,100); beep (e,100); beep (g,100); beep (a,100); beep (f,100); beep (g,100); beep (e,100); beep (c,100); beep (d,100); beep (b3,100); beep (c,300); beep (g3,300); beep (e4,300); beep (a3,300); beep (b3,100); beep (aSH,200); beep (a3,100); beep (g3,100); beep (e,100); beep (g,100); beep (a,100); beep (f,100); beep (g,100); beep (e,100); beep (c,100); beep (d,100); beep (b3,100); beep (g,100); beep (gF,100); beep (f,100); beep (eF,100); beep (e,100); beep (bSH,100); beep (a3,100); beep (c,100); beep (a3,100); beep (c,100); beep (d,100); beep (g,100); beep (gF,100); beep (f,100); beep (eF,100); beep (e,100); beep (cH,100); beep (cH,100); beep (cH,100); beep (g,100); beep (gF,100); beep (f,100); beep (eF,100); beep (e,100); beep (bSH,100); beep (a3,100); beep (c,100); beep (a3,100); beep (c,100); beep (d,100); beep (eF,300); beep (d,300); beep (c,300); beep (c,100); beep (c,100); beep (c,100); beep (c,100); beep (d,100); beep (e,200); beep (c,200); beep (a3,200); beep (g3,100); beep (c,100); beep (c,100); beep (c,100); beep (c,100); beep (d,100); beep (e,100); beep (a,100); beep (g,100); beep (c,100); beep (c,100); beep (c,100); beep (c,100); beep (d,100); beep (e,200); beep (c,200); beep (a3,200); beep (g3,100); delay_ms(700); //Intro beep (e,100); beep (e,100); beep (e,100); beep (c,100); beep (e,100); beep (g,100); beep (g3,100); beep (g3,100); beep (c,100); beep (e,100); beep (g,100); beep (cH,100); beep (eH,100); beep (gH,100); beep (eH,100); beep (bSH,100); beep (c,100); beep (eF,100); beep (gS,100); beep (cH,100); beep (dSH,100); beep (gSH,100); beep (dSH,100); beep (aSH,100); beep (d,100); beep (f,100); beep (b,100); beep (dH,100); beep (fH,100); beep (bF5,100); beep (bF5,100); beep (bF5,100); beep (bF5,100); beep (c6,675); delay_ms(2000); } //**************************************************************************** // // function sprach // Input: none // Output: none (beep does this) // Plays the Also Sprach Zarathustra tune // Requires to be in the same file as the subroutines beep and delay_ms // //**************************************************************************** void sprach(){ beep (c, 800); beep (g, 800); beep (c, 1600); delay_ms(500); beep (e, 400); beep (eF, 3200); delay_ms(3000); beep (c, 800); beep (g, 800); beep (c, 1600); delay_ms(500); beep (eF, 400); beep (e, 3200); delay_ms(2000); beep (c, 800); beep (g, 800); beep (c, 1600); delay_ms(500); beep (e, 400); beep (f, 3200); delay_ms(1000); beep (e, 800); beep (f, 800); beep (g, 1600); delay_ms(100); beep (e, 400); beep (f, 400); beep (g, 400); delay_ms(500); beep (a, 3200); beep (b, 3200); beep (c, 6400); delay_ms(4000); }