Video Tutorial
Watch the video below and/or scroll down to get the code.
Relevant Links:
- Stellaris LM4F120H5QR Microcontroller Data Sheet
- Stellaris LM4F120 LaunchPad Evaluation Board User Manual
Digital Input With Buttons and Switches Code:
#include <lm4f120h5qr.h>
int main()
{
// General-Purpose Input/Output Run Mode Clock Gating Control (RCGCGPIO), pg. 315
SYSCTL_RCGCGPIO_R |= 0x20U; // activate the clock for port f
// GPIO Lock (GPIOLOCK), pg. 645
GPIO_PORTF_LOCK_R = 0x4C4F434BU; // unlock the lock register
// GPIO Commit (GPIOCR), pg. 646
GPIO_PORTF_CR_R = 0xFF; // enable commit for PORT F
// GPIO Analog Mode Select (GPIOASMSEL), pg. 648
GPIO_PORTF_AMSEL_R = 0x00U; // disable analog functionality
// GPIO Port Control (GPIOPCTL), pg. 649
GPIO_PORTF_PCTL_R = 0x00000000U; // configure port f as GPIO
// GPIO Direction (GPIODIR), pg. 624
GPIO_PORTF_DIR_R = 0x0EU; // 0xE = 01110 make PF0 and PF4 input, make PF3, PF2, and PF1 output
// GPIO Alternate Function Select (GPIOAFSEL), pg. 633
GPIO_PORTF_AFSEL_R = 0x00U; // disable alternate functions
// GPIO Pull-Up Select (GPIOPUR), pg. 638
GPIO_PORTF_PUR_R = 0x11U; // 0x11 = 00010001 enable pull up resistors on PF0(SW2) and PF4 (SW1)
// GPIO Digital Enable (GPIODEN), pg. 643
GPIO_PORTF_DEN_R = 0xFFU; // enable digital on all pins in PORTF
while(1)
{
GPIO_PORTF_DATA_R &= ~0x0E; // 0E = 00001110, so ~0E = 11110001 turn off the three leds
switch(GPIO_PORTF_DATA_R & 0x11U) // 0x11 = 00010001
{
case 0x00: //both switches are pressed
GPIO_PORTF_DATA_R |= 0x02; // turn on the red led
break;
case 0x01: //SW1 is pressed, SW2 is not pressed
GPIO_PORTF_DATA_R |= 0x04; // turn on the blue led
break;
case 0x10: // SW2 is pressed, SW1 is not pressed
GPIO_PORTF_DATA_R |= 0x08; // turn on the green led
break;
default: // 0x11 , neither switch is pressed
// don't do anything
break;
}
}
return 0;
}