Up to now I used timers on my Due for different tasks (interrupts, frequency generation). Now I don't understand what's wrong with following configuration of a timer to generate a timeout signal:
// configure Timeout-Timer TIOB2/PA6/Periph.A
void configTIOB2(uint8_t secs) {
PMC->PMC_PCER0 = PMC_PCER0_PID29; // clock enable TC2 with ID=29
PIOA->PIO_ABSR &= !PIO_ABSR_P6; // multiplexer pin PA6 to peripheral A for TIOB2
PIOA->PIO_PDR = PIO_PDR_P6; // GPIO for pin PA6 deactivate
TC0->TC_CHANNEL[2].TC_CMR = TC_CMR_TCCLKS_TIMER_CLOCK4 | // MCK/128 = 656,25 kHz
TC_CMR_WAVE | // Wavemode
TC_CMR_WAVSEL_UP | // cnt up, RC compare not triggers
TC_CMR_BCPB_SET | // RB compare sets TIOB to high
TC_CMR_BCPC_CLEAR | // RC compare sets TIOB to low
TC_CMR_CPCSTOP | // RC compare stops clock
TC_CMR_BSWTRG_CLEAR; // SW trigger sets TIOB to low
TC0->TC_CHANNEL[2].TC_RB = 656250 * secs; // TIOB2 high after 4 secs
TC0->TC_CHANNEL[2].TC_RC = 656250 * (secs+1); // TIOB2 low after 5 secs
// enable and start occurs outside
}
void setup() {
SerialUSB.begin(0); // fuer Print-Ausgaben
while (!SerialUSB);
while (1) {
SerialUSB.print(F("Start Timer "));
TC0->TC_CHANNEL[2].TC_CCR = TC_CCR_CLKEN | TC_CCR_SWTRG; // Timeout clock enable, start
delay(1000);
SerialUSB.print(TC0->TC_CHANNEL[2].TC_CV); // look at counter value
delay(1000);
SerialUSB.print(" ");
SerialUSB.print(TC0->TC_CHANNEL[2].TC_CV);
delay(3000);
SerialUSB.print(" ");
SerialUSB.println(TC0->TC_CHANNEL[2].TC_CV);
SerialUSB.println(F("TIOB2?"));
}
}
void loop() {
// put your main code here, to run repeatedly:
}
My PC is connected to the Due Programming Port.
A second USB cable is connected to the Due Native USB Port (SerialIUSB).
A volt meter is connected to pin A4 (PA6). It should show low level for 4 sec and high level for 1 sec. But there is low level all the time!
The print outs look like:
Start Timer 655660 1311913 3280662
TIOB2?
Start Timer 655644 1311897 3280646
TIOB2?
...
The timer is running 655660 steps per second, but TIOB2 on pin A4 is low all the time ![]()
What is wrong?
remark: when you are testing the sketch on a Due and do some changes then you should disconnect the USB cable from the Native Port during compilation.