python exit code 1602_树莓派上使用 LCD1602 显示基本状态

import time

import smbus

import logx

import logging

BUS = smbus.SMBus(1)

LCD_ADDR = 0x27

BLEN = 1 #turn on/off background light

def turn_light(key):

global BLEN

BLEN = key

if key ==1 :

BUS.write_byte(LCD_ADDR ,0x08)

logging.info('LCD executed turn on BLight')

else:

BUS.write_byte(LCD_ADDR ,0x00)

logging.info('LCD executed turn off BLight')

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_lcd():

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

logging.info('LCD init over')

BUS.write_byte(LCD_ADDR ,0x08)

logging.info('LCD turning on BLight')

except:

return False

else:

return True

def clear_lcd():

send_command(0x01) # Clear Screen

def print_lcd(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))

if __name__ == '__main__':

init_lcd()

print_lcd(0, 0, 'Hello, world!')

print_lcd(8, 1, 'by Jerry')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值