summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOmar Magdy <omar.professional8777@gmail.com>2022-08-24 23:42:32 +0200
committerOmar Magdy <omar.professional8777@gmail.com>2022-08-24 23:42:32 +0200
commitbc90dfb83866078bce80034d06607b0094aa63e4 (patch)
treea66840c28f58720944cc711a82d426c9afe2490b
parent9bebf5f393bbfd51489b4c4e65a066e47e623f1e (diff)
downloadTraffic-light-bc90dfb83866078bce80034d06607b0094aa63e4.tar.xz
Traffic-light-bc90dfb83866078bce80034d06607b0094aa63e4.zip
Added the functionality of timers And finished the project
-rw-r--r--main.c18
-rw-r--r--trafficlight.c36
-rw-r--r--trafficlight.h12
3 files changed, 58 insertions, 8 deletions
diff --git a/main.c b/main.c
index 1da8860..afbcce7 100644
--- a/main.c
+++ b/main.c
@@ -2,17 +2,23 @@
uint32_t period_1 = RED_PERIOD;
uint32_t period_2 = GREEN_PERIOD;
-uint32_t period_3 = RED_PERIOD;
+uint32_t period_3 = GREEN_PERIOD;
bool switch_pressed = false;
+//Traffics
+Traffic tf1 = {RED , GPIO_PORTA_BASE, 2, 3, 4};
+Traffic tf2 = {YELLOW, GPIO_PORTF_BASE, 1, 2, 3};
+Traffic tf_ped = {RED , GPIO_PORTB_BASE, 5, 0, 7};
-Traffic tf1 = {YELLOW, GPIO_PORTA_BASE, 2, 3, 4};
-Traffic tf2 = {RED , GPIO_PORTF_BASE, 1, 2, 3};
-Traffic tf_ped = {RED , GPIO_PORTB_BASE, 5, 4, 1};
+//Decoders
+Bcd bcd1 = {GPIO_PORTE_BASE, 1, 2, 3, 5};
+Bcd bcd2 = {GPIO_PORTD_BASE, 1, 2, 3, 6};
+Bcd bcd_ped = {GPIO_PORTA_BASE, 5, 6, 7, 4}; // (5, 6, 7) A and 4 of E
int main()
{
TrafficInit();
- // PortInit(GPIO_PORTA_BASE, SYSCTL_PERIPH_GPIOA, 0x0, 0xFF); // Initialize porta
- // GPIOPinWrite(GPIO_PORTA_BASE, 0xFF, 2);
+ // PortInit(GPIO_PORTE_BASE, SYSCTL_PERIPH_GPIOE, 0x0, 0xFF); // Initialize porta
+ // GPIOPinWrite(GPIO_PORTE_BASE, 0xFF, 0);
+ // GPIOPinWrite(GPIO_PORTE_BASE, 0xFF, (1 << 1) | (1 << 2));
}
diff --git a/trafficlight.c b/trafficlight.c
index 6ba5e18..c46ae48 100644
--- a/trafficlight.c
+++ b/trafficlight.c
@@ -60,6 +60,7 @@ void Timer2_Handler() {
set_tf_color(tf_ped, RED);
TimerEnable(TIMER0_BASE, TIMER_BOTH);
TimerEnable(TIMER1_BASE, TIMER_BOTH);
+ // TimerDisable(TIMER2_BASE, TIMER_BOTH);
switch_pressed = false;
break;
case GREEN:
@@ -79,13 +80,15 @@ void Switch_Handler() {
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
+ TimerInit(TIMER2_BASE, Timer2_Handler, SYSCTL_PERIPH_TIMER2, period_3); // 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
+ PortInit(GPIO_PORTE_BASE, SYSCTL_PERIPH_GPIOE, 0x0, 0xFF); // Initialize portb
+ PortInit(GPIO_PORTD_BASE, SYSCTL_PERIPH_GPIOD, 0x0, 0xFF); // Initialize portb
__asm("CPSID I"); // Disable all interrupts
GPIOIntUnregister(GPIO_PORTF_BASE);
GPIOIntRegister(GPIO_PORTF_BASE, Switch_Handler);
@@ -97,7 +100,11 @@ void TrafficInit() {
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
+ BcdWrite(bcd1, TIMER0_BASE, false);
+ BcdWrite(bcd2, TIMER1_BASE, false);
+ if(switch_pressed) {
+ BcdWrite(bcd_ped, TIMER2_BASE, true);
+ }
}
}
@@ -125,3 +132,28 @@ void Traffic_Handler(Traffic *tf, uint32_t timer) {
}
}
}
+
+void BcdWrite(Bcd bcd, uint32_t timer, bool ped) {
+ uint8_t n = (TimerValueGet(timer, TIMER_A) / TIVA_CLK) + 1;
+ int bin[4];
+ if(ped) {
+ for(int i = 0; i < 4; i++) {
+ bin[i] = n % 2;
+ n /= 2;
+ }
+ // GPIOPinWrite(GPIO_PORTA_BASE, 0xFF, (((bin[1]) << bcd.B) | ((bin[2]) << bcd.C)));
+ DIO_WritePin(&GPIO_PORTA_DATA_R, bcd.A, bin[0]);
+ DIO_WritePin(&GPIO_PORTA_DATA_R, bcd.B, bin[1]);
+ DIO_WritePin(&GPIO_PORTA_DATA_R, bcd.C, bin[2]);
+ GPIOPinWrite(GPIO_PORTE_BASE, 0xFF, (bin[3] << bcd.D));
+ }
+ else {
+ for(int i = 0; i < 4; i++) {
+ bin[i] = n % 2;
+ n /= 2;
+ }
+ GPIOPinWrite(bcd.port, 0xFF, ((bin[0]) << bcd.A) | ((bin[1]) << bcd.B) | (bin[2] << bcd.C) | (bin[3] << bcd.D));
+ // GPIOPinWrite(bcd.port, 0xFF, (1 << bcd.pins[i]));
+
+ }
+}
diff --git a/trafficlight.h b/trafficlight.h
index efc2306..f1f94a9 100644
--- a/trafficlight.h
+++ b/trafficlight.h
@@ -20,9 +20,20 @@ typedef struct Traffic{
uint8_t green;
} Traffic;
+typedef struct Bcd{
+ uint32_t port;
+ uint8_t A;
+ uint8_t B;
+ uint8_t C;
+ uint8_t D;
+} Bcd;
+
extern Traffic tf1;
extern Traffic tf2;
extern Traffic tf_ped;
+extern Bcd bcd1;
+extern Bcd bcd2;
+extern Bcd bcd_ped;
extern bool switch_pressed;
extern uint32_t period_1;
extern uint32_t period_2;
@@ -33,4 +44,5 @@ void Traffic_Handler(Traffic *tf, uint32_t timer);
void set_tf_color(Traffic tf, uint8_t color);
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);
+void BcdWrite(Bcd bcd, uint32_t timer, bool ped);