Arduino Forum

Products => Arduino Due => Topic started by: masumi on Jul 18, 2013, 04:58 am

Title: Using TC capture mode to count input pulse
Post by: masumi on Jul 18, 2013, 04:58 am
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

Code: [Select]

//
// 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);
}
Title: Re: Using TC capture mode to count input pulse
Post by: elevine on Jul 22, 2013, 06:08 pm
I wish I could help you with your issue, but in the meantime, could you check out my post:
http://forum.arduino.cc/index.php?topic=178773.0

or

http://www.reddit.com/r/arduino/comments/1itdy8/due_at91sam3x8e_processor_how_to_modify_timer/

You seem to understand a good amount about the timers and their registers on the Due AT91SAM3X8E processor. I've found little documentation about the code needed to modify these timers. Any help or information you can provide would be especially useful.

And good karma is often rewarded ;)
Title: Re: Using TC capture mode to count input pulse
Post by: masumi on Jul 23, 2013, 12:23 pm
Thanks elevine,
I read your post and I got  Atmel Software Framework (ASF).
But unfortunately I have not gotten exact answer yet.
It seems that people usually use TC capture mode with TC_RAx and/or TC_RBx.
I want to count input pulse simply with only TC_CVx.

regards,
Title: Re: Using TC capture mode to count input pulse
Post by: jt6245 on Jul 27, 2013, 01:58 pm
In page 873 of the doc11057.pdf,(37.6 Functional Description\37.6.2 32-bit Counter)

"The current value of the counter is accessible in real time by reading the Counter Value Register,
TC_CV. The counter can be reset by a trigger. In this case, the counter value passes to
0x0000 on the next valid edge of the selected clock."

Hope the above text will relieve your concern.  :)
Title: Re: Using TC capture mode to count input pulse
Post by: masumi on Jul 29, 2013, 06:11 am
Thanks jt6245,
it seems that first positive edge after "TC2->TC_CHANNEL[2].TC_CCR = TC_CCR_CLKEN |TC_CCR_SWTRG;" clears the TC_CV register. I feel uncomfortable about this specification.
regards,