0

I'm new to Python and Raspberry Pi, so please bear with me.

I would like to intercept the bytes of data being sent to a 74HC595 switch register. GPIO pin 23 on my RPi is connected to the data pin of the switch register, GPIO pin 24 is connected to the switch clock pin and GPIO pin 25 to the memory clock (latch) pin.

The data being sent to the 74HC595 is activating one of the 74HC595 output pins at a time, sequentially, and pausing briefly between. That program is from my SunFounder kit and the code for that can be found here: Lesson 10 Driving LEDs by 74HC595.

After doing some research, I ended up with WiringPi wrapped in Python (whether WiringPi is appropriate for my need, I don't know). When I run my code below, I get many "0" values being returned, a few "255" and an occasional "63". I expected WiringPi to return an 8-bit value every time a byte is sent to the 74HC595.

Any suggestions?

#!/usr/bin/env python
#Reads the serial input sent to a 595 chip
import wiringpi

DATAPIN = 23
SHIFTCLOCK = 24
LATCHCLK = 25

#Set each of the GPIO pins to input mode
wiringpi.pinMode(DATAPIN, 0)
wiringpi.pinMode(SHIFTCLOCK, 0)
wiringpi.pinMode(LATCHCLK, 0)

wiringpi.wiringPiSetupSys()

def loop():
    while True:
            BITVALUE = wiringpi.shiftIn(DATAPIN, SHIFTCLOCK, 0)
            print BITVALUE

try:
    loop()

Example output:

0
0
0
255
0
0
255
0
0
0
0
63
0
0
255
0
  • What data rate are you trying to capture? I.e. how many times does the bit clock change per second? – joan Mar 27 '16 at 8:29
  • The Python code that sends data to the 595 uses a delay of .001 between each bit. I'm not entirely sure how to convert that to an actual data rate. – Paul Mar 27 '16 at 12:13
  • That's a 1000 bits per second which is luckily very slow in computer terms so you will be able to monitor the data quite easily. – joan Mar 27 '16 at 12:48
  • Agreed, and the script I created to read the data (above) doesn't have any delay. Perhaps that's why I'm getting so many 0's. I feel like my script needs to sync with the clock on the 595 but that's where I get lost. – Paul Mar 27 '16 at 13:05
  • I had a look at wiringPi.shiftIn. It sets the clock to trigger a bit so it is not in fact any use as a monitor. A monitor needs to respond to the clock rather than control it. – joan Mar 27 '16 at 13:40
0

Here is some code which uses my pigpio library to monitor the data and clocks.

It assumes you are using standard shift register signal levels.

Three values are printed per latch.

  1. the number of bits
  2. the data assuming most significant bit first format
  3. the data assuming least significant bit first format
#!/usr/bin/env python

# shiftIn.py
# 2016-03-27
# Public Domain

import time

import pigpio # http://abyz.me.uk/rpi/pigpio/python.html

DATA=23
DATACLK=24
RCLK=25

bit = 0
val = 0
nbits = 0

def cbf(g, l, t):
   global bit, val, nbits
   if g == DATA:
      bit = l
   elif g == DATACLK:
      nbits += 1
      val <<= 1
      val |= bit
   elif g == RCLK:
      val2 = 0
      for i in range(nbits):
         if val & (1<<i):
            val2 |= (1<<(nbits-i-1))
      print(nbits, val, val2)
      nbits = 0
      val = 0

pi = pigpio.pi()

if not pi.connected:
   exit(0)

cb1 = pi.callback(DATA, pigpio.EITHER_EDGE, cbf)
cb2 = pi.callback(DATACLK, pigpio.RISING_EDGE, cbf)
cb3 = pi.callback(RCLK, pigpio.FALLING_EDGE, cbf)

time.sleep(600)

cb1.cancel()
cb2.cancel()
cb3.cancel()

pi.stop()
  • Thanks Joan, this looks like what I'm after. I'll give it a shot later today and let you know how it goes. – Paul Mar 27 '16 at 13:17
  • Joan, this worked perfectly. Thank you for sharing pigpio. – Paul Mar 28 '16 at 3:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.