In: Electrical Engineering
using code composer studio 1. write a program for the msp430fr6989 that will light LED1, whenever S1 is pressed. It should turn off whenever S1 is released.
2. Write a program that will make LED1 blink 5 times when S1 is pressed, and then stop.
1)
#include <msp430fr6989.h>
#define LED_0 BIT0 #define LED_OUT P1OUT #define LED_DIR P1DIR #define BUTTON BIT3
unsigned int blink = 0;
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer LED_DIR |= (LED_0); // Set P1.0 to output direction LED_OUT &= ~(LED_0); // Set the LEDs off
P1REN |= BUTTON; // puller-Resistor on the button-pin is enabled P1OUT |= BUTTON; //Writes a "1" to the portpin P1IES |= BUTTON; //Triggers when you PRESS the button
P1IE |= BUTTON;
__enable_interrupt(); // Interrupts get enabled
for (;;)
{
if(blink > 0)
{
P1OUT ^= (LED_0); // on P1.0
}
}
}
// Port 1 interrupt service routine
#pragma vector=PORT1_VECTOR
__interrupt void Port_1(void)
{
blink ^= 0x01;
P1IFG &= ~BUTTON; // P1.3 IFG cleared
LED_OUT &= ~(LED_0); // Clear the LED
}
2) Write a program that will make LED1 blink 5 times when S1 is pressed, and then stop.
#include <msp430fr6989.h>
#define LED_0 BIT0 #define LED_OUT P1OUT #define LED_DIR P1DIR #define BUTTON BIT3
unsigned int blink = 0;
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer LED_DIR |= (LED_0); // Set P1.0 to output direction LED_OUT &= ~(LED_0); // Set the LEDs off
P1REN |= BUTTON; // puller-Resistor on the button-pin is enabled P1OUT |= BUTTON; //Writes a "1" to the portpin P1IES |= BUTTON; //Triggers when you PRESS the button
P1IE |= BUTTON;
__enable_interrupt(); // Interrupts get enabled
for (;;)
{
if(blink > 0)
{
P1OUT ^= (LED_0); // on P1.0
__delay_cycles(10000)
LED_OUT &= ~(LED_0)
P1OUT ^= (LED_0); // on P1.0
__delay_cycles(10000)
LED_OUT &= ~(LED_0)
P1OUT ^= (LED_0); // on P1.0
__delay_cycles(10000)
LED_OUT &= ~(LED_0)
P1OUT ^= (LED_0); // on P1.0
__delay_cycles(10000)
LED_OUT &= ~(LED_0)
P1OUT ^= (LED_0); // on P1.0
__delay_cycles(10000)
LED_OUT &= ~(LED_0)
}
}
}
// Port 1 interrupt service routine
#pragma vector=PORT1_VECTOR
__interrupt void Port_1(void)
{
blink ^= 0x01;
P1IFG &= ~BUTTON; // P1.3 IFG cleared
LED_OUT &= ~(LED_0); // Clear the LED
}