Write a program for the MSP430 using Energia placing an interrupt on the push 2
ID: 3768745 • Letter: W
Question
Write a program for the MSP430 using Energia placing an interrupt on the push 2 button so that the first time the button is pushed the red LED should turn on for 1 second and then turn off. The second time the button is pushed the red LED should turn on for 1 second, turn off for 1 second, turn back on for 1 second, and off again. The third time the button is pushed the green LED should turn on for 1 second and then turn off. The fourth time the button is pushed the green LED should turn on for 1 second, turn off for 1 second, turn back on for 1 second, and off again. The fifth time the button is pushed the red LED should turn on for 1 second and then turn off. The sixth time the button is pushed the red LED should turn on for 1 second, turn off for 1 second, turn back on for 1 second, and off again…. (Etc. etc. etc.)
Explanation / Answer
#include <io.h>
#include <string.h>
#define RED_LED BIT0
#define GRN_LED BIT6
#define BUTTON BIT3
#define TXD BIT1
#define RXD BIT2
#ifndef CALDCO_16MHZ_
#define CALDCO_16MHZ_ 0x10F8 /* DCOCTL Calibration Data for 16MHz */
const_sfrb(CALDCO_16MHZ, CALDCO_16MHZ_);
#define CALBC1_16MHZ_ 0x10F9 /* BCSCTL1 Calibration Data for 16MHz */
const_sfrb(CALBC1_16MHZ, CALBC1_16MHZ_);
#endif
#ifndef CALDCO_12MHZ_
#define CALDCO_12MHZ_ 0x10FA /* DCOCTL Calibration Data for 12MHz */
const_sfrb(CALDCO_12MHZ, CALDCO_12MHZ_);
#define CALBC1_12MHZ_ 0x10FB /* BCSCTL1 Calibration Data for 12MHz */
const_sfrb(CALBC1_12MHZ, CALBC1_12MHZ_);
#endif
#ifndef CALDCO_8MHZ_
#define CALDCO_8MHZ_ 0x10FC /* DCOCTL Calibration Data for 8MHz */
const_sfrb(CALDCO_8MHZ, CALDCO_8MHZ_);
#define CALBC1_8MHZ_ 0x10FD /* BCSCTL1 Calibration Data for 8MHz */
const_sfrb(CALBC1_8MHZ, CALBC1_8MHZ_);
#endif
/* Ticks per bit, and ticks per half.
* 2400 bps -> 52
*/
#define TPB 52
#define TPH (TPB/2)
unsigned char *data;
unsigned int bytestosend=0;
unsigned int TXWord = 0;
unsigned int txbitcnt = 0;
/* for converting hex values to characters */
unsigned char hex[] = "0123456789ABCDEF";
/* prototypes */
unsigned char set_dco_c(unsigned int delta);
void initUart( void );
int sendString( char *str );
void report_data( void );
unsigned int calbc1_1mhz, caldco_1mhz,
calbc1_8mhz, caldco_8mhz,
calbc1_12mhz, caldco_12mhz,
calbc1_16mhz, caldco_16mhz;
/************ main program starts here ******************/
void main( void ) {
/* stop the watchdog timer */
WDTCTL = WDTPW + WDTHOLD;
/* LEDs off for now, but ready */
P1DIR |= RED_LED+GRN_LED;
P1OUT &= ~ (RED_LED + GRN_LED );
/* start from the 1Mhz calibration values */
BCSCTL1 = CALBC1_1MHZ;
DCOCTL = CALDCO_1MHZ;
/* calculate our own 1Mhz values */
if( !set_dco_c( 244 )) {
calbc1_1mhz = BCSCTL1;
caldco_1mhz = DCOCTL;
} else {
calbc1_1mhz = caldco_1mhz = 0xff;
}
/* calculate our own 8Mhz values */
if( !set_dco_c( 1953 )) {
calbc1_8mhz = BCSCTL1;
caldco_8mhz = DCOCTL;
} else {
calbc1_8mhz = caldco_8mhz = 0xff;
}
/* calculate our own 12Mhz values */
if( !set_dco_c( 2929 )) {
calbc1_12mhz = BCSCTL1;
caldco_12mhz = DCOCTL;
} else {
calbc1_12mhz = caldco_12mhz = 0xff;
}
/* calculate our own 16Mhz values */
if( !set_dco_c( 3662 )) {
calbc1_16mhz = BCSCTL1;
caldco_16mhz = DCOCTL;
} else {
calbc1_16mhz = caldco_16mhz = 0xff;
}
/* get ready to transmit to the computer */
initUart();
/* turn on red LED so we know calibration is done */
P1OUT |= RED_LED;
/* We'll use the button to let the chip know we're ready to communicate.
* Button direction is receive, clear interrupt flag
*/
P1DIR &= ~BUTTON;
P1IFG &= ~BUTTON;
while( 1 ) {
/* done -- go to sleep and wait for button interrupt to report to PC */
P1IE |= BUTTON;
__bis_SR_register( LPM3_bits + GIE );
P1OUT ^= RED_LED;
report_data();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.