Advertisement
Guest User

Untitled

a guest
Aug 28th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import RPi.GPIO as GPIO
  4. import time
  5.  
  6. GPIO.setmode(GPIO.BOARD)
  7.  
  8. # Data and address pins
  9. # MSB first
  10.  
  11. # G23 G24 G10 G09 G25 G11 G08 G07 # BCM
  12. # MOSI MISO SCK CE0 CE1 # alt functions
  13. data_pins = [16, 18, 19, 21, 22, 23, 24, 26] # board
  14. # O7 O6 O5 O4 O3 O2 O1 O0 # chip
  15. # 21 20 19 18 17 15 14 13 # AT27C080
  16.  
  17. assert len(data_pins) == 8, "must have exacty 8 data pins (8 bits/byte)"
  18.  
  19. # G19 G16 G26 G20 G21 # BCM
  20. # # alt functions
  21. address_pins = [35, 36, 37, 38, 40] # board
  22. # A4 A3 A2 A1 A0 # chip
  23. # 8 9 10 11 12 # AT27C080
  24.  
  25. GPIO.setup(data_pins, GPIO.IN)
  26. GPIO.setup(address_pins, GPIO.OUT, initial=GPIO.LOW)
  27.  
  28. def read_byte():
  29. byte = 0
  30. for i in range(len(data_pins)):
  31. bit = GPIO.input(data_pins[i])
  32. byte |= bit * 2**(len(data_pins)-1-i)
  33. #print i,bit,data_pins[i]
  34. return byte
  35.  
  36. def set_address(address):
  37. assert address < 2**len(address_pins), "attempted to set address %.2x beyond %d address pins" % (address, len(address_pins))
  38. for i in range(len(address_pins)):
  39. bit = (address & 2**(len(address_pins)-1-i)) != 0
  40. GPIO.output(address_pins[i], bit)
  41. #print i,bit,address_pins[i]
  42.  
  43. for address in range(0, 2**len(address_pins)):
  44. set_address(address)
  45. if address % 4 == 0: print
  46. print "%.4x =" % (address,),
  47. #time.sleep(0.5)
  48. #byte = read_byte()
  49. #print "%.4x = %.2x" % (address, byte)
  50. time.sleep(0.2)
  51. seen = {}
  52. for i in range(20):
  53. byte = read_byte()
  54. if not seen.has_key(byte): seen[byte] = 0
  55. seen[byte] += 1
  56. print "%.2x " % (byte,),
  57. time.sleep(0.1)
  58. print " ",
  59. for saw in sorted(seen.keys()):
  60. print (str(seen[saw]) + "x") + "%.2x=%s " % (saw, bin(saw)[2:]),
  61. print
  62. time.sleep(0.2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement