DUE registers for reading

Hello,

I am trying to make faster the code fo reading a HX711.
There is this line that goes:
result |= digitalRead(A0) << i;

If I change it for:
result |= ((PIOA->PIO_PDSR & (0x1u << dat_pin)) ?1:0) << i;
I don't get the same result.

Do you know why?

All the code where you can see the transformations (old are commented, new use registers):

void loop() {

  if(!(PIOA->PIO_PDSR & (0x1u << dat_pin))) {//READ
  //if(!digitalRead(A0)) {

    int result = 0;

    //Las lecturas no pueden ser interrumpidas
    //noInterrupts(); //quizás poner prioridad top.

    //PIOB->PIO_SODR = PIO_PB17;//A8 HIGH

    //noInterrupts();
    
    for (int i = 23; i >= 0; --i) {
      PIOA->PIO_SODR = 1u << clk_pin;//WRITE HIGH
      //digitalWrite(A1, HIGH);
      result |= ((PIOA->PIO_PDSR & (0x1u << dat_pin)) ?1:0) << i; //READ Final result = 49250.
      //result |= digitalRead(A0) << i; //READ Final result = 98500.
      PIOA->PIO_CODR = 1u << clk_pin;//WRITE LOW
      //digitalWrite(A1, LOW);
    }

    //Ciclo extra que determina la ganancia de la iguiente lectura: 1 ciclo = 128.
    PIOA->PIO_SODR = 1u << clk_pin;//WRITE HIGH
    PIOA->PIO_CODR = 1u << clk_pin;//WRITE LOW
    //digitalWrite(A1, HIGH);
    //digitalWrite(A1, LOW);

    //interrupts();

    //Volvemos a habilitar interrupts.
    //interrupts();

    //Rellena con unos o ceros por signo.
    result |= (result & 1UL << 23 ? 0xFFUL : 0x00UL) << 24;

    Serial.println(result);
  }
}

IMPORTANT: the result I get with the line using the register is half the result if I use digitalRead. The other lines I can switch between using digitalRead/Writes and registers without problem.

Anyone know the difference between the 2 lines?

I found the answer.
It's the same but it waz too fast.

I needed to ad nops to lower the pace until reach 200ns for every high and low state.

Hope that helps somobody someday.

Have a nice coding day!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.