Lab 3
Modify Loop
9. Modify the while loop to light LED when SW1 push button is pressed.
Comment out (or delete) LED blinking code and replace it with the code we created in the
Worksheet question #4 (page 3-29).
At this point, your main.c file should look similar to this:
// --------------------------------------------------------------------
// main.c (for lab_03b_button project)
// --------------------------------------------------------------------
//***** Header Files **************************************************
#include <driverlib.h>
//***** Defines *******************************************************
#define ONE_SECOND 800000
#define HALF_SECOND 400000
//***** Global Variables **********************************************
volatile unsigned short usiButton1 = 0;
//***** Functions *****************************************************
void main (void)
{
// Stop watchdog timer
WDT_A_hold( WDT_A_BASE );
// Set P1.0 to output direction, P2.1 as input with pullup resistor
GPIO_setAsOutputPin( GPIO_PORT_P1, GPIO_PIN0 );
GPIO_setAsInputPinWithPullUpresistor( GPIO_PORT_P2, GPIO_PIN1 );
while(1) {
// Read P2.1 pin connected to push button 1
usiButton1 = GPIO_getInputPinValue ( GPIO_PORT_P2,
GPIO_PIN1 );
if ( usiButton1 == GPIO_INPUT_PIN_LOW ) {
// If button is down, turn on LED
GPIO_setOutputHighOnPin( GPIO_PORT_P1, GPIO_PIN0 );
}
else {
// If button is up, turn off LED
GPIO_setOutputLowOnPin( GPIO_PORT_P1, GPIO_PIN0 );
}
}
}
Hint: If you want to minimize your typing errors, you can copy/paste the code from the listing above.
We have also placed a copy of this code into the lab’s readme file (in the lab folder); just in
case the copy/paste doesn’t work well from the PDF file.
Copying from PDF will usually mess up the code’s indentation. You can fix this by selecting the
code inside CCSv5 and telling it to clean-up indentation:
Right-click → Source → Correct Indentation (Ctrl+I)
MSP430 Workshop - Using GPIO with MSP430ware 3 - 33