From e304ce76cb2e66b3009cee4386d9de098e09ce94 Mon Sep 17 00:00:00 2001 From: Omar Magdy Date: Tue, 23 Aug 2022 15:17:50 +0200 Subject: Added a Traffic struct and made the code more generic and easy to maintain --- color.c | 4 +-- color.h | 8 +++--- main.c | 78 ++++++++++++++++++++++++++++++++++++++++------------------ trafficlight.c | 20 +++++++++++++-- trafficlight.h | 15 ++++++++++- 5 files changed, 90 insertions(+), 35 deletions(-) diff --git a/color.c b/color.c index e7e072d..297bd02 100644 --- a/color.c +++ b/color.c @@ -1,6 +1,4 @@ -#include "color.h" -#include - +#include "trafficlight.h" void set_color(uint8_t color) { uint32_t pins = GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6; diff --git a/color.h b/color.h index 9a43477..f39e986 100644 --- a/color.h +++ b/color.h @@ -1,5 +1,3 @@ -#include "tivaware.h" - #define LED_RED (1U << 1) #define LED_BLUE (1U << 2) @@ -14,12 +12,12 @@ #define YELLOW_TF3 8 #define GREEN_TF3 9 #define PORTF GPIO_PORTF_DATA_R -// #define RED 1 +#define RED 1 +#define YELLOW 2 +#define GREEN 3 // #define BLUE 2 -// #define GREEN 3 // #define MAGENTA 4 // #define CYAN 5 -// #define YELLOW 6 // #define WHITE 7 diff --git a/main.c b/main.c index 64cc188..2826b11 100644 --- a/main.c +++ b/main.c @@ -1,26 +1,33 @@ #include "trafficlight.h" -uint8_t cur_color_1 = RED_TF1; -uint8_t cur_color_2 = YELLOW_TF2; -uint32_t period_1 = RED_PERIOD; -uint32_t period_2 = GREEN_PERIOD; +uint32_t period_1 = RED_PERIOD; +uint32_t period_2 = GREEN_PERIOD; +uint32_t period_3 = RED_PERIOD; + +Traffic tf1 = {RED, GPIO_PORTF_BASE, 1, 2, 3}; +Traffic tf2 = {YELLOW, GPIO_PORTA_BASE, 2, 3, 4}; +Traffic tf_ped = {RED, GPIO_PORTB_BASE, 5, 0, 1}; + +void Switch_Handler() { + +} void Timer0_Handler() { TimerIntClear(TIMER0_BASE, TIMER_BOTH); - switch(cur_color_1) { - case RED_TF1: - cur_color_1 = GREEN_TF1; - set_color(cur_color_1); + 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_1 = RED_TF1; - set_color(cur_color_1); + case YELLOW: + tf1.cur_color = RED; + set_tf_color(tf1, RED); TimerLoadSet(TIMER0_BASE, TIMER_BOTH, RED_PERIOD); break; - case GREEN_TF1: - cur_color_1 = YELLOW_TF1; - set_color(cur_color_1); + case GREEN: + tf1.cur_color = YELLOW; + set_tf_color(tf1, YELLOW); TimerLoadSet(TIMER0_BASE, TIMER_BOTH, YELLOW_PERIOD); break; } @@ -28,33 +35,56 @@ void Timer0_Handler() { void Timer1_Handler() { TimerIntClear(TIMER1_BASE, TIMER_BOTH); - switch(cur_color_2) { - case RED_TF2: - cur_color_2 = GREEN_TF2; - set_color(cur_color_2); + 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_2 = RED_TF2; - set_color(cur_color_2); + case YELLOW: + tf2.cur_color = RED; + set_tf_color(tf2, RED); TimerLoadSet(TIMER1_BASE, TIMER_BOTH, RED_PERIOD); break; - case GREEN_TF2: - cur_color_2 = YELLOW_TF2; - set_color(cur_color_2); + 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; + } +} + 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 diff --git a/trafficlight.c b/trafficlight.c index a004d98..eff06fb 100644 --- a/trafficlight.c +++ b/trafficlight.c @@ -1,11 +1,27 @@ #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; + switch (color) { + case RED: + GPIOPinWrite(tf.port, pins, (1 << tf.red)); + break; + case YELLOW: + GPIOPinWrite(tf.port ,pins, (1 << tf.yellow)); + break; + case GREEN: + GPIOPinWrite(tf.port ,pins, (1 << tf.green)); + break; + } +} + void PortInit(uint32_t port, uint32_t clk) { - uint32_t pins = GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6; + uint32_t pins = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6; SysCtlPeripheralEnable(clk); while(!SysCtlPeripheralReady(clk)) {}; GPIOUnlockPin(port, pins); - GPIOPinTypeGPIOInput(port, GPIO_PIN_0); + // GPIOPinTypeGPIOInput(port, GPIO_PIN_0); GPIOPinTypeGPIOOutput(port, pins); GPIOPadConfigSet (port, GPIO_PIN_0 | GPIO_PIN_4 | GPIO_PIN_5, GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_STD_WPU); diff --git a/trafficlight.h b/trafficlight.h index 52ba584..2f8f33d 100644 --- a/trafficlight.h +++ b/trafficlight.h @@ -1,12 +1,25 @@ #include #include +#include "tivaware.h" #include "color.h" -#include "dio.h" #define TIVA_CLK 16e6 #define GREEN_PERIOD TIVA_CLK * 5 #define YELLOW_PERIOD TIVA_CLK * 2 #define RED_PERIOD TIVA_CLK * 7 + +typedef struct Traffic{ + uint8_t cur_color; + uint32_t port; + uint8_t red; + uint8_t yellow; + uint8_t green; +} Traffic; + +extern Traffic tf1; +extern Traffic tf2; + +void set_tf_color(Traffic tf, uint8_t color); void PortInit(uint32_t port, uint32_t clk); void TimerInit(uint32_t timer, void(*timer_handler)(), uint32_t clk, uint32_t delay); -- cgit v1.2.3