7 Replies Latest reply on Jun 7, 2016 10:14 AM by if

    MCU: unexpected delays in loops

    if

      I am writing a serial interface to a HX711 IC. It has a very simple serial protocol: a data and a clock line. Every positive clock pulse will put a data bit on the output of the HX711.

      Data is 24 bits, but 1 to 3 extra pulses will have to follow to set some parameters of the HX711.

      I wrote an MCU function, to send 24 bits in a for loop, and read every bit. Then the configure bits in another loop.

      (The logical levels are inverted for the single transistor level-shifter.)

       

      The efficient pulse status between the set and clear instractions is measured about 10us on the oscilloscope.

      But once in a while a clock pulse will go wider in the data-reading for loop, randomly in H or L phase. Looks like something suspends the MCU. The lengthened pulse can be some 50-100us (hard to catch it).

      The longer cycle is a standby instruction for the HX711, so it is not welcome.

       

      What can be the reason of the timing disturbance in such simple loop?

       

      bool hx711_read(char *buffer, int data_port, int clock_port, int pulses)
      {
        if (!gpio_read(data_port)) return false; // hx711 not ready
      
          weight_t weight = 0;
      
          int i;
          for (i = 0; i < 24 ; ++i) {
          gpio_write(clock_port, 0);
          gpio_write(clock_port, 1);
      
              weight <<= 1;
              if(!gpio_read(data_port)) weight++;
          }
      
          // The 24 reading pulses were completed.
          // The rest will adjust the HX711 for the next reading.
          int j;
          for (j = 0; j < pulses-24; ++j) {
          gpio_write(clock_port, 0);
          gpio_write(clock_port, 1);
          }
          // We have the result in binary, now convert it to string.
          weight_to_hex((unsigned long) weight, buffer);
      
          return true;
      }