I'm trying to write the C code that will program my board to function as follows: the on-board LEDs will alternate between 1) red, 2) blue, 3) green, 4) cycle red-blue-green. This alternation occurs when SW1 is pressed. So if SW1 is pressed the LED turns red and toggles. If it is pressed again it turns blue and toggles, and so on. For testing purposes, I have the code set-up to alternate between 1), 2), and 3) and back with each press of SW1. The issue I am having is that whenever I press the button, nothing happens; the LED is stuck at red and I can't see what the issue is.
// 1. Pre-processor Directives Section
// Constant declarations to access port registers using
// symbolic names instead of addresses
#include "TExaS.h"
#define GPIO_PORTF_DATA_R (*((volatile unsigned long *)0x400253FC))
#define GPIO_PORTF_DIR_R (*((volatile unsigned long *)0x40025400))
#define GPIO_PORTF_AFSEL_R (*((volatile unsigned long *)0x40025420))
#define GPIO_PORTF_PUR_R (*((volatile unsigned long *)0x40025510))
#define GPIO_PORTF_DEN_R (*((volatile unsigned long *)0x4002551C))
#define GPIO_PORTF_LOCK_R (*((volatile unsigned long *)0x40025520))
#define GPIO_PORTF_CR_R (*((volatile unsigned long *)0x40025524))
#define GPIO_PORTF_AMSEL_R (*((volatile unsigned long *)0x40025528))
#define GPIO_PORTF_PCTL_R (*((volatile unsigned long *)0x4002552C))
#define SYSCTL_RCGC2_R (*((volatile unsigned long *)0x400FE108))
// 2. Declarations Section
// Global Variables
unsigned long In; // input from PF4
unsigned long Out; // outputs to PF3,PF2,PF1 (multicolor LED)
// Function Prototypes
void PortF_Init(void);
void Delay(void);
void EnableInterrupts(void);
// 3. Subroutines Section
// MAIN: Mandatory for a C Program to be executable
int main(void){
TExaS_Init(SW_PIN_PF40,LED_PIN_PF321); // this initializes the TExaS grader lab 2
EnableInterrupts(); // The grader uses interrupts
PortF_Init(); // Call initialization of port F
GPIO_PORTF_DATA_R = 0x02; // start LED at red
while(1){
In = GPIO_PORTF_DATA_R & 0x11; // read switches into In
if(In == 0x01 && GPIO_PORTF_DATA_R == 0x02){ // 0 - SW1 is pressed, 1 - SW2 not pressed | LED == red
GPIO_PORTF_DATA_R = 0x04; // change LED to blue
}
else if(In == 0x01 && GPIO_PORTF_DATA_R == 0x04){ // SW1 is pressed and LED is blue
GPIO_PORTF_DATA_R = 0x08; // change LED to green
}
else if(In == 0x01 && GPIO_PORTF_DATA_R == 0x08){ // SW1 is pressed and LED is green
GPIO_PORTF_DATA_R = 0x02; // LED returns to red
}
}
}
// Subroutine to initialize port F pins for input and output
// PF4 is input SW1
// PF3,PF2,PF1 are outputs to the LED
// Notes: These five pins are connected to hardware on the LaunchPad
void PortF_Init(void){ volatile unsigned long delay;
SYSCTL_RCGC2_R |= 0x00000020; // enable F's clock
delay = SYSCTL_RCGC2_R; // delay
GPIO_PORTF_LOCK_R = 0x4C4F434B; // unlock PortF PF0
GPIO_PORTF_CR_R = 0x1F; // allow changes to PF4-0
GPIO_PORTF_AMSEL_R &= ~0x1F; // disable analog function
GPIO_PORTF_PCTL_R &= ~0x000FFFFF; // GPIO clear bit PCTL
GPIO_PORTF_DIR_R |= 0x0E; // enable PF3,PF2,PF1 output
GPIO_PORTF_DIR_R &= ~0x11; // disable PF4, PF0 input
GPIO_PORTF_AFSEL_R &= ~0x1F; // no alternate function
GPIO_PORTF_PUR_R = 0x11; // enable pullup resistors on PF4, PF0
GPIO_PORTF_DEN_R |= 0x1F; // enable digital pins PF4-PF0
}
// Color LED(s) PortF
// dark --- 0
// red R-- 0x02
// blue --B 0x04
// green -G- 0x08
// yellow RG- 0x0A
// sky blue -GB 0x0C
// white RGB 0x0E
// pink R-B 0x06
// Subroutine to wait 0.1 sec
// Inputs: None
// Outputs: None
// Notes: ...
void Delay(void){unsigned long volatile time;
time = 727240*200/91; // 0.1sec
while(time){
time--;
}
}