// PortableTrafficLight.c // Runs on LM3S811 // Use a table implementation of a Moore finite state machine to operate // a traffic light. This time, only the "#define" section is processor // specific, and the rest of the code can easily be adapted to another // system. // Daniel Valvano // June 16, 2011 /* This example accompanies the book "Embedded Systems: Real Time Interfacing to the Arm Cortex M3", ISBN: 978-1463590154, Jonathan Valvano, copyright (c) 2011 Copyright 2011 by Jonathan W. Valvano, valvano@mail.utexas.edu You may use, edit, run or distribute this file as long as the above copyright notice remains THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. VALVANO SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. For more information about my classes, my research, and my books, see http://users.ece.utexas.edu/~valvano/ */ // north facing car detector connected to BSP_InPort1 (1=car present) // east facing car detector connected to BSP_InPort0 (1=car present) // east facing red light connected to BSP_OutPort5 // east facing yellow light connected to BSP_OutPort4 // east facing green light connected to BSP_OutPort3 // north facing red light connected to BSP_OutPort2 // north facing yellow light connected to BSP_OutPort1 // north facing green light connected to BSP_OutPort0 #include "SysTick.h" #define BSP_InPort (*((volatile unsigned long *)0x400053FC)) #define BSP_InPort_DDR (*((volatile unsigned long *)0x40005400)) #define BSP_InPort_DEN (*((volatile unsigned long *)0x4000551C)) #define BSP_OutPort (*((volatile unsigned long *)0x400073FC)) #define BSP_OutPort_DDR (*((volatile unsigned long *)0x40007400)) #define BSP_OutPort_DEN (*((volatile unsigned long *)0x4000751C)) #define BSP_GPIO_EN (*((volatile unsigned long *)0x400FE108)) #define BSP_InPort_Mask 0x00000008 // bit mask for InPort #define BSP_OutPort_Mask 0x00000002 // bit mask for OutPort struct State { unsigned long Out; unsigned long Time; const struct State *Next[4];}; typedef const struct State STyp; #define goN &FSM[0] #define waitN &FSM[1] #define goE &FSM[2] #define waitE &FSM[3] STyp FSM[4]={ {0x21,3000,{goN,waitN,goN,waitN}}, {0x22, 500,{goE,goE,goE,goE}}, {0x0C,3000,{goE,goE,waitE,waitE}}, {0x14, 500,{goN,goN,goN,goN}}}; int main(void){ STyp *Pt; // state pointer unsigned long Input; // activate input and output ports BSP_GPIO_EN |= BSP_InPort_Mask|BSP_OutPort_Mask; SysTick_Init(); // initialize SysTick timer BSP_InPort_DDR &= ~0x03; // make BSP_InPort1-0 in BSP_InPort_DEN |= 0x03; // enable digital I/O on BSP_InPort1-0 BSP_OutPort_DDR |= 0x3F; // make BSP_OutPort5-0 out BSP_OutPort_DEN |= 0x3F; // enable digital I/O on BSP_OutPort5-0 Pt = goN; while(1){ BSP_OutPort = Pt->Out; SysTick_Wait10ms(Pt->Time); Input = BSP_InPort&0x03; Pt = Pt->Next[Input]; } }