树莓派 1602A显示屏[通俗易懂]

树莓派 1602A显示屏[通俗易懂]树莓派1602A显示屏下面代码本人测试过没有问题,开始运行完屏幕上什么也没有显示出来,后来调整了下电位器可以了,所以大家如果按着下面的做出来之后运行什么也没有显示的话,记得调整下电位器[img]http://dl2.iteye.com/upload/attachment/0128/7783/cefa073e-97ee-3d45-bddf-d7a878c232a1.png[/img…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺
树莓派 1602A显示屏

下面代码本人测试过没有问题,开始运行完屏幕上什么也没有显示出来,后来调整了下电位器可以了,所以大家如果按着下面的做出来之后运行什么也没有显示的话,记得调整下电位器

[img]http://dl2.iteye.com/upload/attachment/0128/7783/cefa073e-97ee-3d45-bddf-d7a878c232a1.png[/img]

LCD1602接线说明

VSS,接地

VDD,接5V电源

VO,液晶对比度调节,接电位器中间的引脚,电位器两边的引脚分别接5V和接地。

RS,寄存器选择,接GPIO14

RW,读写选择,接地,表示写模式

EN,使能信号,接GPIO15

D0,数据位0,4位工作模式下不用,不接

D1,数据位1,4位工作模式下不用,不接

D2,数据位2,4位工作模式下不用,不接

D3,数据位3,4位工作模式下不用,不接

D4,数据位4,接GPIO17

D5,数据位5,接GPIO18

D6,数据位6,接GPIO27

D7,数据位7,接GPIO22

A,液晶屏背光+,接5V

K,液晶屏背光-,接地

一共就两个文件

lcd1602.py


#!/usr/bin/python

#
# based on code from lrvick and LiquidCrystal
# lrvic - https://github.com/lrvick/raspi-hd44780/blob/master/hd44780.py
# LiquidCrystal - https://github.com/arduino/Arduino/blob/master/libraries/LiquidCrystal/LiquidCrystal.cpp
#

from time import sleep

class lcd1602:

# commands
LCD_CLEARDISPLAY = 0x01
LCD_RETURNHOME = 0x02
LCD_ENTRYMODESET = 0x04
LCD_DISPLAYCONTROL = 0x08
LCD_CURSORSHIFT = 0x10
LCD_FUNCTIONSET = 0x20
LCD_SETCGRAMADDR = 0x40
LCD_SETDDRAMADDR = 0x80

# flags for display entry mode
LCD_ENTRYRIGHT = 0x00
LCD_ENTRYLEFT = 0x02
LCD_ENTRYSHIFTINCREMENT = 0x01
LCD_ENTRYSHIFTDECREMENT = 0x00

# flags for display on/off control
LCD_DISPLAYON = 0x04
LCD_DISPLAYOFF = 0x00
LCD_CURSORON = 0x02
LCD_CURSOROFF = 0x00
LCD_BLINKON = 0x01
LCD_BLINKOFF = 0x00

# flags for display/cursor shift
LCD_DISPLAYMOVE = 0x08
LCD_CURSORMOVE = 0x00

# flags for display/cursor shift
LCD_DISPLAYMOVE = 0x08
LCD_CURSORMOVE = 0x00
LCD_MOVERIGHT = 0x04
LCD_MOVELEFT = 0x00

# flags for function set
LCD_8BITMODE = 0x10
LCD_4BITMODE = 0x00
LCD_2LINE = 0x08
LCD_1LINE = 0x00
LCD_5x10DOTS = 0x04
LCD_5x8DOTS = 0x00



def __init__(self, pin_rs=14, pin_e=15, pins_db=[17, 18, 27, 22], GPIO = None):
# Emulate the old behavior of using RPi.GPIO if we haven't been given
# an explicit GPIO interface to use
if not GPIO:
import RPi.GPIO as GPIO
self.GPIO = GPIO
self.pin_rs = pin_rs
self.pin_e = pin_e
self.pins_db = pins_db

self.GPIO.setmode(GPIO.BCM)
self.GPIO.setwarnings(False)
self.GPIO.setup(self.pin_e, GPIO.OUT)
self.GPIO.setup(self.pin_rs, GPIO.OUT)

for pin in self.pins_db:
self.GPIO.setup(pin, GPIO.OUT)

self.write4bits(0x33) # initialization
self.write4bits(0x32) # initialization
self.write4bits(0x28) # 2 line 5x7 matrix
self.write4bits(0x0C) # turn cursor off 0x0E to enable cursor
self.write4bits(0x06) # shift cursor right

self.displaycontrol = self.LCD_DISPLAYON | self.LCD_CURSOROFF | self.LCD_BLINKOFF

self.displayfunction = self.LCD_4BITMODE | self.LCD_1LINE | self.LCD_5x8DOTS
self.displayfunction |= self.LCD_2LINE

""" Initialize to default text direction (for romance languages) """
self.displaymode = self.LCD_ENTRYLEFT | self.LCD_ENTRYSHIFTDECREMENT
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode) # set the entry mode

self.clear()


def begin(self, cols, lines):

if (lines > 1):
self.numlines = lines
self.displayfunction |= self.LCD_2LINE
self.currline = 0


def home(self):

self.write4bits(self.LCD_RETURNHOME) # set cursor position to zero
self.delayMicroseconds(3000) # this command takes a long time!


def clear(self):

self.write4bits(self.LCD_CLEARDISPLAY) # command to clear display
self.delayMicroseconds(3000) # 3000 microsecond sleep, clearing the display takes a long time


def setCursor(self, col, row):

self.row_offsets = [ 0x00, 0x40, 0x14, 0x54 ]

if ( row > self.numlines ):
row = self.numlines - 1 # we count rows starting w/0

self.write4bits(self.LCD_SETDDRAMADDR | (col + self.row_offsets[row]))


def noDisplay(self):
""" Turn the display off (quickly) """

self.displaycontrol &= ~self.LCD_DISPLAYON
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)


def display(self):
""" Turn the display on (quickly) """

self.displaycontrol |= self.LCD_DISPLAYON
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)


def noCursor(self):
""" Turns the underline cursor on/off """

self.displaycontrol &= ~self.LCD_CURSORON
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)


def cursor(self):
""" Cursor On """

self.displaycontrol |= self.LCD_CURSORON
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)


def noBlink(self):
""" Turn on and off the blinking cursor """

self.displaycontrol &= ~self.LCD_BLINKON
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)


def noBlink(self):
""" Turn on and off the blinking cursor """

self.displaycontrol &= ~self.LCD_BLINKON
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)


def DisplayLeft(self):
""" These commands scroll the display without changing the RAM """

self.write4bits(self.LCD_CURSORSHIFT | self.LCD_DISPLAYMOVE | self.LCD_MOVELEFT)


def scrollDisplayRight(self):
""" These commands scroll the display without changing the RAM """

self.write4bits(self.LCD_CURSORSHIFT | self.LCD_DISPLAYMOVE | self.LCD_MOVERIGHT);


def leftToRight(self):
""" This is for text that flows Left to Right """

self.displaymode |= self.LCD_ENTRYLEFT
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode);


def rightToLeft(self):
""" This is for text that flows Right to Left """
self.displaymode &= ~self.LCD_ENTRYLEFT
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode)


def autoscroll(self):
""" This will 'right justify' text from the cursor """

self.displaymode |= self.LCD_ENTRYSHIFTINCREMENT
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode)


def noAutoscroll(self):
""" This will 'left justify' text from the cursor """

self.displaymode &= ~self.LCD_ENTRYSHIFTINCREMENT
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode)


def write4bits(self, bits, char_mode=False):
""" Send command to LCD """

self.delayMicroseconds(1000) # 1000 microsecond sleep

bits=bin(bits)[2:].zfill(8)

self.GPIO.output(self.pin_rs, char_mode)

for pin in self.pins_db:
self.GPIO.output(pin, False)

for i in range(4):
if bits[i] == "1":
self.GPIO.output(self.pins_db[::-1][i], True)

self.pulseEnable()

for pin in self.pins_db:
self.GPIO.output(pin, False)

for i in range(4,8):
if bits[i] == "1":
self.GPIO.output(self.pins_db[::-1][i-4], True)

self.pulseEnable()


def delayMicroseconds(self, microseconds):
seconds = microseconds / float(1000000) # divide microseconds by 1 million for seconds
sleep(seconds)


def pulseEnable(self):
self.GPIO.output(self.pin_e, False)
self.delayMicroseconds(1) # 1 microsecond pause - enable pulse must be > 450ns
self.GPIO.output(self.pin_e, True)
self.delayMicroseconds(1) # 1 microsecond pause - enable pulse must be > 450ns
self.GPIO.output(self.pin_e, False)
self.delayMicroseconds(1) # commands need > 37us to settle


def message(self, text):
""" Send string to LCD. Newline wraps to second line"""

for char in text:
if char == '\n':
self.write4bits(0xC0) # next line
else:
self.write4bits(ord(char),True)


if __name__ == '__main__':

lcd = lcd1602()
lcd.clear()
lcd.message("hello world!")

1602.py


#!/usr/bin/python

from lcd1602 import *
from datetime import *
import commands

def get_cpu_temp():
tmp = open('/sys/class/thermal/thermal_zone0/temp')
cpu = tmp.read()
tmp.close()
return '{:.2f}'.format( float(cpu)/1000 ) + ' C'

def get_gpu_temp():
tmp = commands.getoutput('vcgencmd measure_temp|awk -F= \'{print $2}\'').replace('\'C','')
gpu = float(tmp)
return '{:.2f}'.format( gpu ) + ' C'

def get_time_now():
return datetime.now().strftime(' %H:%M:%S\n %Y-%m-%d')

def get_ip_info():
return commands.getoutput('ifconfig wlan0|grep inet|awk -Faddr: \'{print $2}\'|awk \'{print $1}\'')

def get_mem_info():
total= commands.getoutput('free -m|grep Mem:|awk \'{print $2}\'')
free = commands.getoutput('free -m|grep cache:|awk \'{print $4}\'')
return 'MEM:\n ' + free +' / '+ total +' M'

lcd = lcd1602()
lcd.clear()

if __name__ == '__main__':

while(1):
lcd.clear()
lcd.message( get_ip_info() )
sleep(5)

lcd.clear()
lcd.message( get_time_now() )
sleep(5)

lcd.clear()
lcd.message( get_mem_info() )
sleep(5)

lcd.clear()
lcd.message( 'CPU: ' + get_cpu_temp()+'\n' )
lcd.message( 'GPU: ' + get_gpu_temp() )
sleep(5)

将以上两个文件保存在同一个目录下,运行1602.py即可打印信息到LCD上。

自己看看代码就会用了

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/190718.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • mac版idea插件安装位置

    mac版idea插件安装位置/Users/xxxx/Library/ApplicationSupport/JetBrains/IntelliJIdea2020.3/pluginsxxx替换为自己的用户名

    2022年5月22日
    184
  • 公司用的检查openweb服务是否启动的脚本

    公司用的检查openweb服务是否启动的脚本

    2022年3月8日
    32
  • 常见模拟电路设计 一(含仿真):方波、三角波、正弦波的互相发生「建议收藏」

    FPGA最近有些整累了,给大家开个模拟电路设计的坑,内含干货,请放心食用一、总体设计方案二、单元电路设计和原理说明2.1方波发生电路波形发生电路可以由集成运放芯片构成运算电路来实现。第一步的方波发生电路,可以由滞回比较器和RC电路构成,如图采用通用运放LM324芯片进行设计,C1和R1组成RC电路,而R2和R3以及LM324构成滞回比较器。D1、D2的作用是稳压。电路波形如下2.2三角波发生电路三角波发生器就是利用集成运放构成积分器,然后对方波信号进行运算,如图其中R4和C2的值

    2022年4月7日
    130
  • 面试java工程师的自我介绍_软件开发和程序员一样吗

    面试java工程师的自我介绍_软件开发和程序员一样吗程序员面试时一段短短的自我介绍,其实是为了揭开更深入的面谈而设计的。下面学习啦小编为你带来java程序员面试自我介绍范文的内容,希望你们喜欢。关于java程序员面试自我介绍范文篇一本人叫小冰,今年22岁,现在是吉林大学软件开发与信息管理专业方向的三年级学生,对软件开发怀有浓烈的兴趣,且对JAVA语言尤其熟悉,能熟练使用jsp、struts、struts2、sring2和hibernate3等流行的…

    2025年6月28日
    3
  • kafka php使用_PHP环境搭建推荐

    kafka php使用_PHP环境搭建推荐1. 下载二进制版本http://kafka.apache.org/downloads.html kafka_2.10-0.10.0.1.tgz 解压到 /home/deploy/tar/kafka_2.10-0.10.0.1  2.启动./bin/zookeeper-server-start.sh-daemonconfig/zookeeper.properties…

    2022年9月22日
    0
  • so文件在线加固加密_安卓so文件解密

    so文件在线加固加密_安卓so文件解密这篇是一系列的关于SO文件保护的自我理解,SO文件保护分为加固,混淆以及最近炒的比较火的虚拟机,由于本人菜鸟,无力分析虚拟机,我相信以后会有机会。。。加固就是将真正的so代码保护起来,不让攻击者那么轻易的发现,至于混淆,由于ART机制的介入,使得O-LLVM越来越火,这以后有机会再分析,这次主要是基于有源码的so文件保护,下次介绍无源码的so文件保护,废话不多说,开搞。在这之前首先对e

    2022年9月15日
    1

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号