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 prescalar



hi,i am trying to toggle my led state after every 1 second....for this purpose i m using my timer1 in 16 bit mode Along with that i m using a prescalar of 255....but it's not working?

#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/sysctl.h"
#include "driverlib/interrupt.h"
#include "driverlib/gpio.h"
#include "driverlib/timer.h"

int main(void)
{
    
    /*Set the clocking to directly run from the crystal at 16MHz*/
    SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN);

    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
    GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1);
    GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1,0);

    SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER1);
    TimerConfigure(TIMER1_BASE, TIMER_CFG_A_PERIODIC);
    TimerPrescaleSet(TIMER1_BASE,TIMER_A,254);

    //ulPeriod = (SysCtlClockGet() / 10) / 2;
    //TimerLoadSet(TIMER1_BASE, TIMER_A, ulPeriod -1);

    IntEnable(INT_TIMER1A);
    TimerIntEnable(TIMER1_BASE, TIMER_TIMA_TIMEOUT);
    IntMasterEnable();

    TimerEnable(TIMER1_BASE, TIMER_A);

    while(1)
    {
    }
}

void Timer1IntHandler(void)
{
    // Clear the timer interrupt
    TimerIntClear(TIMER1_BASE, TIMER_TIMA_TIMEOUT);
    
    GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, GPIO_PIN_1);
        SysCtlDelay(SysCtlClockGet()/3);
    

}

  • You report use of a 16 bit timer & mated prescaler - and report the (always well detailed/helpful) "Not Working!"  (bet you that w/in 60 seconds I can come up with at least 5 illustrations of famed, "Not working...")  Thus - that  description, "Not Working" provides very little value...  In fact, "eats" time/effort of your helpers - but saved you time/effort!   (good that!)...  "Not working" may range from Program Faulted to minor irregularity in Timer Result Register - we're given no clue!

    Now - you hold your entire program, "hostage" to the proper functioning of multiple function calls and/or unclearly defined variables.  Can that be good - is it wise?  To illustrate:

    a) "TimerLoadSet(TIMER1_BASE, TIMER_A, ulPeriod -1);" now commented out - relies upon the correctness of ulPeriod - does it not?  What happens if ulPeriod bears an illegal/improper value?

    b) Critical variable, "ulPeriod = (SysCtlClockGet() / 10) / 2;" now also commented out - relies upon the correct functioning of, "SysCtlClockGet()" - does it not?  And - "SysCtlClockGet()" - under rebrandWare & with certain TM4C MCUs - has long been reported (here & elsewhere) to be error-prone.  Thus you've introduced another potential pitfall.  Is the slight ease/gain you (hopefully) earn from such "indirect methods" worth it?  Surely not at this place - this time!

    c) Our group prefers to, "positively disable any Timer" prior to its set-up/config.  (that's safest, best practice)  You "assume/hope/pray that the timer is disabled" - less than ideal.

    d) You title your post, "Using Prescaler" - yet KISS dictates that you get the basic timer working first - and then and only then "patch in" the Prescaler.   In that manner - you have a far better basis by which to, "blame the Prescaler!"  Perhaps you've done such - but we're not told.  Doing too much - w/out individual test/verify - is a ticket to lost sleep/morale.  Once both perform to "timed perfection" then you can "tweak in" precise values.  (and possibly add properly chosen variables & "known good" function calls...)

    Violating KISS is rarely good - hope that case has been well made here...

    (I've not made the time/effort for exhaustive code review (other than 2 critical code lines you've commented into oblivion - w/out explanation!) believe your "method of attack" is flawed - and provided justification (and work-arounds) for that assessment...)