raspberry pi (12) I2C LCD1602液晶显示器,DS1302事实时钟

1.I2C LCD1602液晶显示器

#!/usr/bin/env python

import time
import smbus

BUS = smbus.SMBus(1)

def write_word(addr, data):
	global BLEN
	temp = data
	if BLEN == 1:
		temp |= 0x08
	else:
		temp &= 0xF7
	BUS.write_byte(addr ,temp)

def send_command(comm):
	# Send bit7-4 firstly
	buf = comm & 0xF0
	buf |= 0x04               # RS = 0, RW = 0, EN = 1
	write_word(LCD_ADDR ,buf)
	time.sleep(0.002)
	buf &= 0xFB               # Make EN = 0
	write_word(LCD_ADDR ,buf)

	# Send bit3-0 secondly
	buf = (comm & 0x0F) << 4
	buf |= 0x04               # RS = 0, RW = 0, EN = 1
	write_word(LCD_ADDR ,buf)
	time.sleep(0.002)
	buf &= 0xFB               # Make EN = 0
	write_word(LCD_ADDR ,buf)

def send_data(data):
	# Send bit7-4 firstly
	buf = data & 0xF0
	buf |= 0x05               # RS = 1, RW = 0, EN = 1
	write_word(LCD_ADDR ,buf)
	time.sleep(0.002)
	buf &= 0xFB               # Make EN = 0
	write_word(LCD_ADDR ,buf)

	# Send bit3-0 secondly
	buf = (data & 0x0F) << 4
	buf |= 0x05               # RS = 1, RW = 0, EN = 1
	write_word(LCD_ADDR ,buf)
	time.sleep(0.002)
	buf &= 0xFB               # Make EN = 0
	write_word(LCD_ADDR ,buf)

def init(addr, bl):
#	global BUS
#	BUS = smbus.SMBus(1)
	global LCD_ADDR
	global BLEN
	LCD_ADDR = addr
	BLEN = bl
	try:
		send_command(0x33) # Must initialize to 8-line mode at first
		time.sleep(0.005)
		send_command(0x32) # Then initialize to 4-line mode
		time.sleep(0.005)
		send_command(0x28) # 2 Lines & 5*7 dots
		time.sleep(0.005)
		send_command(0x0C) # Enable display without cursor
		time.sleep(0.005)
		send_command(0x01) # Clear Screen
		BUS.write_byte(LCD_ADDR, 0x08)
	except:
		return False
	else:
		return True

def clear():
	send_command(0x01) # Clear Screen

def openlight():  # Enable the backlight
	BUS.write_byte(0x27,0x08)
	BUS.close()

def write(x, y, str):
	if x < 0:
		x = 0
	if x > 15:
		x = 15
	if y <0:
		y = 0
	if y > 1:
		y = 1

	# Move cursor
	addr = 0x80 + 0x40 * y + x
	send_command(addr)

	for chr in str:
		send_data(ord(chr))

#!/usr/bin/env python
import LCD1602
import time

def setup():
    LCD1602.init(0x27, 1)    # init(slave address, background light)
    LCD1602.write(0, 0, 'Hello world')
    LCD1602.write(0, 1, 'Raspberry Pi')
    time.sleep(2)

def destroy():
    pass    

if __name__ == "__main__":
    try:
        setup()
        while True:
            pass
    except KeyboardInterrupt:
        destroy()

raspberry pi (12) I2C LCD1602液晶显示器,DS1302事实时钟_第1张图片
2.DS1302事实时钟

#!/usr/bin/python
from datetime import datetime
import ds1302


class DS1302:
	def __init__(self, rangechecks=True):
		self.rangechecks = rangechecks
		ds1302.init_clock()

	def get_datetime(self):
		if not self.check_sanity():
			# if clock are insane: try to reset it
			self.reset_clock()
			# if clock are still insane: return None
			if not self.check_sanity():
				return None
		year, month, date = ds1302.get_date()
		hour, minute, second = ds1302.get_time()
		if self.rangechecks:
			if year < 2000 or year > 3000:
				ds1302.set_date(2000, month, date)
				return self.get_datetime()
			if month not in range(1, 13):
				ds1302.set_date(year, 1, date)
				return self.get_datetime()
			if date not in range(1, 32):
				ds1302.set_date(year, month, 1)
				return self.get_datetime()
			if hour not in range(0, 24):
				ds1302.set_time(0, minute, second)
				return self.get_datetime()
			if minute not in range(0, 60):
				ds1302.set_time(hour, 0, second)
				return self.get_datetime()
			if second not in range(0, 60):
				ds1302.set_time(hour, minute, 0)
				return self.get_datetime()
		return datetime(year, month, date,
						hour, minute, second)


	def set_datetime(self, dt):
		if not self.check_sanity():
			# if clock are insane: try to reset it
			self.reset_clock()
			# if clock are still insane: return False
			if not self.check_sanity():
				return False
		ds1302.set_date(dt.year, dt.month, dt.day)
		ds1302.set_time(dt.hour, dt.minute, dt.second)
		return True

	def check_sanity(self):
		"check sanity of a clock. returns True if clock is sane and False otherwise"
		year, month, date = ds1302.get_date()
		hours, mins, secs = ds1302.get_time()
		if year == 2000 or month == 0 or date == 0:
			return False
		if secs == 80:
			return False
		return True

	def reset_clock(self):
		ds1302.reset_clock()

def format_time(dt):
	if dt is None:
		return ""
	fmt = "%m/%d/%Y %H:%M:%S"
	return dt.strftime(fmt)
	
def parse_time(s):
	fmt = "%m/%d/%Y %H:%M"
	return datetime.strptime(s, fmt)

#!/usr/bin/env python
#-----------------------------------------------------
#
#        This is a program for DS1302 RTC Module.
#    It provide precision timmer.
#
#        This program depend on rpi_time.py. 
#
#        ds1302 Module                Pi
#            VCC ------------------ 5 V (Must be 5v)
#            GND ------------------ GND
#            SCL ---------------- Pin 16
#            SDA ---------------- Pin 18
#            RST ---------------- Pin 22
#
#-----------------------------------------------------
from datetime import datetime
import ds1302
import rpi_time
import time
import LCD1602

rtc = rpi_time.DS1302()

def setup():
    print ''
    print ''
    print rtc.get_datetime()
    print ''
    print ''
    a = raw_input( "Do you want to setup date and time?(y/n) ")
    if a == 'y' or a == 'Y':
        date = raw_input("Input date:(YYYY MM DD) ")
        time = raw_input("Input time:(HH MM SS) ")
        date = date.split()
        time = time.split()
        print ''
        print ''
        ds1302.set_date(int(date[0]), int(date[1]), int(date[2]))
        ds1302.set_time(int(time[0]), int(time[1]), int(time[2]))
        dt = rtc.get_datetime()
        print "You set the date and time to:", dt

    LCD1602.init(0x27, 1)    # init(slave address, background light)

def loop():
    while True:
        a = rtc.get_datetime()
        s = rpi_time.format_time(a)
        print s
        splits = s.split(' ')
        print splits[0]
        LCD1602.write(0, 0, splits[0])
        LCD1602.write(0, 1, splits[1])
        time.sleep(0.5)

def destory():
    GPIO.cleanup()                # Release resource

if __name__ == '__main__':        # Program start from here
    setup()
    try:
        loop()
    except KeyboardInterrupt:      # When 'Ctrl+C' is pressed, the child program destroy() will be  executed.
        destory()

raspberry pi (12) I2C LCD1602液晶显示器,DS1302事实时钟_第2张图片

你可能感兴趣的:(python3,raspberry,pi)