Go Down

Topic: Reading two digital Pins at the same time (Read 147 times) previous topic - next topic

cristobalmr

Hi, I'm new to this!

I'm working in a counter. I have a detector that will give me a signal when something have pass through it, so I must make coincidence between two detectors. Both signals will be 0.0005 microseconds apart, so I can say that the arduino will get them at the same time.

My goal is to read, for now, 2 pins (in a future I'll want 4, max). I have found PIO registrers are usefull for my case. I read the datasheet, and many posts, but many of them focus in the writing and output function, instead of Input.

I made a simple sketch to see what PIO_PDSR gives, in the monitor it prints 2147483646 (i dont know what it means). And nothing was connected to the arduino (just a test, I thought that the monitor would print a binary or something like that)

Code: [Select]

void setup() {
  Serial.begin(9600);
 
  pinMode(44,INPUT);
  pinMode(46,INPUT);
}

void loop() {
  unsigned long int stat = PIOC->PIO_PDSR;
  Serial.println(stat);
}


So I need help with what should I expect from PIO_PDSR, with the code, or with more information like posts and/or guides.

I've read this posts:
https://forum.arduino.cc/index.php?topic=129868.0
http://forum.arduino.cc/index.php?topic=260731.0
http://forum.arduino.cc/index.php?topic=299504.0

Also I'm kinda new with the arduino language (c/c++), but i know stuff so dont worry about the programming level.

Thanks in advance!


ard_newbie

From Sam3x datasheet page 624:

31.5.8 Inputs

The level on each I/O line can be read through PIO_PDSR (Pin Data Status Register). This register indicates the level of the I/O lines regardless of their configuration, whether uniquely as an input or driven by the PIO controller or driven by a peripheral.

Reading the I/O line levels requires the clock of the PIO controller to be enabled, otherwise PIO_PDSR reads the levels present on the I/O line at the time the clock was disabled.

Connect pull down resistors to Pin 44=PC19 and PIn 46=PC17, and try this code :


Code: [Select]

void setup() {
  Serial.begin(250000);
  /*
    Pin 44 = PC19
    Pin 46 = PC17
  */
  PMC->PMC_PCER0 = PMC_PCER0_PID13;   // PIOC power ON
}

void loop() {
 
  uint32_t status = PIOC->PIO_PDSR;
  boolean statusPin44 = status & (1 << 19);
  boolean statusPin46 = status & (1 << 17);
  Serial.print("status Pin44 = "); Serial.println(statusPin44);
  Serial.print("status Pin46 = "); Serial.println(statusPin46);
  delay(1000);
}



cristobalmr

Thanks! that worked. I thought that the clock of the PIO was always enabled, and with your answer you cleared my mind about PIO_PDSR; it keeps all the values as a binary and doing that logic operation you get the value of a specific pin.

Again, thank you so much!

Go Up