How to implement a simple 1 msec timer interrupt routine?

Discussions around product based on ARM Cortex M0+ core.

Moderator: nferre

t.bohler@unitechenergy.no
Posts: 24
Joined: Wed Jun 25, 2014 8:47 am

How to implement a simple 1 msec timer interrupt routine?

Thu Jun 26, 2014 1:02 pm

Hi, I'm new to SAM D20 (Xplained Pro) and have the following question:
How can I implement a simple 1 millisec timer Interrupt Service Routine (ISR)?
The only thing the ISR shall do is set a variable "mSec1=TRUE" (checked in a background loop) and re-start the 1 msec time measurement. Shall I use SysTick(System Timer), TC (Timer/Counters) or other features? What's best (simplest) ?
And, does anyone have some sample code for that configuration and handeling ?

Best Regards
Terje Bohler
terbo
Posts: 26
Joined: Sun Oct 06, 2013 11:04 pm

Re: How to implement a simple 1 msec timer interrupt routine

Fri Jun 27, 2014 12:00 am

Thanks awneil.

Well, it's not what I am looking for because the delay_cycles(..) "hangs" for 1 msec. I need to do other stuff in the meantime, so when a 1-msec-interrupt occur, I must:
1) Set mSec1=TRUE in the ISR routine
2) Return to main-program (execute other stuff) where I also check/reads the mSec1 variable (among others):
3) If (mSec1==TRUE) HandleTheIsr(..)

Regards Terje Bohler
pozz
Posts: 67
Joined: Fri Jun 13, 2014 2:55 pm

Re: How to implement a simple 1 msec timer interrupt routine

Fri Jun 27, 2014 8:47 am

I don't know if you need other timers, but you can set SysTick to trigger every 1msec. And you can use just CMSIS to configure SysTick:

Code: Select all

void SysTick_Handler(void)
{
	mSec1 = TRUE;
}
void
timer_init(void)
{
	SysTick_Config(SYSTEM_CLOCK / ISR_FREQ);
}
SYSTEM_CLOCK should be the processor clock (the Xplained board generally uses the internal 8MHz oscillator with a prescaler set to 8, so it is 1000000). CMSIS defines the variable SystemCoreClock that should be the processor clock (the numerator), but Atmel doesn't assign a right value to it.

ISR_FREQ is the frequency of interruptions of SysTick. In your case is 1/0.001=1000.

The only problem with this approach is if your background loop/tasks take more than 1ms and you want to stop SysTick interrupts until background takes care about the flag mSec1. In that case, I think you have to use one TC peripheral.

Return to “SAM D20 Cortex-M0+ MCU”

Who is online

Users browsing this forum: No registered users and 1 guest