/*
Header: // for #include (see below) and also add file to folder
PLL.h
SysTick.h
Files: // add into Microvision and Folder
PLL.c
SysTick.c
startup.s
*/
#include <stdint.h>
#include "PLL.h"
#include "SysTick.h"
// for volatile... values, go to bottom of the page for explanation
#define LIGHT (*((volatile unsigned long *)0x400050FC))
// base & 40005000 (PORTB)
#define PLIGHT (*((volatile unsigned long *)0x4002503C)) // base & 40025000 (PORTF)
#define GPIO_PORTB_OUT (*((volatile unsigned long *)0x400050FC)) // bits 5-0
#define GPIO_PORTF_OUT (*((volatile unsigned long *)0x40025028))
#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))
#define GPIO_PORTE_IN (*((volatile unsigned long *)0x4002401C)) // bits 2-0
#define SENSOR (*((volatile unsigned long *)0x4002401C)) // &PE 4002400
#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))
#define GPIO_PORTF_DIR_R (*((volatile unsigned long *)0x40025400)) // additional port, F
#define GPIO_PORTF_AFSEL_R (*((volatile unsigned long *)0x40025420))
#define GPIO_PORTF_DEN_R (*((volatile unsigned long *)0x4002551C))
#define GPIO_PORTF_AMSEL_R (*((volatile unsigned long *)0x40025528))
#define GPIO_PORTF_PCTL_R (*((volatile unsigned long *)0x4002552C))
#define GPIO_PORTF_PUR_R (*((volatile unsigned long *)0x40025028))
#define SYSCTL_RCGC2_GPIOF 0x00000020 // port F Clock Gating Control
#define SYSCTL_RCGC2_GPIOE 0x00000010 // port E Clock Gating Control
#define SYSCTL_RCGC2_GPIOB 0x00000002 // port B Clock Gating Control
// Linked data structure
struct State {
unsigned long Out;
unsigned long Pout;
unsigned long Time;
unsigned long Next[8];
};
typedef const struct State STyp;
#define goS 0
#define CautionS 1
#define goW 2
#define CautionW 3
#define PwaitS 4
#define PgoS 5
#define PblinkONS1 6
#define PblinkOFFS1 7
#define PblinkONS2 8
#define PblinkOFFS2 9
#define PwaitW 10
#define PgoW 11
#define PblinkONW1 12
#define PblinkOFFW1 13
#define PblinkONW2 14
#define PblinkOFFW2 15
STyp FSM[16]={ // each line, different states
{0x21, 0x02, 550,{goS,CautionS,goS,CautionS,PwaitS,PwaitS,PwaitS,PwaitS}},
{0x22, 0x02, 550,{goW,goW,goW,goW,goW,goW,goW,goW}},
{0x0C, 0x02, 550,{goW,goW,CautionW,CautionW,PwaitW,PwaitW,PwaitW,PwaitW}},
{0x14, 0x02, 550,{goS,goS,goS,goS,goS,goS,goS,goS}},
{0x22, 0x02, 550,{PgoS,PgoS,PgoS,PgoS,PgoS,PgoS,PgoS}},
{0x24, 0x08, 350,{PblinkONS1,PblinkONS1,PblinkONS1,PblinkONS1,PblinkONS1,PblinkONS1,PblinkONS1,PblinkONS1}},
{0x24, 0x02, 50,{PblinkOFFS1,PblinkOFFS1,PblinkOFFS1,PblinkOFFS1,PblinkOFFS1,PblinkOFFS1,PblinkOFFS1,PblinkOFFS1}},
{0x24, 0x00, 50,{PblinkONS2,PblinkONS2,PblinkONS2,PblinkONS2,PblinkONS2,PblinkONS2,PblinkONS2,PblinkONS2}},
{0x24, 0x02, 50,{PblinkOFFS2,PblinkOFFS2,PblinkOFFS2,PblinkOFFS2,PblinkOFFS2,PblinkOFFS2,PblinkOFFS2,PblinkOFFS2}},
{0x24, 0x00, 550,{goW,goW,goW,goW,goW,goW,goW,goW}},
{0x14, 0x02, 550,{PgoW,PgoW,PgoW,PgoW,PgoW,PgoW,PgoW,PgoW}},
{0x24, 0x08, 350,{PblinkONW1,PblinkONW1,PblinkONW1,PblinkONW1,PblinkONW1,PblinkONW1,PblinkONW1,PblinkONW1}},
{0x24, 0x02, 50,{PblinkOFFW1,PblinkOFFW1,PblinkOFFW1,PblinkOFFW1,PblinkOFFW1,PblinkOFFW1,PblinkOFFW1,PblinkOFFW1}},
{0x24, 0x00, 50,{PblinkONW2,PblinkONW2,PblinkONW2,PblinkONW2,PblinkONW2,PblinkONW2,PblinkONW2,PblinkONW2}},
{0x24, 0x02, 50,{PblinkOFFW2,PblinkOFFW2,PblinkOFFW2,PblinkOFFW2,PblinkOFFW2,PblinkOFFW2,PblinkOFFW2,PblinkOFFW2}},
{0x24, 0x00, 250,{goS,goS,goS,goS,goS,goS,goS,goS}}};
unsigned long S; // index to the current state
unsigned long Input;
int main(void){
volatile unsigned long delay;
PLL_Init(); // 80 MHz, Program 10.1
SysTick_Init(); // Program 10.2
SYSCTL_RCGC2_R |= 0x32; // 1) For Port B, E & F
delay = SYSCTL_RCGC2_R; // 2) no need to unlock
GPIO_PORTE_AMSEL_R &=~ 0x07; // disable analog function on PE1-0
GPIO_PORTE_AFSEL_R &=~ 0x07; // regular function on PE1-0
GPIO_PORTE_DIR_R &=~ 0x07; // inputs on PE1-0
GPIO_PORTE_DEN_R |= 0x07; // enable digital on PE1-0
GPIO_PORTE_PCTL_R &=~ 0x00000FFF; // enable regular GPIO
GPIO_PORTB_AMSEL_R &=~ 0x3F; // disable analog function on PB5-0
GPIO_PORTB_AFSEL_R &=~ 0x3F; // regular function on PB5-0
GPIO_PORTB_DIR_R |= 0x3F; // outputs on PB5-0
GPIO_PORTB_DEN_R |= 0x3F; // enable digital on PB5-0
GPIO_PORTB_PCTL_R &=~ 0x00FFFFFF; // enable regular GPIO
GPIO_PORTF_AMSEL_R &=~ 0x0A;
GPIO_PORTF_AFSEL_R &=~ 0x0A;
GPIO_PORTF_DIR_R |= 0x0A;
GPIO_PORTF_DEN_R |= 0x0A;
GPIO_PORTF_PUR_R = 0x0A;
GPIO_PORTF_PCTL_R &=~ 0x0000F0F0;
S = goS;
while(1){
LIGHT = FSM[S].Out; // set traffic lights
PLIGHT = FSM[S].Pout; //set Pedestrian lights
SysTick_Wait10ms(FSM[S].Time);
Input = SENSOR; // read sensors
S = FSM[S].Next[Input];
}
}
Defining the value for #LIGHT:
(accessing bits 0-5, see left table)
Base: 0x4000 5000
offsets: 0080
0040
+ 0020
0010
0008
0004
_________________
0x 4000 50(8+4+2+1 = 15 => F) (8+4 = 12 => C) == 0x4000 50FC
https://drive.google.com/drive/u/0/folders/1TmxG8vcH76qDIACuWfLp_aC4nEm4OfSI