From 5aa3477c59aec1ebedf2a70d8aebfed0d9d23a77 Mon Sep 17 00:00:00 2001 From: Omar Magdy Date: Mon, 22 Aug 2022 14:03:01 +0200 Subject: Added initial code + Made the led stay green for 5 seconds then yellow for 2 seconds then red --- color.c | 6 +++--- color.h | 3 +++ main.c | 69 ++++++++++++++++++++++++++++++++++++++++---------------------- tivaware.h | 1 + 4 files changed, 52 insertions(+), 27 deletions(-) diff --git a/color.c b/color.c index 967350f..9806979 100644 --- a/color.c +++ b/color.c @@ -1,9 +1,6 @@ #include "color.h" #include -#define LED_RED (1U << 1) -#define LED_BLUE (1U << 2) -#define LED_GREEN (1U << 3) void set_color(uint8_t color) { switch (color) { @@ -16,6 +13,9 @@ void set_color(uint8_t color) { case GREEN: GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3, 0x8); break; + case YELLOW: + GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3, 0xA); + break; case WHITE: GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3, 0xe); break; diff --git a/color.h b/color.h index 77fe82a..1d1b0bc 100644 --- a/color.h +++ b/color.h @@ -1,6 +1,9 @@ #include "tivaware.h" +#define LED_RED (1U << 1) +#define LED_BLUE (1U << 2) +#define LED_GREEN (1U << 3) #define PORTF GPIO_PORTF_DATA_R #define INTIAL_COLOR WHITE #define RED 1 diff --git a/main.c b/main.c index c076e22..7dbc47a 100644 --- a/main.c +++ b/main.c @@ -1,8 +1,37 @@ #include "color.h" -#include "systick.h" #include "stdio.h" +#include -int cnt = 0; +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; + + + + +void Timer0_Handler() { + TimerIntClear(TIMER0_BASE, TIMER_BOTH); + switch(cur_color) { + case RED: + cur_color = GREEN; + set_color(cur_color); + TimerLoadSet(TIMER0_BASE, TIMER_BOTH, GREEN_PERIOD); + break; + case YELLOW: + cur_color = RED; + set_color(cur_color); + TimerLoadSet(TIMER0_BASE, TIMER_BOTH, RED_PERIOD); + break; + case GREEN: + cur_color = YELLOW; + set_color(cur_color); + TimerLoadSet(TIMER0_BASE, TIMER_BOTH, YELLOW_PERIOD); + break; + } +} void portFInit() { SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); @@ -13,34 +42,26 @@ void portFInit() { GPIOPadConfigSet (GPIO_PORTF_BASE, GPIO_PIN_0 | GPIO_PIN_4, GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_STD_WPU); } - -void toggle_white() { - if((GPIO_PORTF_DATA_R & 0xe) == 0xe) { - reset_color(); - } else { - set_color(WHITE); - } -} - -void SysTick_Handler() { - cnt++; - if(cnt % 5 == 0) { - toggle_color(WHITE); - } -} - -void toggle_interrupts() { +void traffic_delay() { portFInit(); - SysTickDisable(); - SysTickPeriodSet(PERIOD); - SysTickIntEnable(); - SysTickEnable(); + 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) { } + } int main() { - toggle_interrupts(); + traffic_delay(); } diff --git a/tivaware.h b/tivaware.h index 3438c1a..ce3d029 100644 --- a/tivaware.h +++ b/tivaware.h @@ -6,6 +6,7 @@ #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" #define RIGHT_SWITCH !(GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_0) != 0) #define LEFT_SWITCH !(GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_4) != 0) -- cgit v1.2.3