How to set up Timer and interrupts

Hi,

I try to set up a interrupt but can't get it running. I have the following code, but running out of ideas what to try next. Can by anybody help me with it?

Cheers

#include <stdint.h>
#include "Arduino.h"
#include "Print.h"

#include <inttypes.h>

//==================================================//

#define INTERRUPT1_TIMER_NUM 6
#define INTERRUPT1_TIMER_COUNTER TC2
#define INTERRUPT1_TIMER_CHANNEL 0
#define INTERRUPT1_FREQUENCY 10000000
#define INTERRUPT1_PRIORITY 5
#define INTERRUPT1_TIMER_IRQN TC6_IRQn
//#define HAL_INTERRUPT1_TIMER_ISR void TC6_Handler()

//==================================================//

void HAL_Interrupt1_timer_start();

//==================================================//

void TC6_Handler(void)
{
TC_GetStatus(INTERRUPT1_TIMER_COUNTER, INTERRUPT1_TIMER_CHANNEL);

if(digitalRead(13) == LOW)
digitalWrite(13, LOW);
else
digitalWrite(13, LOW);
}

void HAL_Interrupt1_timer_start() {
// Get the ISR from table
Tc *tc = INTERRUPT1_TIMER_COUNTER;
IRQn_Type irq = INTERRUPT1_TIMER_IRQN;
uint32_t channel = INTERRUPT1_TIMER_CHANNEL;
uint32_t rc = VARIANT_MCK / 128 / INTERRUPT1_FREQUENCY; // creates the wanted frequency, 128 because we selected clock 4 -> see clock info for more details

pmc_set_writeprotect(false); // remove write protection on registers
pmc_enable_periph_clk((uint32_t)irq); // turns on the clock

NVIC_SetPriority(irq, 4);

tc->TC_CHANNEL[channel].TC_CCR = TC_CCR_CLKDIS;

tc->TC_CHANNEL[channel].TC_SR; // clear status register
tc->TC_CHANNEL[channel].TC_CMR = TC_CMR_WAVSEL_UP_RC | TC_CMR_WAVE | TC_CMR_TCCLKS_TIMER_CLOCK4;

tc->TC_CHANNEL[channel].TC_IER = TC_IER_CPCS; // enable interrupt on timer match with register C
tc->TC_CHANNEL[channel].TC_IDR = ~TC_IER_CPCS;

tc->TC_CHANNEL[channel].TC_RC = rc;
tc->TC_CHANNEL[channel].TC_RA = rc/2;

tc->TC_CHANNEL[channel].TC_CCR = TC_CCR_CLKEN; //enables the clock

NVIC_EnableIRQ(irq); // enable Nested Vector Interrupt Controller
}

void setup()
{
pinMode(13,OUTPUT);
HAL_Interrupt1_timer_start();
}

void loop()
{
}

Here is a basic timer programming performing a nice SOS with all the explanations that you can find on this site:

http://2manyprojects.net/timer-interrupts

You can also use a TC Library like this one :

and BTW, use code tags to post your code.

Hey thanks for the quick response ard_newbie.

But is there also a solution with the TC and Interrupt registers?

Cheers

What are you trying to accomplish exactly?