Advertisement
Guest User

Python HD44780 LCD + DS18B20 | Error

a guest
Feb 22nd, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.46 KB | None | 0 0
  1. #LCD_RS = 14
  2. #LCD_E  = 8
  3. #LCD_D4 = 20
  4. #LCD_D5 = 7
  5. #LCD_D6 = 12
  6. #LCD_D7 = 16
  7. import RPi.GPIO as GPIO
  8. from time import sleep
  9. import os
  10. import time
  11. import datetime
  12. import glob
  13. from time import strftime
  14.  
  15. os.system('modprobe w1-gpio')
  16. os.system('modprobe w1-therm')
  17. temp_sensor = '/sys/bus/w1/devices/28-00000a19b171/w1_slave'
  18.  
  19. def tempRead():
  20.         t = open(temp_sensor, 'r')
  21.         lines = t.readlines()
  22.         t.close()
  23.  
  24.         temp_output = lines[1].find('t=')
  25.         if temp_output != -1:
  26.                 temp_string = lines[1].strip()[temp_output+2:]
  27.                 temp_c = float(temp_string)/1000.0
  28.         return round(temp_c,1)
  29.  
  30. class HD44780:
  31.  
  32.     def __init__(self, pin_rs=14, pin_e=8, pins_db=[20, 7, 12, 16]):
  33.  
  34.         self.pin_rs = pin_rs
  35.         self.pin_e = pin_e
  36.         self.pins_db = pins_db
  37.  
  38.         GPIO.setmode(GPIO.BCM)
  39.         GPIO.setup(self.pin_e, GPIO.OUT)
  40.         GPIO.setup(self.pin_rs, GPIO.OUT)
  41.         for pin in self.pins_db:
  42.             GPIO.setup(pin, GPIO.OUT)
  43.  
  44.         self.clear()
  45.  
  46.     def clear(self):
  47.         """ Blank / Reset LCD """
  48.  
  49.         self.cmd(0x33) # $33 8-bit mode
  50.         self.cmd(0x32) # $32 8-bit mode
  51.         self.cmd(0x28) # $28 8-bit mode
  52.         self.cmd(0x0C) # $0C 8-bit mode
  53.         self.cmd(0x06) # $06 8-bit mode
  54.         self.cmd(0x01) # $01 8-bit mode
  55.  
  56.     def cmd(self, bits, char_mode=False):
  57.         """ Send command to LCD """
  58.  
  59.         sleep(0.001)
  60.         bits=bin(bits)[2:].zfill(8)
  61.  
  62.         GPIO.output(self.pin_rs, char_mode)
  63.  
  64.         for pin in self.pins_db:
  65.             GPIO.output(pin, False)
  66.  
  67.         for i in range(4):
  68.             if bits[i] == "1":
  69.                 GPIO.output(self.pins_db[::-1][i], True)
  70.  
  71.         GPIO.output(self.pin_e, True)
  72.         GPIO.output(self.pin_e, False)
  73.  
  74.         for pin in self.pins_db:
  75.             GPIO.output(pin, False)
  76.  
  77.         for i in range(4,8):
  78.             if bits[i] == "1":
  79.                 GPIO.output(self.pins_db[::-1][i-4], True)
  80.  
  81.         GPIO.output(self.pin_e, True)
  82.         GPIO.output(self.pin_e, False)
  83.  
  84.     def message(self, text):
  85.         """ Send string to LCD. Newline wraps to second line"""
  86.  
  87.         for char in text:
  88.             if char == '\n':
  89.                 self.cmd(0xC0) # next line
  90.             else:
  91.                 self.cmd(ord(char),True)
  92.  
  93. if __name__ == '__main__':
  94.  
  95.     lcd = HD44780()
  96.  
  97.     while True:
  98.         lcd.clear()
  99.         temp = tempRead()
  100.         lcd.message(temp)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement