This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Using timer for delay


#include <stdint.h> #include <stdbool.h> #include <stdio.h> #include "inc/hw_memmap.h" #include "driverlib/interrupt.h" #include "driverlib/gpio.h" #include "driverlib/sysctl.h" #include "inc/hw_types.h" #include "inc/hw_ints.h" #include "inc/hw_gpio.h" #include "driverlib/timer.h" #define SW1 GPIO_PIN_4 #define SW2 GPIO_PIN_0 #define LED_RED GPIO_PIN_1 #define LED_GREEN GPIO_PIN_2 #define LED_BLUE GPIO_PIN_3 uint32_t readStatus,ui32Period,tinh; uint32_t val=0; int tick; void INIT_LED() { SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3); GPIOPinWrite(GPIO_PORTF_BASE, LED_RED|LED_GREEN|LED_BLUE, 0); } void config_timer() { SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0); TimerConfigure(TIMER0_BASE, TIMER_CFG_A_PERIODIC_UP); ui32Period = (SysCtlClockGet()/8);//5s TimerLoadSet(TIMER0_BASE, TIMER_A, ui32Period);//5s TimerEnable(TIMER0_BASE, TIMER_A); } uint32_t flg=0; int main(void) { SysCtlClockSet(SYSCTL_SYSDIV_2_5| SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ); INIT_LED(); config_timer(); ui32Period= ui32Period/2; valLED = LED_RED; while(1) { val=TimerValueGet(TIMER0_BASE,TIMER_A); if(TimerValueGet(TIMER0_BASE,TIMER_A) < ui32Period) { flg =1; } else flg=0; if(!flg) GPIOPinWrite(GPIO_PORTF_BASE, LED_RED, valLED); else GPIOPinWrite(GPIO_PORTF_BASE, LED_RED, 0); } }

I'd like to create a delay timer. period is 5 second. Ton = 2.5 sec, Duty = 50%.
My code as above, something wrong, I can not create a program as my desire. Ton so short.

Please help to indicate to where is my errors.

Thanks in advanced!

  • Hello Anh,

    A value load of SysCtlClockGet will result in 1 second count. So to get 5 sec count it has to be SysCtlClockGet()*5 and not SysCtlClockGet()/8.

    Also since you need fractional second, it would be better to set a match at 2.5 second using the Match register so that at time out interrupt you can set the flag and at match you can clear the flag,

    Also please refer to the other thread for generating interrupt so that delays can be performed accurately.

    e2e.ti.com/.../457885

    Regards
    Amit