I am working with a TM4C lunchpad and need to assign Ports E and B as inputs and outputs to be able to control a simple traffic light built on a breadboard. There are three LEDs on the board as well as two buttons: when btn1 is pressed, the LEDs on alternate between red - green - yellow - back to red, when btn2 is pressed, the current LED that is on begins to flash, when both are pressed, the LEDs cycle. I've tried using other ports and have had success (that is how I know it is not a hardware issue), but the specified requirements are for Ports B[2:0] as outputs and E[1:0] as inputs, and I can't see what I'm doing wrong in the code. When I upload the code to the board, the only functioning component is the red led on the breadboard (which is the initial state of the FSM), but clicking either button does nothing. Note: Port B[2:0] is connected to the anode of it's respective LED (2 - red, 1 - yellow, 0 - green) and the cathode of each is connected to it's own resistor which is connected to ground.
#include <stdint.h>
#include "tm4c123gh6pm.h"
// ***** 1. Assign switches *****
#define LIGHT (*((volatile unsigned long *)0x4000501C)) // Port B Pins 3, 2, 1
// Port B base: 4000.5000 + x0004 (bit 0: 4*(2^0)) + x0008 (bit 1: 4*(2^1)) = x 0010 (bit 2: 4*(2^2))
#define SENSOR (*((volatile unsigned long *)0x4002400C)) // Port E Pins 0 and 1
// Port E base: 4002.4000 + x0004 (bit 0: 4*(2^0)) + x0008 (bit 1: 4*(2^1))
// ***** 2. Pre-processor Directives Section *****
//Port B used for LEDs
#define GPIO_PORTB_DATA_R (*((volatile unsigned long *)0x400053FC))
#define GPIO_PORTB_DIR_R (*((volatile unsigned long *)0x40005400))
#define GPIO_PORTB_AFSEL_R (*((volatile unsigned long *)0x40005420))
#define GPIO_PORTB_DEN_R (*((volatile unsigned long *)0x4000551C))
#define GPIO_PORTB_AMSEL_R (*((volatile unsigned long *)0x40005528))
#define GPIO_PORTB_PCTL_R (*((volatile unsigned long *)0x4000552C))
//Port E used for switches
#define GPIO_PORTE_DATA_R (*((volatile unsigned long *)0x400243FC))
#define GPIO_PORTE_DIR_R (*((volatile unsigned long *)0x40024400))
#define GPIO_PORTE_AFSEL_R (*((volatile unsigned long *)0x40024420))
#define GPIO_PORTE_DEN_R (*((volatile unsigned long *)0x4002451C))
#define GPIO_PORTE_AMSEL_R (*((volatile unsigned long *)0x40024528))
#define GPIO_PORTE_PCTL_R (*((volatile unsigned long *)0x4002452C))
#define SYSCTL_RCGC2_R (*((volatile unsigned long *)0x400FE108))
// ***** 3. Global Declarations Section *****
// FUNCTION PROTOTYPES: Each subroutine defined
void PortB_Init(void);
void PortE_Init(void);
void DisableInterrupts(void); // Disable interrupts
void EnableInterrupts(void); // Enable interrupts
void Delay(unsigned int amp);
// ***** 4. Subroutines Section *****
// Data structure for FSM
struct State {
uint32_t Out;
uint32_t Time;
uint32_t Next[4]; // 2 inputs, four combinations
};
typedef const struct State STyp;
#define rOn 0
#define rOff 1
#define yOn 2
#define yOff 3
#define gOn 4
#define gOff 5
STyp FSM[6]={
// 00 01 10 11
{0x04,100, {rOn,gOn,rOff,rOff}}, // State rOn | 0x08 = PortB Pin 2 ON
{0x00,100, {rOn,rOn,rOn, gOn}}, // State rOff
{0x02,100, {yOn,rOn,yOff,yOff}}, // State yOn | 0x02 = PortB Pin 1 ON
{0x00,100, {yOn,yOn,yOn, rOn}}, // State yOff
{0x01,100, {gOn,yOn,gOff,gOff}}, // state gOn | 0x40 = PortB Pin 0 ON
{0x00,100, {gOn,gOn,gOn, yOn}} // state gOff
};
int main(void){
uint8_t cs; // Index to the current state
uint8_t Input; // Index to the inputs
// Initialize GPIO on Ports B, E
PortB_Init(); // Call initialization of port PB0 PB1 PB2
PortE_Init(); // Call initialization of port PE0 PE1
EnableInterrupts(); // The grader uses interrupts
// Initial state: Red LED lit
// Set delay for 100ms
Delay(10);
cs = rOn; // Assign current state to be red on
while(1){
LIGHT = FSM[cs].Out;
Delay(FSM[cs].Time);
Input = SENSOR >> 2;
cs = FSM[cs].Next[Input];
}
}
void PortE_Init(void){ volatile unsigned long delay; // Port E INPUT
SYSCTL_RCGC2_R |= 0x00000010; // E clock
delay = SYSCTL_RCGC2_R; // delay
GPIO_PORTE_AMSEL_R &= ~0x03; // disable analog function
GPIO_PORTE_PCTL_R &= ~0x000000FF; // GPIO clear PCTL bits for PE1 and PE0
GPIO_PORTE_DIR_R &= ~0x03; // PE1 and PE0 (turn off inputs)
GPIO_PORTE_AFSEL_R &= ~0x03; // no alternate function
GPIO_PORTE_DEN_R |= 0x03; // enable digital pins PE1 and PE0
}
void PortB_Init(void){ volatile unsigned long delay; // Port B OUTPUT
SYSCTL_RCGC2_R |= 0x00000002; // B clock
delay = SYSCTL_RCGC2_R; // delay
GPIO_PORTB_AMSEL_R &= ~0x07; // disable analog function
GPIO_PORTB_PCTL_R &= ~0x00000FFF; // GPIO clear PCTL bits for PB2, PB1, and PB0
GPIO_PORTB_DIR_R |= 0x07; // PB2, PB1, and PB0 (turn on/SET outputs)
GPIO_PORTB_AFSEL_R &= ~0x07; // no alternate function
GPIO_PORTB_DEN_R |= 0x07; // enable digital pins PB2, PB1, and PB0
}
void Delay(unsigned int amp) {
volatile uint32_t time;
time = ((727240*200/91)*2/1000) * amp;
while(time) {
time--;
}
}
User contributions licensed under CC BY-SA 3.0