Hi
I studied a piece of code I found on the net and tried to document every possible line I could find info on (with info + datasheet reference). That way I have allready a better understanding of what the code does
Code here
volatile boolean ledOn;
void TC3_Handler() //TC1 ch 0
{
TC_GetStatus(TC1, 0);
digitalWrite(13, ledOn = !ledOn);
}
void setup()
{
pinMode(13, OUTPUT);
pmc_set_writeprotect(false); //Turn off write protection on Power Management Controller
pmc_enable_periph_clk(TC3_IRQn); // Enable the peripheral clock by IRQ
TC_Configure(TC1, 0, TC_CMR_WAVE | TC_CMR_WAVSEL_UP_RC | TC_CMR_TCCLKS_TIMER_CLOCK4); // Configure the timer on its specified channel. Timer is stopped after config and must be started in code.
// Used parameters
// ---------------
// TC_CMR_WAVE = Set the operating mode to WaveForm mode (datasheet p. 862)
// TC_CMR_WAVSEL_UP_RC = UP counting mode with automatic trigger on RC compare. When RC is reached the counter also resets (datasheet p. 884)
// TC_CMR_TCCLKS_TIMER_CLOCK4 = Set counting speed to Master Clock divided by 128 (datasheet p. 884)
uint32_t rc = VARIANT_MCK / 128 / 1;
TC_SetRA(TC1, 0, rc >> 1); // Set the counter value where the output of TIOA goes high in register RA (datasheet p.867)
TC_SetRC(TC1, 0, rc); //Set the counter value where the output of TIOA goes low and the counter is reset in register RC (datasheet p.867)
TC_Start(TC1, 0); // Start the Timer Counter which was stopped after the TC_configure
TC1->TC_CHANNEL[0].TC_IER= TC_IER_CPCS | TC_IER_CPAS;
TC1->TC_CHANNEL[0].TC_IDR=~(TC_IER_CPCS | TC_IER_CPAS);
NVIC_EnableIRQ(TC3_IRQn); // Enables the interrupt so that in this case TC3_Handler can be called on overflow of the counter
Serial.begin(250000); // opens serial port, sets data rate to 250000 bps
}
void loop()
{
Serial.println("print current counter value of TC1 channel 0 here");
}
This code inits a counter on Timer 1 channel 0 and lets it count up until it reaches the value in register RC, after which it resets and the TC3_Handler() interrupHandler is called.
This is tested code and works very fine.
But now I would like to be able to read the value of the counter itself. It is a 32bits unsigned integer.
Would anybody know how I can READ the current value in the Timer Counter Value Register. On page 879 of the datasheet there is info about the register mapping of this TC_CV register, but I have no clue how to read it.
Also, since it is READ ONLY, I wonder how I can RESET the TC_CV register by code so it is 0 when I want it ? Maybe it is 0 on start? or can I do anything to set this value somehow to 0 ?
Kind regards,
Bart