I been working on a project based on replicating a red light traffic system by using the Tiva C series, I am new into programming and microcontrollers so i am having a lot of issues. The main idea is to have 4 traffic lights, each having 3 LEDS for a total of 12. Then i want to add a temperature sensor and a light sensor to modify the sequence of the leds accordingly to the values gotten from the sensors.
What i am here for, is to understand how to generate a three event sequence by using a single timer.
I want to use the timer to divide the process into three different events, at each event I want to be able to light up an LED of an external circuit. First Event would light up the first LED (Green), second event would light up the second LED(YELLOW) and the third event to light up a third LED (RED).
The following code is what I been working on, also I want to use only one timer to do this implementation.
// function prototypes
void init_timer(void);
void Timer0_ISR(void);
void init_output(void);
uint32_t timer=0;
uint32_t sys_clock;
int main(void)
{
// Configure system clock at 40 MHz.
SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN);
sys_clock = SysCtlClockGet();
init_timer();
init_output();
// Enable the processor to respond to interrupts.
IntMasterEnable();
// Start the timer by enabling operation of the timer module.
TimerEnable(TIMER0_BASE, TIMER_A);
while(1){
}
}
void init_output(void) //Enabling the different ports
{
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); // Enable GPIO Port F.
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3);
}
void init_timer(void)
{
// Enable and configure Timer0 peripheral.
SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
// Configure as a 32-bit timer in periodic mode.
TimerConfigure(TIMER0_BASE, TIMER_CFG_A_CAP_COUNT);
// Initialize timer load register.
TimerLoadSet(TIMER0_BASE, TIMER_A, sys_clock -1);
// Registers a function to be called when the interrupt occurs.
IntRegister(INT_TIMER0A, Timer0_ISR);
//IntRegister(INT_TIMER0A, Timer1_ISR);
// The specified interrupt is enabled in the interrupt controller.
IntEnable(INT_TIMER0A);
// Enable the indicated timer interrupt source.
TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
}
void Timer0_ISR(void)
{
// Clear the timer interrupt.
TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
timer = TimerValueGet(TIMER0_BASE, TIMER_A);
//setting the timer = to the value of the timer at that moment, then divide the timer into 3, in order to have three different events. At each event I used the GPIOS to send a high or low output. I am sure I can not divide the timer like this but that is the way I thought of the timer, please help find another way to do such sequence.
if (timer = 13333333) {
//GPIOPinRead(GPIO_PORTA_BASE, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3);
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, 15);
GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, 0);
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, 0);
}
else if (timer = 26666666)
{
//GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3);
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, 0);
GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, 0);
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, 15);
}
else if (timer = 39999999){
//GPIOPinRead(GPIO_PORTB_BASE, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3);
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, 0);
GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, 15);
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, 0);
}
else {
timer=0;
}
}