Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #LCD_RS = 14
- #LCD_E = 8
- #LCD_D4 = 20
- #LCD_D5 = 7
- #LCD_D6 = 12
- #LCD_D7 = 16
- import RPi.GPIO as GPIO
- from time import sleep
- import os
- import time
- import datetime
- import glob
- from time import strftime
- os.system('modprobe w1-gpio')
- os.system('modprobe w1-therm')
- temp_sensor = '/sys/bus/w1/devices/28-00000a19b171/w1_slave'
- def tempRead():
- t = open(temp_sensor, 'r')
- lines = t.readlines()
- t.close()
- temp_output = lines[1].find('t=')
- if temp_output != -1:
- temp_string = lines[1].strip()[temp_output+2:]
- temp_c = float(temp_string)/1000.0
- return round(temp_c,1)
- class HD44780:
- def __init__(self, pin_rs=14, pin_e=8, pins_db=[20, 7, 12, 16]):
- self.pin_rs = pin_rs
- self.pin_e = pin_e
- self.pins_db = pins_db
- GPIO.setmode(GPIO.BCM)
- GPIO.setup(self.pin_e, GPIO.OUT)
- GPIO.setup(self.pin_rs, GPIO.OUT)
- for pin in self.pins_db:
- GPIO.setup(pin, GPIO.OUT)
- self.clear()
- def clear(self):
- """ Blank / Reset LCD """
- self.cmd(0x33) # $33 8-bit mode
- self.cmd(0x32) # $32 8-bit mode
- self.cmd(0x28) # $28 8-bit mode
- self.cmd(0x0C) # $0C 8-bit mode
- self.cmd(0x06) # $06 8-bit mode
- self.cmd(0x01) # $01 8-bit mode
- def cmd(self, bits, char_mode=False):
- """ Send command to LCD """
- sleep(0.001)
- bits=bin(bits)[2:].zfill(8)
- GPIO.output(self.pin_rs, char_mode)
- for pin in self.pins_db:
- GPIO.output(pin, False)
- for i in range(4):
- if bits[i] == "1":
- GPIO.output(self.pins_db[::-1][i], True)
- GPIO.output(self.pin_e, True)
- GPIO.output(self.pin_e, False)
- for pin in self.pins_db:
- GPIO.output(pin, False)
- for i in range(4,8):
- if bits[i] == "1":
- GPIO.output(self.pins_db[::-1][i-4], True)
- GPIO.output(self.pin_e, True)
- GPIO.output(self.pin_e, False)
- def message(self, text):
- """ Send string to LCD. Newline wraps to second line"""
- for char in text:
- if char == '\n':
- self.cmd(0xC0) # next line
- else:
- self.cmd(ord(char),True)
- if __name__ == '__main__':
- lcd = HD44780()
- while True:
- lcd.clear()
- temp = tempRead()
- lcd.message(temp)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement