Hi, I'm using Arduino Due board which has 84MHz internal clock
Also, I know that microcontroller chip of Due board is sam3x-32bit
I am currently trying to read signal generated by another chip.
this external chip has 3bit-2MHz output and 2MHz clock signal output as well.
Arduino Due reads 2Mhz clock
First, I would like to detect rising edge of external 2MHz clock (at pin25).
Then, if clock is at rising edge, read from designated pins(pin33, pin34, pin35) and record it to the buffer array.
My trial of Arduino Due code is below,
byte buf[1000];
byte curr;
byte prev;
unsigned long before=0;
unsigned long after=0;
void setup(){
Serial.begin(115200);
pinMode(25,INPUT); // external clock input, PD0 pin
pinMode(33,INPUT); // data input, PC1 pin
pinMode(34,INPUT); // data input, PC2 pin
pinMode(35,INPUT); // data input, PC3 pin
}
void loop(){
int j;
// initialize buffer
for(j=0;j<1000;j++){
buf[j]=0;
}
j=0;
before = micros();
for(;j<1000;){
if( (curr=REG_PIOD_PDSR & 0x01) && (prev==0) ){ // if clock is at rising edge
buf[j++] = REG_PIOC_PDSR & 0x0E; // read one sample on PC1, PC2, PC3
}
prev = curr;
}
after = micros();
Serial.println(after-before);
}
expected value for time duration, after-before, is 500.
But, if you implement this code, print value is 1000, not 500.
(it seems Arduino Due detects rising edge of external clock every 2 cycles)
So, I slightly changed my code by replacing 'if clock is at rising edge' with 'if clock is HIGH'
before = micros();
for(;j<1000;){
[u][b] if( (curr=REG_PIOD_PDSR & 0x01) ){ // if clock is HIGH[/b][/u]
buf[j++] = REG_PIOC_PDSR & 0x0E; // read one sample on PC1, PC2, PC3
}
}
after = micros();
Thus, I could get print value 500. That means reading data with 2MHz was possible.
Anyone can help me with detecting rising edge problem above?
Thanks.
cf)
I have tried to find out calculation time for each line below.
if( (curr=REG_PIOD_PDSR & 0x01) )
;
and
if( (curr=REG_PIOD_PDSR & 0x01) && (prev==0) )
;
prev = curr;
both has approximately 33ns calculation time
while
for(j=0;j<1000;j++){
if( (curr=REG_PIOD_PDSR & 0x01) && (prev==0) ) {
;
}
prev = curr;
}
has 133 usec, (133ns per one j-value)
buf[j++] = REG_PIOC_PDSR & 0x0E;
has approximately 95ns.
while
for(j=0;j<1000;j++){
buf[j++] = REG_PIOC_PDSR & 0x0E;
}
has approximately 195usec, (195ns per one j-value)
it seems for-loop adds 100ns calculation time approximately.
(84MHz / 8 =10.5MHz -> 1/10.5MHz ~ 100ns I guess...?)