RaspberryPi-wiringPi驱动lcd1602

概览:

wiringPi

wiringPi GPIO对应引脚
https://pinout.xyz/pinout/wiringpi

编译安装

1
2
3
git clone git://git.drogon.net/wiringPi
cd git wiringPi
./build

lcd 1602

LCD1602液晶引脚说明

pin 名称 注释
1 VSS 电源地
2 VDD 电源正极
3 VL 液晶显示偏压信号
4 RS 数据/命令选择端(H/L)
5 R/S 读/写选择端(H/L)
6 E 使能信号
7 D0 Data I/O
8 D1 Data I/O
9 D2 Data I/O
10 D3 Data I/O
11 D4 Data I/O
12 D5 Data I/O
13 D6 Data I/O
14 D7 Data I/O
15 BLA 背光源正极
16 BLK 背光源负极

RaspberryPi端代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
* Author: Mardan
* Date: 2019.6.10
* Project: GuzalAida
*/


#include
#include
#include
#include
#include
#include

#define uchar unsigned char

//Global variable
FILE *fp;
char nowtime[16];
char cpu_load[16];
int lcdprint_page=1;

void gpio_init(void);
void lcd1602_control(uchar cmd);
void lcd1602_command(uchar dat);
void lcd1602_setmode(void);
void lcdprint(uchar *p,int row_number);

void page1();


int main(int argc, char const *argv[]){
wiringPiSetup();
gpio_init();
lcd1602_setmode();
lcdprint(" Welcome to",1);
lcdprint(" GuzalAida ",2);
sleep(3);
while(1){
lcd1602_control(0x01);//清屏
page1();
lcdprint_page++;
sleep(1);
}
return 0;
}

void page1(){
struct tm *ptr;
time_t lt;
lt=time(NULL);
ptr=localtime(<);
strftime(nowtime,16,"%I:%M:%S - %b",ptr);
lcdprint(nowtime,1);
fp=popen("uptime | awk '{print $9,$10,$11,$12}'","r");
fgets(cpu_load,16,fp);
lcdprint(cpu_load,2);
printf("Time: %s --- Load: %s\n",nowtime,cpu_load);
if(lcdprint_page>100){syslog(LOG_INFO,"Time: %s --- Load: %s\n",nowtime,cpu_load);lcdprint_page=0;}
pclose(fp);

}

void gpio_init(){
int i;
for(i=0;i<8;i++){
pinMode(i,OUTPUT);
} //data pin(0 to 7) Setpin
pinMode(27,OUTPUT); //RS Setpin
pinMode(28,OUTPUT); //EN Setpin
}

void lcd1602_control(uchar cmd){
digitalWrite(27,LOW); //RS = 0
digitalWriteByte(cmd);//GPIO0=cmd
digitalWrite(28,HIGH);
delay(5);
digitalWrite(28,LOW);
}

void lcd1602_command(uchar dat){
digitalWrite(27,HIGH); //RS=1
digitalWriteByte(dat);//GPIO0=dat
digitalWrite(28,HIGH);// EN=1;
delay(5);
digitalWrite(28,LOW);// EN=0;
}

void lcd1602_setmode(void){
lcd1602_control(0x38);//八线模式下两行显示
lcd1602_control(0x0c);//开启显示
lcd1602_control(0x06);//地址+1
lcd1602_control(0x01);//清屏
}

void lcdprint(uchar *p,int row_number){
int i=0;
row_number==1?lcd1602_control(0x80):lcd1602_control(0xc0);
while(p[i]!='\0'){
lcd1602_command(p[i]);
i++;
}
}
------ 文章已经结束,谢谢你的阅读 ------
------ article has ended,thank you for reading ------
Thanks support Me

---------------- The End ----------------