I'm currently working on a project where I need to control a servo motor with a PWM signal that varies from 1Khz to 5Khz. I currently have a bit of example code working but it only works on pins 4 & 5 (due to the register and timer addresses). I was wondering if anyone could help me adapt this code to use pin 2 since I already have a custom PCB for the due that will use that pin for the servo motor. If I'm not mistaken this will also set pin 13 to the same PWM outputs, but that is fine in my case.
Here is the example code I already have:
// Output 50% duty cycle PWM at 5kHz and 1kHz on digital pins D4 and D5 using TC6
void setup()
{
REG_PMC_PCER1 |= PMC_PCER1_PID33; // Enable peripheral TC6 (TC2 Channel 0)
REG_PIOC_ABSR |= PIO_ABSR_P26 | PIO_ABSR_P25; // Switch the multiplexer to peripheral B for TIOA6 and TIOB6
REG_PIOC_PDR |= PIO_PDR_P26 | PIO_PDR_P25; // Disable the GPIO on the corresponding pins
REG_TC2_CMR0 = TC_CMR_BCPC_SET | // Set TIOB on counter match with RC0
TC_CMR_ACPC_SET | // Set TIOA on counter match with RC0
TC_CMR_BCPB_CLEAR | // Clear TIOB on counter match with RB0
TC_CMR_ACPA_CLEAR | // Clear TIOA on counter match with RA0
TC_CMR_WAVE | // Enable wave mode
TC_CMR_WAVSEL_UP_RC | // Count up with automatic trigger on RC compare
TC_CMR_EEVT_XC0 | // Set event selection to XC0 to make TIOB an output
TC_CMR_TCCLKS_TIMER_CLOCK1; // Set the timer clock to TCLK1 (MCK/2 = 84MHz/2 = 48MHz)
REG_TC2_RA0 = 4200; // Load the RA0 register
REG_TC2_RB0 = 4200; // Load the RB0 register
REG_TC2_RC0 = 8400; // Load the RC0 register
REG_TC2_CCR0 = TC_CCR_SWTRG | TC_CCR_CLKEN; // Enable the timer TC6
}
void loop() {
delay(1000);
REG_TC2_RA0 = 21000; // Load the RA0 register
REG_TC2_RB0 = 21000; // Load the RB0 register
REG_TC2_RC0 = 42000; // Load the RC0 register
REG_TC2_CCR0 = TC_CCR_SWTRG | TC_CCR_CLKEN; // Enable the timer TC6
delay(1000);
REG_TC2_RA0 = 4200; // Load the RA0 register
REG_TC2_RB0 = 4200; // Load the RB0 register
REG_TC2_RC0 = 8400; // Load the RC0 register
REG_TC2_CCR0 = TC_CCR_SWTRG | TC_CCR_CLKEN; // Enable the timer TC6
}