Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    Translate Arduino code to microPython

    Scheduled Pinned Locked Moved MicroPython
    arduinomicropythonissues
    4 Posts 2 Posters 54 Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • J Offline
      JHN
      last edited by

      Hello, I have an issue when i tried to translate Arduino code to microPyhton, This is the code that i tried to translate:

      /*
       * SN74HC165N_shift_reg
       *
       * Program to shift in the bit values from a SN74HC165N 8-bit
       * parallel-in/serial-out shift register.
       *
       * This sketch demonstrates reading in 16 digital states from a
       * pair of daisy-chained SN74HC165N shift registers while using
       * only 4 digital pins on the Arduino.
       *
       * You can daisy-chain these chips by connecting the serial-out
       * (Q7 pin) on one shift register to the serial-in (Ds pin) of
       * the other.
       * 
       * Of course you can daisy chain as many as you like while still
       * using only 4 Arduino pins (though you would have to process
       * them 4 at a time into separate unsigned long variables).
       * 
      */
      
      /* How many shift register chips are daisy-chained.
      */
      #define NUMBER_OF_SHIFT_CHIPS   3
      
      /* Width of data (how many ext lines).
      */
      #define DATA_WIDTH   NUMBER_OF_SHIFT_CHIPS * 8
      
      /* Width of pulse to trigger the shift register to read and latch.
      */
      #define PULSE_WIDTH_USEC   5
      
      /* Optional delay between shift register reads.
      */
      #define POLL_DELAY_MSEC   1
      
      /* You will need to change the "int" to "long" If the
       * NUMBER_OF_SHIFT_CHIPS is higher than 2.
      */
      #define BYTES_VAL_T unsigned long
      
      int ploadPin        = 5;  // BLUE - Connects to /PL aka Parallel load aka latch pin the 165
      int clockEnablePin  = 9;  // not used - Connects to Clock Enable pin the 165 (should always be low)
      int dataPin         = 6; // YELLOW - Connects to the Q7 aka MISO pin the 165
      int clockPin        = 7; // PARPEL - Connects to the Clock pin the 165
      
      BYTES_VAL_T pinValues;
      BYTES_VAL_T oldPinValues;
      
      /* This function is essentially a "shift-in" routine reading the
       * serial Data from the shift register chips and representing
       * the state of those pins in an unsigned integer (or long).
      */
      BYTES_VAL_T read_shift_regs()
      {
          long bitVal;
          BYTES_VAL_T bytesVal = 0;
      
          /* Trigger a parallel Load to latch the state of the data lines,
          */
      //    digitalWrite(clockEnablePin, HIGH);
          digitalWrite(ploadPin, LOW);
          //delayMicroseconds(PULSE_WIDTH_USEC);
          digitalWrite(ploadPin, HIGH);
      //    digitalWrite(clockEnablePin, LOW);
      
          /* Loop to read each bit value from the serial out line
           * of the SN74HC165N.
          */
          for(int i = 0; i < DATA_WIDTH; i++)
          {
              bitVal = digitalRead(dataPin);
      
              /* Set the corresponding bit in bytesVal.
              */
              bytesVal |= (bitVal << ((DATA_WIDTH-1) - i));
      
              /* Pulse the Clock (rising edge shifts the next bit).
              */
              digitalWrite(clockPin, HIGH);
              //delayMicroseconds(PULSE_WIDTH_USEC);
              digitalWrite(clockPin, LOW);
          }
      
          return(bytesVal);
      }
      
      /* Dump the list of zones along with their current status.
      */
      void display_pin_values()
      {
          Serial.print("Pin States:\r\n");
      
          for(int i = 0; i < DATA_WIDTH; i++)
          {
              Serial.print("  Pin-");
              Serial.print(i);
              Serial.print(": ");
      
              if((pinValues >> i) & 1)
                  Serial.print("HIGH");
              else
                  Serial.print("LOW");
      
              Serial.print("\r\n");
          }
      
          Serial.print("\r\n");
      }
      
      void setup()
      {
          Serial.begin(115200);
      
          /* Initialize our digital pins...
          */
          pinMode(ploadPin, OUTPUT);
          pinMode(clockEnablePin, OUTPUT);
          pinMode(clockPin, OUTPUT);
          pinMode(dataPin, INPUT);
      
          digitalWrite(clockPin, LOW);
          digitalWrite(ploadPin, HIGH);
          digitalWrite(clockEnablePin, LOW);
          /* Read in and display the pin states at startup.
          */
          pinValues = read_shift_regs();
          display_pin_values();
          oldPinValues = pinValues;
      }
      
      void loop()
      {
          /* Read the state of all zones.
          */
          pinValues = read_shift_regs();
      
          /* If there was a chage in state, display which ones changed.
          */
          if(pinValues != oldPinValues)
          {
              Serial.print("*Pin value change detected*\r\n");
              Serial.println(pinValues);
              display_pin_values();
              oldPinValues = pinValues;
          }
      
          delay(POLL_DELAY_MSEC);
      }
      

      My translated code:

      from machine import Pin
      import utime
      # How many shift register chips are daisy-chained.
      NUMBER_OF_SHIFT_CHIPS = 3
      
      # Width of data (how many ext lines)
      DATA_WIDTH = NUMBER_OF_SHIFT_CHIPS * 8
      
      # Width of pulse to trigger the shift register to read and latch.
      PULSE_WIDTH_USEC = 5
      
      # Optional delay between shift register reads.
      POLL_DELAY_MSEC = 1
      
      pinValues = 0
      oldPinValues = 0
      
      
      
      def read_shift_regs():
          bitVal = 0
          bytesVal = 0
          ploadPin.value(0)
          utime.sleep_us(PULSE_WIDTH_USEC)
          ploadPin.value(1)
      
          for i in range(DATA_WIDTH):
      
              bitVal = dataPin.value()
              # Set the corresponding bit in bytesVal.
              print(i,bitVal,bytesVal)
              bytesVal |= (bitVal << ((DATA_WIDTH-1) - i))
              clockPin.value(1)
              utime.sleep_us(PULSE_WIDTH_USEC)
              clockPin.value(0)
          return bytesVal
      
      def display_pin_values():
          print("Pin states: ")
          for i in range(DATA_WIDTH):
              if(pinValues >> i & 1):
                  print(" Pin-" ,i ,": HIGH")
              else:
                  print(" Pin-" ,i ,": LOW")
          print("\r\n")
      
      
      #Set up
      ploadPin = Pin('P2', mode=Pin.OUT) #Blue
      clockPin = Pin('P14', mode=Pin.OUT) #Purple
      dataPin = Pin('P4', mode=Pin.IN) #Yellow
      
      
      clockPin.value(0)
      ploadPin.value(1)
      pinValues = read_shift_regs()
      display_pin_values()
      oldPinValues = pinValues
      while True:
          pinValues = read_shift_regs()
          if(pinValues != oldPinValues):
              print("Pin value change detected*\r\n")
              print(pinValues)
              display_pin_values()
              oldPinValues = pinValues
          utime.sleep_ms(POLL_DELAY_MSEC)
      

      The issue is that i only get 0 from the sensors. So the serial communication does not work like i thought.

      1 Reply Last reply Reply Quote 0
      • G Offline
        Gijs
        last edited by

        Hi!
        s
        I checked your code and in python, it seems to be missing the clockEnablePin, if you set it to low, it activates the SN74HC165N chip
        I believe some of the pins on the esp32 device float high, but im not sure that is the issue in this case.
        Let me know if that makes it work!

        Gijs

        J 1 Reply Last reply Reply Quote 0
        • J Offline
          JHN
          last edited by

          This post is deleted!
          1 Reply Last reply Reply Quote 0
          • J Offline
            JHN @Gijs
            last edited by

            @Gijs

            Hello! that pin are not needed, infact my code is working, one hardware pin "p2" had some issue, it is constant 3.3V which means that the ploadPin are not toggled. The code awill hopefully be uploaded on Git!

            1 Reply Last reply Reply Quote 1

            Hello! It looks like you're interested in this conversation, but you don't have an account yet.

            Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

            With your input, this post could be even better 💗

            Register Login
            • 1 / 1
            • First post
              Last post

            Pycom on Twitter

            Pycom