summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOmar Magdy <omar.professional8777@gmail.com>2022-08-23 12:32:39 +0200
committerOmar Magdy <omar.professional8777@gmail.com>2022-08-23 12:32:39 +0200
commitb5c1df30961369762e6c9ead8a11f8c9d0f5f8a3 (patch)
tree3f2f6583b0b302d56ab88ef476f1c4fcbce510b2
parent5a180583b8835b564d519c8a2548edc92a01842e (diff)
downloadTraffic-light-b5c1df30961369762e6c9ead8a11f8c9d0f5f8a3.tar.xz
Traffic-light-b5c1df30961369762e6c9ead8a11f8c9d0f5f8a3.zip
Added two timers and made them sync the 2 traffic lights as when one
traffic light is green the other is red and vice versa.
-rw-r--r--color.c56
-rw-r--r--color.h26
-rw-r--r--main.c91
-rw-r--r--trafficlight.c29
-rw-r--r--trafficlight.h12
5 files changed, 131 insertions, 83 deletions
diff --git a/color.c b/color.c
index 9806979..e7e072d 100644
--- a/color.c
+++ b/color.c
@@ -3,42 +3,46 @@
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;
switch (color) {
- case RED:
- GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3, 0x2);
+ case RED_TF1:
+ GPIOPinWrite(GPIO_PORTF_BASE, pins, 0x2);
break;
- case BLUE:
- GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3, 0x4);
+ case YELLOW_TF1:
+ GPIOPinWrite(GPIO_PORTF_BASE, pins, 0x4);
break;
- case GREEN:
- GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3, 0x8);
+ case GREEN_TF1:
+ GPIOPinWrite(GPIO_PORTF_BASE, pins, 0x8);
break;
- case YELLOW:
- GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3, 0xA);
+ case RED_TF2:
+ GPIOPinWrite(GPIO_PORTA_BASE, pins, 0x4);
break;
- case WHITE:
- GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3, 0xe);
+ case YELLOW_TF2:
+ GPIOPinWrite(GPIO_PORTA_BASE, pins, 0x8);
break;
- }
-}
-
-void toggle_color(uint8_t color) {
- switch (color) {
- case RED:
- GPIO_PORTF_DATA_R ^= LED_RED;
- break;
- case BLUE:
- GPIO_PORTF_DATA_R ^= LED_BLUE;
- break;
- case GREEN:
- GPIO_PORTF_DATA_R ^= LED_GREEN;
- break;
- case WHITE:
- GPIO_PORTF_DATA_R ^= (LED_RED | LED_BLUE | LED_GREEN);
+ case GREEN_TF2:
+ GPIOPinWrite(GPIO_PORTA_BASE, pins, 0x10);
break;
}
}
+// void toggle_color(uint8_t color) {
+// switch (color) {
+// case RED:
+// GPIO_PORTF_DATA_R ^= LED_RED;
+// break;
+// case BLUE:
+// GPIO_PORTF_DATA_R ^= LED_BLUE;
+// break;
+// case GREEN:
+// GPIO_PORTF_DATA_R ^= LED_GREEN;
+// break;
+// case WHITE:
+// GPIO_PORTF_DATA_R ^= (LED_RED | LED_BLUE | LED_GREEN);
+// break;
+// }
+// }
+
void reset_color() {
PORTF &= 0x11;
}
diff --git a/color.h b/color.h
index 1d1b0bc..9a43477 100644
--- a/color.h
+++ b/color.h
@@ -4,20 +4,28 @@
#define LED_RED (1U << 1)
#define LED_BLUE (1U << 2)
#define LED_GREEN (1U << 3)
+#define RED_TF1 1
+#define YELLOW_TF1 2
+#define GREEN_TF1 3
+#define RED_TF2 4
+#define YELLOW_TF2 5
+#define GREEN_TF2 6
+#define RED_TF3 7
+#define YELLOW_TF3 8
+#define GREEN_TF3 9
#define PORTF GPIO_PORTF_DATA_R
-#define INTIAL_COLOR WHITE
-#define RED 1
-#define BLUE 2
-#define GREEN 3
-#define MAGENTA 4
-#define CYAN 5
-#define YELLOW 6
-#define WHITE 7
+// #define RED 1
+// #define BLUE 2
+// #define GREEN 3
+// #define MAGENTA 4
+// #define CYAN 5
+// #define YELLOW 6
+// #define WHITE 7
void set_color(uint8_t color);
-void toggle_color(uint8_t color);
+// void toggle_color(uint8_t color);
void reset_color();
diff --git a/main.c b/main.c
index 7dbc47a..64cc188 100644
--- a/main.c
+++ b/main.c
@@ -1,67 +1,62 @@
-#include "color.h"
-#include "stdio.h"
-#include <stdint.h>
-
-int cnt = 1;
-#define GREEN_PERIOD 80000000
-#define YELLOW_PERIOD 48000000
-#define RED_PERIOD 16000000
-uint8_t cur_color = GREEN;
-uint32_t period = GREEN_PERIOD;
-
-
+#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;
void Timer0_Handler() {
TimerIntClear(TIMER0_BASE, TIMER_BOTH);
- switch(cur_color) {
- case RED:
- cur_color = GREEN;
- set_color(cur_color);
+ switch(cur_color_1) {
+ case RED_TF1:
+ cur_color_1 = GREEN_TF1;
+ set_color(cur_color_1);
TimerLoadSet(TIMER0_BASE, TIMER_BOTH, GREEN_PERIOD);
break;
- case YELLOW:
- cur_color = RED;
- set_color(cur_color);
+ case YELLOW_TF1:
+ cur_color_1 = RED_TF1;
+ set_color(cur_color_1);
TimerLoadSet(TIMER0_BASE, TIMER_BOTH, RED_PERIOD);
break;
- case GREEN:
- cur_color = YELLOW;
- set_color(cur_color);
+ case GREEN_TF1:
+ cur_color_1 = YELLOW_TF1;
+ set_color(cur_color_1);
TimerLoadSet(TIMER0_BASE, TIMER_BOTH, YELLOW_PERIOD);
break;
}
}
-void portFInit() {
- SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
- while(!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOF)) {};
- GPIOUnlockPin(GPIO_PORTF_BASE, GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4);
- GPIOPinTypeGPIOInput(GPIO_PORTF_BASE, GPIO_PIN_0 | GPIO_PIN_4);
- GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3);
- GPIOPadConfigSet (GPIO_PORTF_BASE, GPIO_PIN_0 | GPIO_PIN_4,
- GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_STD_WPU);
-}
-void traffic_delay() {
- portFInit();
- reset_color();
- SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
- while(!SysCtlPeripheralReady(SYSCTL_PERIPH_TIMER0)) {}
- TimerDisable(TIMER0_BASE, TIMER_BOTH);
- TimerConfigure(TIMER0_BASE, (TIMER_CFG_PERIODIC));
- TimerLoadSet(TIMER0_BASE, TIMER_BOTH, period);
- TimerIntUnregister(TIMER0_BASE, TIMER_BOTH);
- TimerEnable(TIMER0_BASE, TIMER_BOTH);
- set_color(GREEN);
- TimerIntRegister(TIMER0_BASE, TIMER_BOTH, (*Timer0_Handler));
- TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT | TIMER_TIMB_TIMEOUT);
- while(1) {
-
+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);
+ TimerLoadSet(TIMER1_BASE, TIMER_BOTH, GREEN_PERIOD);
+ break;
+ case YELLOW_TF2:
+ cur_color_2 = RED_TF2;
+ set_color(cur_color_2);
+ TimerLoadSet(TIMER1_BASE, TIMER_BOTH, RED_PERIOD);
+ break;
+ case GREEN_TF2:
+ cur_color_2 = YELLOW_TF2;
+ set_color(cur_color_2);
+ TimerLoadSet(TIMER1_BASE, TIMER_BOTH, YELLOW_PERIOD);
+ break;
}
-
}
+
int main()
{
- traffic_delay();
+ PortInit(GPIO_PORTF_BASE, SYSCTL_PERIPH_GPIOF); // Initialize portf
+ PortInit(GPIO_PORTA_BASE, SYSCTL_PERIPH_GPIOA); // Initialize porta
+ __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
+ __asm("CPSIE I"); // Enable all interrupts
+ while(1) {
+ __asm("wfi"); // power saving mode
+ }
}
diff --git a/trafficlight.c b/trafficlight.c
new file mode 100644
index 0000000..a004d98
--- /dev/null
+++ b/trafficlight.c
@@ -0,0 +1,29 @@
+#include "trafficlight.h"
+
+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;
+ 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,
+ 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);
+ TimerConfigure(timer, (TIMER_CFG_PERIODIC));
+ TimerLoadSet(timer, TIMER_BOTH, delay);
+ TimerIntUnregister(timer, TIMER_BOTH);
+ TimerEnable(timer, TIMER_BOTH);
+ TimerIntRegister(timer, TIMER_BOTH, timer_handler);
+ TimerIntEnable(timer, TIMER_TIMA_TIMEOUT | TIMER_TIMB_TIMEOUT);
+}
+
+
diff --git a/trafficlight.h b/trafficlight.h
new file mode 100644
index 0000000..52ba584
--- /dev/null
+++ b/trafficlight.h
@@ -0,0 +1,12 @@
+#include <stdint.h>
+#include <stdbool.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
+
+void PortInit(uint32_t port, uint32_t clk);
+void TimerInit(uint32_t timer, void(*timer_handler)(), uint32_t clk, uint32_t delay);