Hi @smanna
If you hook your scope probe up to either digital pin D4 or D5 you'll see a 4kHz PWM signal with 50% duty cycle. These ouputs come directly from the TC6 timer.
If you change the RA and RB values between 0 and the value in the RC register, you'll see the duty-cycle of the corresponding PWM output change between 0% and 100%.
The TC6 timer's frequency is determined by the value in the RC register given by the formula:
PWM_FREQUENCY = TIMER_CLOCK / RC
or alternatively:
RC = TIMER_CLOCK / PWM_FREQUENCY
Since TIMER_CLOCK is set to CLOCK1, the timer's clock frequency is MCLK / 2 = 42MHz, so in this case:
RC = 42MHz / 4kHz = 10500
To get a 50% duty-cycle RA and RB are set to half of this value (5250).
There are various ways to test the frequency of the ISR. Personally, if I can see a 4kHz output from the timer on my scope (and I'm sure I've selected the correct timer mode) then that's good enough.
To be 100% sure though, one way is to manually turn on and off (know as bit banging) a digital pin with the ISR itself, then look at the resulting output on a scope.
To do this, set up a pin to be a digital output in the setup() portion of the sketch, say digital pin D2:
pinMode(2, OUTPUT); // Set digital pin D2 to an OUTPUT
...then within the ISR itself add this code:
void TC6_Handler() // ISR TC6 (TC2 Channel 0)
{
if (TC2->TC_CHANNEL[0].TC_SR & TC_SR_CPCS) // Check for RC compare condition
{
// Add you code here...
static uint8_t state = LOW; // Define an output state variable
digitalWrite(2, state); // Output the current state
state = !state; // Change the output state LOW to HIGH or HIGH to LOW
}
}
After uploading these changes you should be able to see an 2kHz square wave output on D2. The toggling of the output each time the ISR runs, means that the output frequency of the waveform is half the number of times the ISR is called per second.