LCD1602 Driver For Raspberry Pi 3

树莓派3b的Lcd1602驱动,基于wiringPi编写。
目前仅实现基础功能。

lcd1602.h

/*
// This file is part of LCD1602 Driver project
// Last-Modified:2019-1-26 16:54:33 @ V0.0.1
// Copyright (C)2019 SENCOM <sencom1997@outlook.com>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifndef LCD1602_H
#define LCD1602_H

#include "wiringPi.h"
#include "time.h"

#define SETMODE 0x38	 //16*2显示,5*8点阵,8位数据接口
#define DISOPEN 0x0C	 //显示开,显示光标,光标不闪烁
#define DISMODE 0x06	 //读写字符后地址加1,屏显不移动
#define SETADDR 0x80	 //设置数据地址指针初始值
#define CLEAR	0x01	 //清屏,数据指针清零
#define RET	0x02	 //回车,数据指针清零
#define TEST	0xFF	 //回车,数据指针清零

#define SLEEP_L 100000	 //长延时
#define SLEEP_S  50000	 //短延时

typedef signed char        int8_t;
typedef short              int16_t;
typedef int                int32_t;
typedef long long          int64_t;
typedef unsigned char      uint8_t;
typedef unsigned short     uint16_t;
typedef unsigned int       uint32_t;
typedef unsigned long long uint64_t;

class LCD1602
{
	private:
		int _rs_pin; 	// LOW: 命令.  HIGH: 数据.
		int _rw_pin; 	// LOW: 写.  HIGH: 读.
		int _en_pin; 	// 高电平有效.
		int _data_pins[8];
		
		void Delay(uint32_t s);	//延时
		void Check_Busy(void);	//检查忙
		void Write_Com(uint8_t com);//写命令
		void Write_Data(uint8_t data);	//写数据
	public:
		void Init(int rs, int rw, int en,int d0, int d1, int d2, int d3,int d4, int d5, int d6, int d7);
		void Write_String(uint8_t x,uint8_t y,const char *s);
		void Clear(void);//清屏
}; 

#endif

 

lcd1602.cpp

/*
// This file is part of LCD1602 Driver project
// Copyright (C)2019 SENCOM <sencom1997@outlook.com>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "lcd1602.h"

void LCD1602::Init( int rs, int rw, int en, int d0, int d1, int d2, int d3, int d4, int d5, int d6, int d7)
{
	wiringPiSetup();
	_rs_pin = rs;
	_rw_pin = rw;
	_en_pin = en;
  
	_data_pins[0] = d0;
	_data_pins[1] = d1;
	_data_pins[2] = d2;
	_data_pins[3] = d3; 
	_data_pins[4] = d4;
	_data_pins[5] = d5;
	_data_pins[6] = d6;
	_data_pins[7] = d7; 
	for(int i=0; i<8; i++)
		pinMode(_data_pins[i],OUTPUT);
	
	pinMode(_en_pin,OUTPUT);
	pinMode(_rw_pin,OUTPUT);
	pinMode(_rs_pin,OUTPUT);
	
	Write_Com(SETMODE);//模式设置
        Delay(SLEEP_L);
        Write_Com(DISOPEN);//显示设置
        Delay(SLEEP_L);
        Write_Com(DISMODE);//显示模式
        Delay(SLEEP_L);
        Write_Com(CLEAR);//清屏
        Delay(SLEEP_L);
	
}

void LCD1602::Write_Com(uint8_t com)
{
	digitalWrite(_en_pin,LOW);
	digitalWrite(_rs_pin,LOW);
	digitalWrite(_rw_pin,LOW);
//	printf("%2X  ",com);
	for(uint8_t i=0; i<8; i++)
		digitalWrite(_data_pins[i],(com >> i) & 0x01);
//	for(uint8_t i=0; i<8; i++)			  刚开始时候老是初始化失败(没反应)
//	{						  以为是这里宏定义 HIGH LOW 的问题
//		if ( (com >> i) & 0x01 == 1)		  后来去看wiringPi.h 发现
//		{					  HIGH 定义为 1
//			digitalWrite(_data_pins[i],HIGH); LOW  定义为 0  完全没毛病
//			printf("1 ");			  过了好几天后重新拾起,检查发现是线接错了... ...
//		}					  所以就留下了这一段Debug代码
//		else
//		{
//			digitalWrite(_data_pins[i],LOW);
//			printf("0 ");
//		}
//	}
//	printf("\n");
	Delay(SLEEP_S);
	digitalWrite(_en_pin,HIGH);
	Delay(SLEEP_S);
	digitalWrite(_en_pin,LOW);
}

void LCD1602::Write_Data(uint8_t data)
{
	digitalWrite(_en_pin,LOW);
	Delay(SLEEP_S);
	digitalWrite(_rs_pin,HIGH);
	digitalWrite(_rw_pin,LOW);
	for(uint8_t i=0; i<8; i++)
		digitalWrite(_data_pins[i],(data >> i) & 0x01);
	Delay(SLEEP_S);
	digitalWrite(_en_pin,HIGH);
	Delay(SLEEP_S);
	digitalWrite(_en_pin,LOW);
}
void LCD1602::Write_String(uint8_t x,uint8_t y,const char *s)
{
	if(y == 1)
		Write_Com(0x80 + x - 1);//在第一行 x 列写
	else
		Write_Com(0xC0 + x - 1);//在第二行 x 列写
	while(*s)
	{
		Write_Data(*s);
		s++;
	}
}

void LCD1602::Clear()
{
	Write_Com(CLEAR);
	Delay(SLEEP_L);
}

void LCD1602::Delay(uint32_t s)
{
//	此方法延时只能按秒计算 过长
//	clock_t start = clock();
//	clock_t lay = (clock_t)s * CLOCKS_PER_SEC;
//	while ((clock()-start) < lay);
	while(s)s --;	//现在换成 while 循环
}

 

example_tiny_clock.cpp

/*
// This file is part of LCD1602 Driver project
// Copyright (C)2019 SENCOM <sencom1997@outlook.com>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.	
*/

#include "lcd1602.h"	//引用 lcd1602

int main()
{
	time_t t;
        char buf[128];
        char stime[64];	//存放时间截取
	char sdate[64];	//存放日期截取
	int i,j;
	
	LCD1602 lcd;
	lcd.Init(1,4,5,0,2,3,21,22,23,24,25);
	while(1)
	{
		time(&t);
		ctime_r(&t,buf); 
		for(i=0; i<11;i++)
			sdate[i] = buf[i];
		sdate[i] = '\0';
		for(i=11,j=0; i<20;i++,j++)
			stime[j] = buf[i];
		stime[j] = '\0';
		lcd.Write_String(1,1,sdate);
		lcd.Write_String(1,2,stime);
	}
	return 0;
}

上图为wiringPi源码

 

上图为效果图

 

运行例子方法:

1.确保你的树莓派已经安装 wiringPi
2.连线
VSS  -> GND
VDD -> VCC
VO -> 偏压信号
RS-> GPIO1
RW -> GPIO4
E     -> GPIO5
D -> GPIO0
D -> GPIO2
D -> GPIO3
D -> GPIO21
D -> GPIO22
D -> GPIO23
D -> GPIO24
D -> GPIO25
A -> VCC
K -> GND
3.切换到项目目录然后g++ lcd1602.cpp example_tiny_clock.cpp -lwiringPi && ./a.out
4.完成

 

如果后续有更新,我会在我的GitHub上更新!!!

传送->GitHub