Advertisement
Wiefreak

Displayclass

Apr 7th, 2014
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.48 KB | None | 0 0
  1. #!/usr/bin/python
  2. import time
  3. import RPi.GPIO as GPIO
  4.  
  5. # Zuordnung der GPIO Pins (ggf. anpassen)
  6. DISPLAY_RS = 7
  7. DISPLAY_E  = 8
  8. DISPLAY_DATA4 = 25
  9. DISPLAY_DATA5 = 24
  10. DISPLAY_DATA6 = 23
  11. DISPLAY_DATA7 = 18
  12.  
  13.  
  14.  
  15.  
  16. DISPLAY_WIDTH = 16  # Zeichen je Zeile
  17. DISPLAY_LINE_1 = 0x80   # Adresse der ersten Display Zeile
  18. DISPLAY_LINE_2 = 0xC0   # Adresse der zweiten Display Zeile
  19. DISPLAY_LINE_3 = 0x94
  20. DISPLAY_LINE_4 = 0xD4
  21. DISPLAY_CHR = True
  22. DISPLAY_CMD = False
  23. E_PULSE = 0.00005
  24. E_DELAY = 0.00005
  25.  
  26. def main():
  27.     GPIO.setmode(GPIO.BCM)
  28.     GPIO.setup(DISPLAY_E, GPIO.OUT)
  29.     GPIO.setup(DISPLAY_RS, GPIO.OUT)
  30.     GPIO.setup(DISPLAY_DATA4, GPIO.OUT)
  31.     GPIO.setup(DISPLAY_DATA5, GPIO.OUT)
  32.     GPIO.setup(DISPLAY_DATA6, GPIO.OUT)
  33.     GPIO.setup(DISPLAY_DATA7, GPIO.OUT)
  34.  
  35.     display_init()
  36.  
  37.    
  38.  
  39.  
  40.  
  41.  
  42. def display_init():
  43.     lcd_byte(0x33,DISPLAY_CMD)
  44.     lcd_byte(0x32,DISPLAY_CMD)
  45.     lcd_byte(0x28,DISPLAY_CMD)
  46.     lcd_byte(0x0C,DISPLAY_CMD)  
  47.     lcd_byte(0x06,DISPLAY_CMD)
  48.     lcd_byte(0x01,DISPLAY_CMD)  
  49.  
  50. def lcd_string(message):
  51.     message = message.ljust(DISPLAY_WIDTH," ")  
  52.     for i in range(DISPLAY_WIDTH):
  53.       lcd_byte(ord(message[i]),DISPLAY_CHR)
  54.  
  55. def lcd_byte(bits, mode):
  56.     GPIO.output(DISPLAY_RS, mode)
  57.     GPIO.output(DISPLAY_DATA4, False)
  58.     GPIO.output(DISPLAY_DATA5, False)
  59.     GPIO.output(DISPLAY_DATA6, False)
  60.     GPIO.output(DISPLAY_DATA7, False)
  61.     if bits&0x10==0x10:
  62.       GPIO.output(DISPLAY_DATA4, True)
  63.     if bits&0x20==0x20:
  64.       GPIO.output(DISPLAY_DATA5, True)
  65.     if bits&0x40==0x40:
  66.       GPIO.output(DISPLAY_DATA6, True)
  67.     if bits&0x80==0x80:
  68.       GPIO.output(DISPLAY_DATA7, True)
  69.     time.sleep(E_DELAY)    
  70.     GPIO.output(DISPLAY_E, True)  
  71.     time.sleep(E_PULSE)
  72.     GPIO.output(DISPLAY_E, False)  
  73.     time.sleep(E_DELAY)      
  74.     GPIO.output(DISPLAY_DATA4, False)
  75.     GPIO.output(DISPLAY_DATA5, False)
  76.     GPIO.output(DISPLAY_DATA6, False)
  77.     GPIO.output(DISPLAY_DATA7, False)
  78.     if bits&0x01==0x01:
  79.       GPIO.output(DISPLAY_DATA4, True)
  80.     if bits&0x02==0x02:
  81.       GPIO.output(DISPLAY_DATA5, True)
  82.     if bits&0x04==0x04:
  83.       GPIO.output(DISPLAY_DATA6, True)
  84.     if bits&0x08==0x08:
  85.       GPIO.output(DISPLAY_DATA7, True)
  86.     time.sleep(E_DELAY)    
  87.     GPIO.output(DISPLAY_E, True)  
  88.     time.sleep(E_PULSE)
  89.     GPIO.output(DISPLAY_E, False)  
  90.     time.sleep(E_DELAY)  
  91.  
  92. def show(a,b,c,d)
  93.     lcd_byte(DISPLAY_LINE_1, DISPLAY_CMD)
  94.     lcd_string(a)
  95.     lcd_byte(DISPLAY_LINE_2, DISPLAY_CMD)
  96.     lcd_string(b)
  97.     lcd_byte(DISPLAY_LINE_3, DISPLAY_CMD)
  98.     lcd_string(c)
  99.     lcd_byte(DISPLAY_LINE_4, DISPLAY_CMD)
  100.     lcd_string(d)
  101.  
  102. if __name__ == '__main__':
  103.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement