summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--main.c86
-rw-r--r--tivaware.h2
-rw-r--r--trafficlight.c122
-rw-r--r--trafficlight.h10
4 files changed, 128 insertions, 92 deletions
diff --git a/main.c b/main.c
index cf3c931..bf8916d 100644
--- a/main.c
+++ b/main.c
@@ -3,90 +3,14 @@
uint32_t period_1 = RED_PERIOD;
uint32_t period_2 = GREEN_PERIOD;
uint32_t period_3 = RED_PERIOD;
+bool switch_pressed = false;
-Traffic tf1 = {RED , GPIO_PORTF_BASE, 1, 2, 3, 0};
-Traffic tf2 = {YELLOW, GPIO_PORTA_BASE, 2, 3, 4, 0};
-Traffic tf_ped = {RED , GPIO_PORTB_BASE, 5, 0, 1, 0};
-
-void Switch_Handler() {
-
-}
-
-void Timer0_Handler() {
- TimerIntClear(TIMER0_BASE, TIMER_BOTH);
- switch(tf1.cur_color) {
- case RED:
- tf1.cur_color = GREEN;
- set_tf_color(tf1, GREEN);
- TimerLoadSet(TIMER0_BASE, TIMER_BOTH, GREEN_PERIOD);
- break;
- case YELLOW:
- tf1.cur_color = RED;
- set_tf_color(tf1, RED);
- TimerLoadSet(TIMER0_BASE, TIMER_BOTH, RED_PERIOD);
- break;
- case GREEN:
- tf1.cur_color = YELLOW;
- set_tf_color(tf1, YELLOW);
- TimerLoadSet(TIMER0_BASE, TIMER_BOTH, YELLOW_PERIOD);
- break;
- }
-}
-
-void Timer1_Handler() {
- TimerIntClear(TIMER1_BASE, TIMER_BOTH);
- switch(tf2.cur_color) {
- case RED:
- tf2.cur_color = GREEN;
- set_tf_color(tf2, GREEN);
- TimerLoadSet(TIMER1_BASE, TIMER_BOTH, GREEN_PERIOD);
- break;
- case YELLOW:
- tf2.cur_color = RED;
- set_tf_color(tf2, RED);
- TimerLoadSet(TIMER1_BASE, TIMER_BOTH, RED_PERIOD);
- break;
- case GREEN:
- tf2.cur_color = YELLOW;
- set_tf_color(tf2, YELLOW);
- TimerLoadSet(TIMER1_BASE, TIMER_BOTH, YELLOW_PERIOD);
- break;
- }
-}
-
-void Timer2_Handler() {
- TimerIntClear(TIMER2_BASE, TIMER_BOTH);
- switch(tf_ped.cur_color) {
- case RED:
- tf_ped.cur_color = GREEN;
- set_tf_color(tf_ped, GREEN);
- TimerLoadSet(TIMER2_BASE, TIMER_BOTH, GREEN_PERIOD);
- break;
- case YELLOW:
- tf_ped.cur_color = RED;
- set_tf_color(tf_ped, RED);
- TimerLoadSet(TIMER2_BASE, TIMER_BOTH, RED_PERIOD);
- break;
- case GREEN:
- tf_ped.cur_color = YELLOW;
- set_tf_color(tf_ped, YELLOW);
- TimerLoadSet(TIMER2_BASE, TIMER_BOTH, YELLOW_PERIOD);
- break;
- }
-}
+Traffic tf1 = {YELLOW, GPIO_PORTA_BASE, 2, 3, 4, 0};
+Traffic tf2 = {RED , GPIO_PORTF_BASE, 1, 2, 3, 0};
+Traffic tf_ped = {RED , GPIO_PORTB_BASE, 5, 4, 1, 0};
int main()
{
- PortInit(GPIO_PORTF_BASE, SYSCTL_PERIPH_GPIOF); // Initialize portf
- PortInit(GPIO_PORTA_BASE, SYSCTL_PERIPH_GPIOA); // Initialize porta
- PortInit(GPIO_PORTB_BASE, SYSCTL_PERIPH_GPIOB); // Initialize portb
- __asm("CPSID I"); // Disable all interrupts
- TimerInit(TIMER0_BASE, Timer0_Handler, SYSCTL_PERIPH_TIMER0, period_1); // Intialize Timer0 with period_1
- TimerInit(TIMER1_BASE, Timer1_Handler, SYSCTL_PERIPH_TIMER1, period_2); // Intialize Timer1 with period_2
- TimerInit(TIMER2_BASE, Timer2_Handler, SYSCTL_PERIPH_TIMER2, period_3); // Intialize Timer1 with period_2
- __asm("CPSIE I"); // Enable all interrupts
- while(1) {
- __asm("wfi"); // power saving mode
- }
+ TrafficInit();
}
diff --git a/tivaware.h b/tivaware.h
index ce3d029..7ee26ab 100644
--- a/tivaware.h
+++ b/tivaware.h
@@ -2,11 +2,13 @@
#include <stdbool.h>
#include "tm4c123gh6pm.h"
#include "../../ti/TivaWare_C_Series-2.2.0.295/inc/hw_memmap.h"
+#include "../../ti/TivaWare_C_Series-2.2.0.295/inc/hw_ints.h"
#include "../../ti/TivaWare_C_Series-2.2.0.295/driverlib/debug.h"
#include "../../ti/TivaWare_C_Series-2.2.0.295/driverlib/gpio.h"
#include "../../ti/TivaWare_C_Series-2.2.0.295/driverlib/sysctl.h"
#include "../../ti/TivaWare_C_Series-2.2.0.295/driverlib/systick.h"
#include "../../ti/TivaWare_C_Series-2.2.0.295/driverlib/timer.h"
+#include "../../ti/TivaWare_C_Series-2.2.0.295/driverlib/interrupt.h"
#define RIGHT_SWITCH !(GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_0) != 0)
#define LEFT_SWITCH !(GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_4) != 0)
diff --git a/trafficlight.c b/trafficlight.c
index eff06fb..9adf380 100644
--- a/trafficlight.c
+++ b/trafficlight.c
@@ -1,8 +1,7 @@
#include "trafficlight.h"
-
void set_tf_color(Traffic tf, uint8_t color) {
- uint32_t pins = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6;
+ uint32_t pins = 0xFF;
switch (color) {
case RED:
GPIOPinWrite(tf.port, pins, (1 << tf.red));
@@ -16,21 +15,18 @@ void set_tf_color(Traffic tf, uint8_t color) {
}
}
-void PortInit(uint32_t port, uint32_t clk) {
- uint32_t pins = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6;
+void PortInit(uint32_t port, uint32_t clk, uint32_t input, uint32_t output) {
+ uint32_t pins = 0xFF;
SysCtlPeripheralEnable(clk);
while(!SysCtlPeripheralReady(clk)) {};
GPIOUnlockPin(port, pins);
- // GPIOPinTypeGPIOInput(port, GPIO_PIN_0);
- GPIOPinTypeGPIOOutput(port, pins);
- GPIOPadConfigSet (port, GPIO_PIN_0 | GPIO_PIN_4 | GPIO_PIN_5,
+ GPIOPinTypeGPIOInput(port, input);
+ GPIOPinTypeGPIOOutput(port, output);
+ GPIOPadConfigSet (port, pins,
GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_STD_WPU);
}
-
-
void TimerInit(uint32_t timer, void(*timer_handler)(), uint32_t clk, uint32_t delay) {
- reset_color();
SysCtlPeripheralEnable(clk);
while(!SysCtlPeripheralReady(clk)) {}
TimerDisable(timer, TIMER_BOTH);
@@ -42,4 +38,110 @@ void TimerInit(uint32_t timer, void(*timer_handler)(), uint32_t clk, uint32_t de
TimerIntEnable(timer, TIMER_TIMA_TIMEOUT | TIMER_TIMB_TIMEOUT);
}
+void Timer0_Handler() {
+ TimerIntClear(TIMER0_BASE, TIMER_BOTH);
+ if(switch_pressed) {
+ set_tf_color(tf1, RED);
+ } else {
+ switch(tf1.cur_color) {
+ case RED:
+ tf1.cur_color = GREEN;
+ set_tf_color(tf1, GREEN);
+ TimerLoadSet(TIMER0_BASE, TIMER_BOTH, GREEN_PERIOD);
+ break;
+ case YELLOW:
+ tf1.cur_color = RED;
+ set_tf_color(tf1, RED);
+ TimerLoadSet(TIMER0_BASE, TIMER_BOTH, RED_PERIOD);
+ break;
+ case GREEN:
+ tf1.cur_color = YELLOW;
+ set_tf_color(tf1, YELLOW);
+ TimerLoadSet(TIMER0_BASE, TIMER_BOTH, YELLOW_PERIOD);
+ break;
+ }
+ }
+}
+
+void Timer1_Handler() {
+ TimerIntClear(TIMER1_BASE, TIMER_BOTH);
+ if(switch_pressed) {
+ set_tf_color(tf2, RED);
+ } else {
+ switch(tf2.cur_color) {
+ case RED:
+ tf2.cur_color = GREEN;
+ set_tf_color(tf2, GREEN);
+ TimerLoadSet(TIMER1_BASE, TIMER_BOTH, GREEN_PERIOD);
+ break;
+ case YELLOW:
+ tf2.cur_color = RED;
+ set_tf_color(tf2, RED);
+ TimerLoadSet(TIMER1_BASE, TIMER_BOTH, RED_PERIOD);
+ break;
+ case GREEN:
+ tf2.cur_color = YELLOW;
+ set_tf_color(tf2, YELLOW);
+ TimerLoadSet(TIMER1_BASE, TIMER_BOTH, YELLOW_PERIOD);
+ break;
+ }
+
+ }
+}
+
+void Timer2_Handler() {
+ TimerIntClear(TIMER2_BASE, TIMER_BOTH);
+ switch(tf_ped.cur_color) {
+ case RED:
+ tf_ped.cur_color = GREEN;
+ set_tf_color(tf_ped, GREEN);
+ TimerLoadSet(TIMER2_BASE, TIMER_BOTH, GREEN_PERIOD);
+ break;
+ case YELLOW:
+ set_tf_color(tf1, tf1.cur_color);
+ set_tf_color(tf2, tf2.cur_color);
+ set_tf_color(tf_ped, RED);
+ TimerEnable(TIMER0_BASE, TIMER_BOTH);
+ TimerEnable(TIMER1_BASE, TIMER_BOTH);
+ switch_pressed = false;
+ break;
+ case GREEN:
+ tf_ped.cur_color = YELLOW;
+ set_tf_color(tf_ped, YELLOW);
+ TimerLoadSet(TIMER2_BASE, TIMER_BOTH, YELLOW_PERIOD);
+ break;
+ }
+}
+
+void Switch_Handler() {
+ GPIOIntClear(GPIO_PORTF_BASE, GPIO_PIN_0);
+ switch_pressed = true;
+ tf1.prevPeriod = TimerValueGet(tf1.port, TIMER_A);
+ tf2.prevPeriod = TimerValueGet(tf2.port, TIMER_A);
+ TimerDisable(TIMER0_BASE, TIMER_BOTH);
+ TimerDisable(TIMER1_BASE, TIMER_BOTH);
+ set_tf_color(tf1, RED);
+ set_tf_color(tf2, RED);
+ set_tf_color(tf_ped, GREEN);
+ tf_ped.cur_color = RED;
+ TimerInit(TIMER2_BASE, Timer2_Handler, SYSCTL_PERIPH_TIMER2, GREEN_PERIOD); // Intialize Timer2 with period_3
+}
+void TrafficInit() {
+ PortInit(GPIO_PORTF_BASE, SYSCTL_PERIPH_GPIOF, 0x1, 0x7E); // Initialize portf
+ PortInit(GPIO_PORTA_BASE, SYSCTL_PERIPH_GPIOA, 0x0, 0xFF); // Initialize porta
+ PortInit(GPIO_PORTB_BASE, SYSCTL_PERIPH_GPIOB, 0x0, 0xFF); // Initialize portb
+ __asm("CPSID I"); // Disable all interrupts
+ GPIOIntUnregister(GPIO_PORTF_BASE);
+ GPIOIntRegister(GPIO_PORTF_BASE, Switch_Handler);
+ GPIOIntEnable(GPIO_PORTF_BASE, GPIO_INT_PIN_0);
+ GPIOIntTypeSet(GPIO_PORTF_BASE, GPIO_PIN_0, GPIO_RISING_EDGE);
+ IntEnable(INT_GPIOF_TM4C123);
+ IntMasterEnable();
+ TimerInit(TIMER0_BASE, Timer0_Handler, SYSCTL_PERIPH_TIMER0, period_1); // Intialize Timer0 with period_1
+ TimerInit(TIMER1_BASE, Timer1_Handler, SYSCTL_PERIPH_TIMER1, period_2); // Intialize Timer1 with period_2
+ __asm("CPSIE I"); // Enable all interrupts
+ while(1) {
+ __asm("wfi"); // power saving mode
+ }
+}
diff --git a/trafficlight.h b/trafficlight.h
index 6d86982..510b89c 100644
--- a/trafficlight.h
+++ b/trafficlight.h
@@ -1,5 +1,6 @@
#include <stdint.h>
#include <stdbool.h>
+#include <stdlib.h>
#include "tivaware.h"
#define TIVA_CLK 16e6
@@ -22,7 +23,14 @@ typedef struct Traffic{
extern Traffic tf1;
extern Traffic tf2;
+extern Traffic tf_ped;
+extern bool switch_pressed;
+extern uint32_t period_1;
+extern uint32_t period_2;
+extern uint32_t period_3;
+void TrafficInit();
void set_tf_color(Traffic tf, uint8_t color);
-void PortInit(uint32_t port, uint32_t clk);
+void PortInit(uint32_t port, uint32_t clk, uint32_t input, uint32_t output);
void TimerInit(uint32_t timer, void(*timer_handler)(), uint32_t clk, uint32_t delay);
+