Dear forks,
I'm trying to count input pulse by using TC capture function.
But I found strange behavior.
Please look at the code.
If you have DUE, you can try this.
(connect DigitalPin7 to DigitalPin30)
Why does first line show zero ?
It must be one.
thanks.
--- head of result ----
hello world
0
1
2
3
4
5
//
// connect DigitalPin7 to DigitalPin30
//
static void startTC8() { // pulse capture counter
// TCLK8 PD9 Peri-B
uint32_t maskbit = 1 << 9;
PIOD->PIO_PDR = maskbit; // enables peripheral control of the pin
PIOD->PIO_ABSR |= maskbit; // Assigns the I/O line to the Peripheral B function
pmc_enable_periph_clk(ID_TC8);
TC2->TC_CHANNEL[2].TC_CCR =
TC_CCR_CLKDIS; // disables the clock
TC2->TC_CHANNEL[2].TC_CMR =
TC_CMR_TCCLKS_XC2 | // == TCLK8
0; // CLKI: rising edge of the clock
TC2->TC_CHANNEL[2].TC_CCR =
TC_CCR_CLKEN | // enables the clock if CLKDIS is not 1
TC_CCR_SWTRG; // the counter is reset and the clock is started
}
void setup() {
Serial.begin(57600);
pinMode(7,OUTPUT);
digitalWrite(7,LOW);
startTC8();
Serial.println("hello world");
}
void loop() {
digitalWrite(7,HIGH);
delay(2);
digitalWrite(7,LOW);
delay(1000);
Serial.println(TC2->TC_CHANNEL[2].TC_CV,DEC);
}