Some modifications (I set a 50% duty cycle in setup()):
/*******************************************************************************/
/* TIOA6 Frequency = 32.3 KHz over pin 5 */
/*******************************************************************************/
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
PMC->PMC_PCER1 |= PMC_PCER1_PID33; // TC6 power ON? not sure how to change the PCER index in this line using page 38 (or others) in the datasheet.
PIOC->PIO_PDR |= PIO_PDR_P25; // Assuming the "C" should come from the PIOC => PC25
PIOC->PIO_ABSR |= PIO_PC25B_TIOA6; // Periperal type B as well, by page 859
// If TC_CHANNEL[1] is TIOA7, is it correct to assume that TC_CHANNEL[0] is TIOA6 ?
TC2->TC_CHANNEL[0].TC_CMR = TC_CMR_TCCLKS_TIMER_CLOCK1 // MCK/2, clk on rising edge
| TC_CMR_WAVE // Waveform mode
| TC_CMR_WAVSEL_UP_RC // UP mode with automatic trigger on RC Compare
| TC_CMR_ACPA_CLEAR // Clear TIOA6 on RA compare match -- See page 883
| TC_CMR_ACPC_SET; // Set TIOA6 on RC compare match
TC2->TC_CHANNEL[0].TC_RC = 1300; //<********************* Frequency = (Mck/2)/TC_RC rate ~ 32.3 KHz
TC2->TC_CHANNEL[0].TC_RA = 650; //<******************** Duty cycle = (TC_RA/TC_RC) * 100 % = 50%
TC2->TC_CHANNEL[0].TC_IER = TC_IER_CPCS; // Interrupt on RC compare match
NVIC_EnableIRQ(TC6_IRQn);
TC2->TC_CHANNEL[0].TC_CCR = TC_CCR_SWTRG | TC_CCR_CLKEN; // Software trigger TC6 counter and enable
}
void TC6_Handler() { // Is this function name a saved word?
// static uint32_t Count;
TC2->TC_CHANNEL[0].TC_SR; //Read and clear status register so that the interrupt handler fires again and again
// To simplify and focus - commenting the LED lines out. They are solely for LED on/off right?
// Count++;
// if (Count == 20000) {
// digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); // Toggle every 1 Hz
// Count = 0;
// }
}
void loop() {
/*
int duty;
duty+=1;
if (duty>100) duty=0;
TC2->TC_CHANNEL[0].TC_RA = duty; // Would this line be enough to change the duty cycle?
delay(1000);
*/
// 0 = 0% < Duty cycle < 1300 = 100%
}