Simplest timer in KL05Z?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Simplest timer in KL05Z?

805 Views
kazola
Contributor III

Hi everybody,

these are my first days with KL05Z :smileyhappy:

I would like to set a 1-second timer which I will use to raise a flag as a timeout for a polling procedure, since I do not have enough pins to work via interrupts.

Since I will be doing polling, I will be in active mode.

Is there any simple example or code snippet of this to begin with? :smileyhappy:

Many, many thanks in advance.

Have a nice day!

PS: I have found a related examples but I do not want to use ProcessorExpert :smileyhappy:

Tags (3)
0 Kudos
2 Replies

492 Views
mjbcswitzerland
Specialist V

Hi

The standard timer in all M4/M0+ cores (rather than peripheral timers) is the SYSTICK. Often this is used as a periodic interrupt for the OS and then for SW timing.

It won't make 1s if the clock is high - about 335ms max. at 50MHz - but is probably the best one to start with. Generally one uses a higher rate TICK to generate longer SW delays from.

Below is code to configure it as a periodic timer interrupt:

eg. #define TICK_RESOLUTION 50 // 50ms TICK rate

// Routine to initialise the Tick interrupt (uses Cortex M4/M0+ SysTick timer)

//

#define REQUIRED_MS ((1000/TICK_RESOLUTION))                             // the TICK frequency we require in kHz

#define TICK_DIVIDE (((CORE_CLOCK + REQUIRED_MS/2)/REQUIRED_MS) - 1)     // the divide ratio required

extern void fnStartTick(void)

{

    VECTOR_TABLE *ptrVect;

    ptrVect = (VECTOR_TABLE *)(RAM_START_ADDRESS);

    SYSTICK_RELOAD = TICK_DIVIDE;                                        // set reload value to determine the period

    ptrVect->ptrSysTick = _RealTimeInterrupt;                            // enter interrupt handler

    SYSTEM_HANDLER_12_15_PRIORITY_REGISTER |= (SYSTICK_PRIORITY << 24);  // enter the SYSTICK priority

    SYSTICK_CSR = (SYSTICK_CORE_CLOCK | SYSTICK_ENABLE | SYSTICK_TICKINT); // enable timer and its interrupt

}

_RealTimeInterrupt() is called. You will need to fill out the register details depending on which headers you use.

static __interrupt void _RealTimeInterrupt(void)

{

    INT_CONT_STATE_REG = PENDSTCLR;                                      // reset interrupt

    // + code

}

Regards

Mark

0 Kudos

492 Views
kazola
Contributor III

Hi Mark,

many, many thanks for your answer.

However, for a freescale beginner it is a bit difficult snippet. I've done my own code by taking profit of another answer in the forum. I left it here just in case someone needs it.

#include "derivative.h"

#include "freedom_gpio.h"

char toggle = 0;

void SysTick_Handler()

{

    // toggle LEDs

    if (toggle)

        LED1_ON;

    else

        LED1_OFF;

  

     toggle ^=1;

}

// main code

int main(void)

{

  

#if(defined(CW))

    // required

    // -----------

    sysinit();

#endif

  

    gpio_init();

  

    SYST_RVR = SysTick_RVR_RELOAD(6000000);

    SYST_CVR = SysTick_CVR_CURRENT(0);

    SYST_CSR |= SysTick_CSR_ENABLE_MASK | SysTick_CSR_TICKINT_MASK | SysTick_CSR_CLKSOURCE_SHIFT;

  

    for(;;){}

  

    return 0;

}

Let me know if there is something wrong! :smileyhappy:

Have a nice day and sorry for the delay in answering.

0 Kudos