// ***** 0. Documentation Section ***** // TableTrafficLight.c for Lab 10 // Runs on LM4F120/TM4C123 // Index implementation of a Moore finite state machine to operate a traffic light. // Daniel Valvano, Jonathan Valvano // November 7, 2013 // east/west red light connected to PB5 // east/west yellow light connected to PB4 // east/west green light connected to PB3 // north/south facing red light connected to PB2 // north/south facing yellow light connected to PB1 // north/south facing green light connected to PB0 // pedestrian detector connected to PE2 (1=pedestrian present) // north/south car detector connected to PE1 (1=car present) // east/west car detector connected to PE0 (1=car present) // "walk" light connected to PF3 (built-in green LED) // "don't walk" light connected to PF1 (built-in red LED) // ***** 1. Pre-processor Directives Section ***** #include "TExaS.h" #include "tm4c123gh6pm.h" // ***** 2. Global Declarations Section ***** // FUNCTION PROTOTYPES: Each subroutine defined void DisableInterrupts(void); // Disable interrupts void EnableInterrupts(void); // Enable interrupts // ***** 3. Subroutines Section ***** #define NVIC_ST_CTRL_R (*((volatile unsigned long *)0xE000E010)) #define NVIC_ST_RELOAD_R (*((volatile unsigned long *)0xE000E014)) #define NVIC_ST_CURRENT_R (*((volatile unsigned long *)0xE000E018)) #define PF31 (*((volatile unsigned long *)0x40025028)) #define WalkLights (*((volatile unsigned long *)0x40025028)) #define PB543210 (*((volatile unsigned long *)0x400050FC)) #define CarLights (*((volatile unsigned long *)0x400050FC)) #define PE210 (*((volatile unsigned long *)0x4002401C)) #define Sensors (*((volatile unsigned long *)0x4002401C)) struct State { unsigned long TrafficOut; unsigned long WalkOut; unsigned long Time; unsigned long Next[8]; }; typedef const struct State STyp; #define GoN 0 #define WaitN 1 #define GoE 2 #define WaitE 3 #define Walk 4 #define DontWalk 5 #define WaitWalkOn1 6 #define WaitWalkOff1 7 #define WaitWalkOn2 8 #define WaitWalkOff2 9 #define WaitWalkOn3 10 #define WaitWalkOff3 11 STyp FSM[12]={ {0x21,0x02, 10, { GoN, WaitN, WaitN, WaitN, GoN, WaitN, WaitN, WaitN}}, {0x22,0x02, 10, { GoN, Walk, GoE, Walk, GoN, Walk, GoE, Walk}}, {0x0C,0x02, 10, { GoE, WaitE, GoE, WaitE, WaitE, WaitE, WaitE, WaitE}}, {0x14,0x02, 10, { GoE, Walk, GoE, Walk, GoN, Walk, GoN, Walk}}, {0x24,0x08, 10, { Walk, Walk, WaitWalkOn1, WaitWalkOn1, WaitWalkOn1, WaitWalkOn1, WaitWalkOn1, WaitWalkOn1}}, {0x24,0x02, 10, { Walk, Walk, GoE, GoE, GoN, GoN, GoN, GoE}}, {0x24,0x02, 5, {WaitWalkOff1, Walk, WaitWalkOff1, WaitWalkOff1, WaitWalkOff1, WaitWalkOff1, WaitWalkOff1, WaitWalkOff1}}, {0x24,0x00, 5, {WaitWalkOn2, Walk, WaitWalkOn2, WaitWalkOn2, WaitWalkOn2, WaitWalkOn2, WaitWalkOn2, WaitWalkOn2}}, {0x24,0x02, 5, {WaitWalkOff2, Walk, WaitWalkOff2, WaitWalkOff2, WaitWalkOff2, WaitWalkOff2, WaitWalkOff2, WaitWalkOff2}}, {0x24,0x00, 5, {WaitWalkOn3, Walk, WaitWalkOn3, WaitWalkOn3, WaitWalkOn3,WaitWalkOn3,WaitWalkOn3, WaitWalkOn3}}, {0x24,0x02, 5, {WaitWalkOff3, Walk, WaitWalkOff3, WaitWalkOff3, WaitWalkOff3, WaitWalkOff3, WaitWalkOff3, WaitWalkOff3}}, {0x24,0x00, 5, {Walk, Walk, DontWalk,DontWalk,DontWalk,DontWalk,DontWalk,DontWalk}} }; void SysTick_Init(void){ NVIC_ST_CTRL_R = 0; // disable SysTick during setup NVIC_ST_CTRL_R = 0x00000005; // enable SysTick with core clock } // The delay parameter is in units of the 80 MHz core clock. (12.5 ns) void SysTick_Wait(unsigned long delay){ NVIC_ST_RELOAD_R = delay-1; // number of counts to wait NVIC_ST_CURRENT_R = 0; // any value written to CURRENT clears while((NVIC_ST_CTRL_R&0x00010000)==0){ // wait for count flag } } // 800000*12.5ns equals 10ms void SysTick_Wait10ms(unsigned long delay){ unsigned long i; for(i=0; i