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 --- main.c | 69 +++++++++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 45 insertions(+), 24 deletions(-) (limited to 'main.c') 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(); } -- cgit v1.2.3