Code to Configure timer and generate interrupt

Go To Last Post
6 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello All,

 

I am using SAME54 Xplained Pro Evaluation kit. I want example code to configure timer using registers which are available under "Atmel\Studio\7.0\packs\atmel\SAME54_DFP\1.0.67\include\instance\tc.h" file. 

 

I am new to this controller so any example code to configure the timer using registers like REG_TC1_CTRLA etc. will be helpful to me.

 

Thanks in advance.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You can search here for REG_TC or similar, note that the TC is not unique for SAME54 so even if you find an example for SAMD20, SAMD21, SAMC21 etc. it can be useful.

Here is another resource:

https://github.com/ataradov/mcu-starter-projects

/Lars

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Lajon,

 

I have modified the code given the link below to configure timer. I wonder why it is not going into the Timer_Handler() function. 

 

https://community.atmel.com/forum/tc-configuration-samc20?skey=REG_TC

 

My modified code is:- 

 


#include "sam.h"

volatile uint32_t tick;
volatile uint32_t loop;

void TC2_Handler(void)
{
	tick++;

	REG_TC2_INTFLAG = TC_INTFLAG_OVF;

}

int main(void)
{
    /* Initialize the SAM system */
    SystemInit();

	REG_GCLK_GENCTRL0 = GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_DFLL;   //GCLK_GENCTRL_SRC_OSC48M; //set GCLK0 to operate from 4Mhz frequency (48Mhz/12 = 4MHz. see errata)
	//enable all peripherals clock in MCLK module
	REG_MCLK_APBBMASK =	MCLK_APBBMASK_SERCOM2 | MCLK_APBBMASK_TC2 |  MCLK_APBBMASK_TC3 |  MCLK_APBBMASK_TCC0;		

	// Enable TC2,TC3 clock from GCLK1
	REG_GCLK_PCHCTRL31 = GCLK_PCHCTRL_GEN_GCLK0 | GCLK_PCHCTRL_CHEN;

	REG_TC2_CTRLA = TC_CTRLA_MODE_COUNT8 | TC_CTRLA_PRESCALER_DIV1024;
	REG_TC2_COUNT8_PER = 200;

	TC2->COUNT8.INTENSET.bit.OVF = 1; // set overflow interrupt
	NVIC_EnableIRQ(TC2_IRQn); // enable interrupts for TC2

	TC2->COUNT8.CTRLA.bit.ENABLE = 1; // enable TC2

	while(TC2->COUNT8.SYNCBUSY.bit.ENABLE == 1); // wait for TC2 to be enabled

    /* Replace with your application code */
    while (1)
    {
		loop++;
    }
}

Why it's not going into TC2_Handler() function while we set a breakpoint during debugging. What is missing over here??

Last Edited: Wed. Jun 19, 2019 - 11:34 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You could also configure the timer/interrupt in Atmel START, and review the generated code as well.

 

JIm

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

REG_GCLK_PCHCTRL26 not REG_GCLK_PCHCTRL31.

/Lars

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Try this

https://microchipdeveloper.com/32arm:samd21-nvic-configuration

Its for samd21 but i guess it should be same for smae54