Here is a snippet of code:
Note that I had print statements and a colourful display to help get this right, but they caused the problems. The printed result and the display said it was all working fine, but an oscilloscope on the GPIO pins told another story, of missed pulses. After I commented all the monitoring stuff out, it worked fine. I expected some delays when printing, but not GPIO just not responding. Is this a known problem?
Note that I use 74HCT logic at 5V, because the input threshold is 1.2V, like old-skool TTL, and is a better match for the GPIO levels. It's not driving the other way; I would use potential dividers of course.
Code: Select all
for p in range(0, 25):
#x=(p%5)
#y=(p//5)
#a[x,y]=phi[p]%16 #the 5x5 array now has thevalues in it which are rolled over
#pt0=Point(x*100,y*100)
#pt1=Point((x+1)*100,(y+1)*100)
#square=Rectangle(pt0,pt1)
#square.setFill(color_rgb(rval,gval,bval))
#c=phi[p]%16
#square.setFill(color_rgb(chroma[3*c],chroma[3*c+1],chroma[3*c+2]))
#square.draw(win)
#The values are pruned to the last 4 bits and sent down the shift registers to the counters.
#logic levels are inverted because GPIO outputs drive 74HCT04 invertors.
z=BitArray(uint=phi[p]%16,length=4)
#print (z.bin)
#print (z[0],z[1],z[2],z[3])
if p==12:
continue #element 12 always has zero hard wired
if z[0]:
GPIO.output(36, GPIO.LOW)
#print ('DATAon')# Turn on
else:
GPIO.output(36, GPIO.HIGH)
#print ('DATAoff')# Turn off
GPIO.output(38, GPIO.LOW) # Turn off, one short shift clock pulse
GPIO.output(38, GPIO.HIGH)
#print ('clock') # clock
if z[1]:
GPIO.output(36, GPIO.LOW)
#print ('DATAon')# Turn on
else:
GPIO.output(36, GPIO.HIGH)
#print ('DATAoff')# Turn off
GPIO.output(38, GPIO.LOW) # Turn off, one short shift clock pulse
GPIO.output(38, GPIO.HIGH)
#print ('clock') # clock
if z[2]:
GPIO.output(36, GPIO.LOW)
#print ('DATAon')# Turn on
else:
GPIO.output(36, GPIO.HIGH)
#print ('DATAoff')# Turn off
GPIO.output(38, GPIO.LOW) # Turn off, one short shift clock pulse
GPIO.output(38, GPIO.HIGH)
#print ('clock') # clock
if z[3]:
GPIO.output(36, GPIO.LOW)
#print ('DATAon')# Turn on
else:
GPIO.output(36, GPIO.HIGH)
#print ('DATAoff')# Turn off
GPIO.output(38, GPIO.LOW) # Turn off, one short shift clock pulse
GPIO.output(38, GPIO.HIGH)
#print ('clock') # clock
GPIO.output(40, GPIO.HIGH) # Turn off, one short pulse to load data from shift register into counters
GPIO.output(40, GPIO.LOW)
#print ('LOAD')
time.sleep(0.5)
#print('Reading MCP3008 values, press Ctrl-C to quit...')
value = mcp.read_adc(0)
# Print the ADC values.
print (value)
# Pause for half a second.This test script runs nicely, giving 4us pulses with 4us space before the pulse on the next pin:
Code: Select all
import RPi.GPIO as GPIO # Import Raspberry Pi GPIO library
#logic levels are inverted because GPIO outputs drive 74HCT04 invertors.
GPIO.setwarnings(False) # Ignore warning for now
GPIO.setmode(GPIO.BOARD) # Use physical pin numbering
GPIO.setup(36, GPIO.OUT, initial=GPIO.HIGH) # Set pin 36 to be an output pin and set initial value to low (off)
GPIO.setup(38, GPIO.OUT, initial=GPIO.HIGH) # Set pin 38 to be an output pin and set initial value to low (off)
GPIO.setup(40, GPIO.OUT, initial=GPIO.LOW) # Set pin 40 to be an output pin and set initial value to low (off)
while True:
GPIO.output(36, GPIO.LOW) # Turn off, one short pulse
GPIO.output(36, GPIO.HIGH)
GPIO.output(38, GPIO.LOW) # Turn off, one short pulse
GPIO.output(38, GPIO.HIGH)
GPIO.output(40, GPIO.LOW) # Turn off, one short pulse
GPIO.output(40, GPIO.HIGH)[moderator added code tags]