In this lab exercise you will program the MSP 430 board to play either the Star
ID: 1846852 • Letter: I
Question
In this lab exercise you will program the MSP 430 board to play either the Star Wars Empire March or a
tone when the four switches are set to different values. Specifically:
1. When the switches are 0x0 (binary 0000), the March will play. It will finish before the switches
are examined again.
2. When the switches are 0x1 to 0xF (binary 0001 to 1111), an audible tune will play. It will play
for a very short time, after which the switches are examined again. You may not notice the
break when the switches are examined.
3. Suggestion: wire the switches to Port 2, bits 0 to 3. Wire the speaker to Port1.1
How do put the code in the main part (the on with the color red)? i just don't get it .
// This macro identifies the ports available and provides the delay function
#include "msp430g2553.h"
//These macro define the frequencies for the tunes
#define c 261
#define d 294
#define e 329
#define f 349
#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
// 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 march();
//****************************************************************************
//
// 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
//PUT YOUR CODE HERE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//Set the direction for the speaker port and switch ports
//PUT YOUR CODE HERE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//You will need several lines here
// Define variable that identifies if the switches are slid
int SwitchP2_0 = 0;
int SwitchP2_1 = 0;
int SwitchP2_2 = 0;
int SwitchP2_3 = 0;
int FrequencyTotal = 0; //Relates to 12 Hz, lowest audible frequency
int FrequencyChange = 100; //Relates to 1332 Hz change
//PUT YOUR CODE HERE THAT MAKES A FOREVER LOOP!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//Read the slide switches, set the variables based on switch values
//MODIFY THIS CODE BELOW!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
SwitchP2_0 = xxxxxxxxxxxxx;
SwitchP2_1 = xxxxxxxxxxxxx;
SwitchP2_2 = xxxxxxxxxxxxx;
SwitchP2_3 = xxxxxxxxxxxxx;
//If all of switches are 0000, play the Empire March
//MODIFY THIS CODE BELOW!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
if(xxxxxxxxx){
march();
delay_ms(2000);
} // end of if code
else {
//The switches are between 0001 and 1111. Play a tone instead. Based
//on the switches slid, the speaker makes a low (0001) or high (1111) tone
//Feel free to experiment with different settings
if (SwitchP2_0) FrequencyTotal = FrequencyTotal + FrequencyChange*1;
if (SwitchP2_1) FrequencyTotal = FrequencyTotal + FrequencyChange*2;
if (SwitchP2_2) FrequencyTotal = FrequencyTotal + FrequencyChange*4;
if (SwitchP2_3) FrequencyTotal = FrequencyTotal + FrequencyChange*8;
//Make sound proportional to switches
beep(FrequencyTotal, 100);
// Resets frequency total
FrequencyTotal = 0;
} // end of else code
} // 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;
unsigned int delay = 10000/note; //This is the semiperiod of each note.
unsigned int time = (duration*100)/(delay*2); //This is how much time we need to spend on the note.
for (i=0;i<time;i++){
P1OUT |= BIT1; //Set P1.1...
delay_us(delay); //...for a semiperiod...
P1OUT &= ~BIT1; //...then reset it...
delay_us(delay); //...for the other semiperiod.
}
if (!P2IN) delay_ms(50); //Add a little delay to separate the single notes
}
//****************************************************************************
//
// 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);
}
Explanation / Answer
I have modified only the main program. Please let me know if you need any thing else.
modified main is as below
int main( void )
{
// Disable the watchdog timer
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
//Set the direction for the speaker port and switch ports
// port2, 0 to 3 are input and port 1.1 is output
P2SEL &= 0xF0 ; 2.0-3 are GPIO -> clear lower 4 bits
P2DIR &= 0xF0 ; 2.0-3 are input -> lower 4 bits to cleared for input
P1SEL &= 0xFD ; 1.1 is GPIO -> clear 1st bit
P1DIR |= 0x02 ; 1st bit to be set for output
// Define variable that identifies if the switches are slid
int SwitchP2_0 = 0;
int SwitchP2_1 = 0;
int SwitchP2_2 = 0;
int SwitchP2_3 = 0;
int FrequencyTotal = 0; //Relates to 12 Hz, lowest audible frequency
int FrequencyChange = 100; //Relates to 1332 Hz change
while(1)
{
//Read the slide switches, set the variables based on switch values
SwitchP2_0 = P2IN & 0x01; // reading the input value of 0th bit
SwitchP2_1 = P2IN & 0x02; // reading the input value of 1st bit
SwitchP2_2 = P2IN & 0x04; // reading the input value of 2nd bit
SwitchP2_3 = P2IN & 0x08; // reading the input value of 3rd bit
// if all the inputs are 0 then P2IN & 0x0F will be zero else 1
if(P2IN & 0x0F)
{
//some input is 1
/*if (SwitchP2_0) FrequencyTotal = FrequencyTotal + FrequencyChange*1;
if (SwitchP2_1) FrequencyTotal = FrequencyTotal + FrequencyChange*2;
if (SwitchP2_2) FrequencyTotal = FrequencyTotal + FrequencyChange*4;
if (SwitchP2_3) FrequencyTotal = FrequencyTotal + FrequencyChange*8;*/
FrequencyTotal = FrequencyTotal + FrequencyChange*(P2IN&0x0F);
//Make sound proportional to switches
beep(FrequencyTotal, 100);
// Resets frequency total
FrequencyTotal = 0;
}//end of if
else
{
//all the swithes are zero
march();
} // end of else
// large delay between reading of switches
delay_ms(2000);
} //end of while loop
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.