In: Electrical Engineering
1.. Write a code that LED1 (Red LED- P1.0) turns ON when after you push S1(push button - P1.1) five times and stays OFF forever after that.
2.. Write a code that LED1 (Red LED- P1.0) turns OFF and LED2 (Red LED- P9.7) Turns ON after you push S1(push button -P1.1) five times and they stay that way forever.
Answer :- 1) The code has been written below-
#include <msp430g2553.h> // header file that depends upon
your variant
int main(void)
{
WDTCTL = WDTPW | WDTHOLD;// Stop watchdog timer
P1DIR = 0X40; //Declare PIN0 as output and PIN1 as input for port
one
P1OUT = 0X00; //initially make LED1 in off state
unsigned int i, cnt = 0; //Delay variable
while(1)
{
if (!(BIT1 & P1IN)) //when S1 is pressed
{
cnt++;
if(cnt == 5)
{
P1OUT = 0X01; //Make LED on after five time press
for(i=0;i<30000;i++); //Necessary delay, change it to see the
effect on toggling
P1OUT = 0x00; //make LED off forever
break; //come out of infinite loop
}
}
}
return 0;
}
Answer :- 2) The code is written below-
#include <msp430g2553.h> // header file that depends upon
your variant
int main(void)
{
WDTCTL = WDTPW | WDTHOLD;// Stop watchdog timer
P1DIR = 0x01; //Declare PIN0 as output and PIN1 as input for port
one
P4DIR = 0x80; //make port4 pin7 as output for LED2
P1OUT = 0x01; //initially make LED1 in on state
P4OUT = 0x00; //initially make LED2 in off state
unsigned int i, cnt = 0; //Delay variable
while(1)
{
if (!(BIT1 & P1IN)) //when S1 is pressed
{
cnt++;
if(cnt == 5)
{
P1OUT = 0X00; //Make LED1 off after five time press
P4OUT = 0X80; //Make LED2 on after five time press
break; //come out of infinite loop
}
}
}
return 0;
}
Note :- For problem 2, the LED2 is at P4.7 and not at P9.7 as per the datasheet. I request the student to post name of board also in further posting of questions. Thank You.