Advertisement
AverageMan

Average Man's Segment Test

May 9th, 2014
705
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.97 KB | None | 0 0
  1. #! /usr/bin/python
  2.  
  3. #This code tests my temperature monitor display using 7-segment display modules with 2x MCP23017 chips
  4. #(see http://AverageManVsRaspberryPi.com for the full blog)
  5. #Each LED within each 7-segment display should light up in turn, confirming correct sequence (see blog for sequence)
  6. #
  7. #MCP23017 ports can be addressed as followed:
  8. #
  9. #port 0 = 1
  10. #port 1 = 2
  11. #port 2 = 4
  12. #port 3 = 8
  13. #port 4 = 16
  14. #port 5 = 32
  15. #port 6 = 64
  16. #port 7 = 128
  17. #
  18. #MCP23017 has 2 banks/ports, A and B. Each bank has ports 0-7 as above.
  19. #
  20.  
  21. #Imports
  22. import smbus
  23. import sys
  24. import getopt
  25. import time
  26. import os
  27.  
  28. #Set up SMBus
  29. bus = smbus.SMBus(0)  #for revision 2 board...use 1
  30.  
  31. #Set the I2C address
  32. address1 = 0x20 (Address pins all to GND)
  33. address2 = 0x21 (First 2 address pins to GND, last address pin to 5v)
  34.  
  35. # Set both banks to outputs
  36. bus.write_byte_data(address1,0x00,0x00)
  37. bus.write_byte_data(address1,0x01,0x00)
  38. bus.write_byte_data(address2,0x00,0x00)
  39. bus.write_byte_data(address2,0x01,0x00)
  40.  
  41. #Bank mapping
  42. bank1 = 1 #Port A on 0x20
  43. bank2 = 2 #Port B on 0x20
  44. bank3 = 3 #Port A on 0x21
  45.  
  46. #More Bank Mapping
  47. def set_led(data,bank):
  48.   if bank == 1:
  49.    bus.write_byte_data(address1,0x12,data) #Bank 1 = MCP port A
  50.   if bank == 2:
  51.    bus.write_byte_data(address1,0x13,data) #Bank 2 = MCP port B
  52.   if bank == 3:
  53.    bus.write_byte_data(address2,0x12,data) #Bank 3 = MCP port A (second chip)
  54.  
  55. #Clear all Segments
  56.  
  57. set_led(0,bank1)
  58. set_led(0,bank2)
  59. set_led(0,bank3)
  60. time.sleep(2)
  61.  
  62. def main():
  63.  
  64.   count = 1
  65.   while 1:
  66.     set_led(count,bank1)
  67.     time.sleep(2)
  68.     set_led(count,bank2)
  69.     time.sleep(2)
  70.     set_led(count,bank3)
  71.     time.sleep(2)
  72.    
  73.     #Clear all Segments
  74.     set_led(0,bank1)
  75.     set_led(0,bank2)
  76.     set_led(0,bank3)
  77.     time.sleep(2)
  78.    
  79.     #Add count to count, to get the next port number
  80.     count = count + count
  81.    
  82.     #Check for last segment
  83.     if count == 256:
  84.       return
  85.      
  86. if __name__ == "__main__":
  87.    main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement