You are here
Home > embedded >

How To Use Two Timers In Same Project

If you are using SAM series controller you probably have came across this question, How do i implement two timer which generate two interrupt at different time in same project? or How we can use two timer in same project in SAME54 controller? Well SAM series controller are ARM-based MCUs and MPUs from ATMEL now Microchip.

We are using ATSAME54P20A ie SAME54 explained pro board to evaluate the functionality of it. So created new project using start.atmel.com with TC0 and TC1 along with WDT and interrputs. The project tree looks as below,

This project works well and TC0 timer interrupt is working but TC1 timer interrupt is not working. Following is code for initialization and using timer.

	TIMER_1_task1.interval = 50;
	TIMER_1_task1.cb       = TIMER_1_task1_cb;
	TIMER_1_task1.mode     = TIMER_TASK_REPEAT;


	timer_add_task(&TIMER_1, &TIMER_1_task1);
		
	TIMER_0_task1.interval = 10;
	TIMER_0_task1.cb       = TIMER_0_task1_cb;
	TIMER_0_task1.mode     = TIMER_TASK_REPEAT;
	
	timer_add_task(&TIMER_0, &TIMER_0_task1);
	timer_start(&TIMER_0);
	timer_start(&TIMER_1);

Put breakpoint in ‘TIMER_1_task1_cb’ function and it never hits breakpoint, but it hits breakpoint in ‘TIMER_0_task1_cb’ function. When we check out status flag of timer1, as soon as it initializes TC0 using ‘TIMER_0_init()’ from driver_init.c it sets ‘STOP’ and ‘SLAVE’ bit.

Bit 1 – SLAVE Slave Status Flag
This bit is only available in 32-bit mode on the slave TC (i.e., TC1 and/or TC3). The bit is set when the associated master TC (TC0 and TC2, respectively) is set to run in 32-bit mode.
Bit 0 – STOP Stop Status Flag
This bit is set when the TC is disabled, on a Stop command, or on an overflow/underflow condition when the One-Shot bit in the Control B Set register (CTRLBSET.ONESHOT) is ‘1’.

Problem is while using start.atmel.com configuration there is no option to set timer in 8 bit or 16 bit or 32 bit mode. By default when we create project it sets in 32-bit mode.

Solution to this issue is set both timers in either 16-bit or 8 bit mode. The settings can be found in “Config\hpl_tc_config.h” file. The change should look like as below

#ifndef CONF_TC0_MODE
#define CONF_TC0_MODE TC_CTRLA_MODE_COUNT16_Val
#endif

#ifndef CONF_TC1_MODE
#define CONF_TC1_MODE TC_CTRLA_MODE_COUNT16_Val
#endif

With this change both timers are working.

Leave a Reply

Top