大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺
- 接线图
- 4位接线法
[codesyntax lang=”cpp”]
/**
* VSS GND
* VDD 5V
* V0 GND
* RS 12
* RW 11
* E 10
* D4 9
* D5 8
* D6 7
* D7 6
* A V5
* K GND
* from http://surenpi.com
* created 2015/3/23
*/
int LCD1602_RS=12;
int LCD1602_RW=11;
int LCD1602_EN=10;
int DB[] = { 6, 7, 8, 9};
char str1[]="Welcome to";
char str2[]="surenpi";
char str3[]="find me";
char str4[]="surenpi.com";
void LCD_Command_Write(int command)
{
int i,temp;
digitalWrite( LCD1602_RS,LOW);
digitalWrite( LCD1602_RW,LOW);
digitalWrite( LCD1602_EN,LOW);
temp=command & 0xf0;
for (i=DB[0]; i <= 9; i++)
{
digitalWrite(i,temp & 0x80);
temp <<= 1;
}
digitalWrite( LCD1602_EN,HIGH);
delayMicroseconds(1);
digitalWrite( LCD1602_EN,LOW);
temp=(command & 0x0f)<<4;
for (i=DB[0]; i <= 9; i++)
{
digitalWrite(i,temp & 0x80);
temp <<= 1;
}
digitalWrite( LCD1602_EN,HIGH);
delayMicroseconds(1);
digitalWrite( LCD1602_EN,LOW);
}
void cleanScreen(int delayTime = 50)
{
LCD_Command_Write(0x01);
delay(delayTime);
}
void LCD_Data_Write(int dat)
{
int i=0,temp;
digitalWrite( LCD1602_RS,HIGH);
digitalWrite( LCD1602_RW,LOW);
digitalWrite( LCD1602_EN,LOW);
temp=dat & 0xf0;
for (i=DB[0]; i <= 9; i++)
{
digitalWrite(i,temp & 0x80);
temp <<= 1;
}
digitalWrite( LCD1602_EN,HIGH);
delayMicroseconds(1);
digitalWrite( LCD1602_EN,LOW);
temp=(dat & 0x0f)<<4;
for (i=DB[0]; i <= 9; i++)
{
digitalWrite(i,temp & 0x80);
temp <<= 1;
}
digitalWrite( LCD1602_EN,HIGH);
delayMicroseconds(1);
digitalWrite( LCD1602_EN,LOW);
}
void LCD_SET_XY( int x, int y )
{
int address;
if (y ==0) address = 0x80 + x;
else address = 0xC0 + x;
LCD_Command_Write(address);
}
void LCD_Write_Char( int x,int y,int dat)
{
LCD_SET_XY( x, y );
LCD_Data_Write(dat);
}
void LCD_Write_String(int x,int y,char *s, int delayTime = 50)
{
LCD_SET_XY(x, y); //设置地址
int i = 0;
while(*s && ++i <= 16) //写字符串
{
LCD_Data_Write(*s);
s++;
}
delay(delayTime);
if(*s)
{
cleanScreen();
LCD_Write_String(x, y, s, delayTime);
}
}
void setup (void)
{
int i = 0;
for(i = 6; i <= 12; i++)
{
pinMode(i,OUTPUT);
}
delay(100);
LCD_Command_Write(0x28);//4线 2行 5x7
delay(50);
LCD_Command_Write(0x06);
delay(50);
LCD_Command_Write(0x0c);
delay(50);
LCD_Command_Write(0x80);
delay(50);
LCD_Command_Write(0x01);
delay(50);
}
void loop (void)
{
cleanScreen();
LCD_Write_String(3,0,str1);//第1行,第4个地址起
LCD_Write_String(0,1,str2, 3000);//第2行,第2个地址起
cleanScreen();
LCD_Write_String(0,0,str3);
LCD_Write_String(0,1,str4, 3000);
}
[/codesyntax]
- 8位接线法
[codesyntax lang=”cpp”]
/**
* 8 bit
* from http://surenpi.com
*/
int DI = 12;
int RW = 11;
int DB[] = {3, 4, 5, 6, 7, 8, 9, 10};//使用数组来定义总线需要的管脚
int Enable = 2;
void LcdCommandWrite(int value)
{
// 定义所有引脚
int i = 0;
for (i=DB[0]; i <= DI; i++) //总线赋值
{
digitalWrite(i,value & 01);//因为1602液晶信号识别是D7-D0(不是D0-D7),这里是用来反转信号。
value >>= 1;
}
digitalWrite(Enable,LOW);
delayMicroseconds(1);
digitalWrite(Enable,HIGH);
delayMicroseconds(1); // 延时1ms
digitalWrite(Enable,LOW);
delayMicroseconds(1); // 延时1ms
}
void LcdDataWrite(int value)
{
// 定义所有引脚
int i = 0;
digitalWrite(DI, HIGH);
digitalWrite(RW, LOW);
for (i=DB[0]; i <= DB[7]; i++)
{
digitalWrite(i,value & 01);
value >>= 1;
}
digitalWrite(Enable,LOW);
delayMicroseconds(1);
digitalWrite(Enable,HIGH);
delayMicroseconds(1);
digitalWrite(Enable,LOW);
delayMicroseconds(1); // 延时1ms
}
void setup (void)
{
int i = 0;
for(i=Enable; i <= DI; i++)
{
pinMode(i,OUTPUT);
}
delay(100);
// 短暂的停顿后初始化LCD
// 用于LCD控制需要
LcdCommandWrite(0x38); // 设置为8-bit接口,2行显示,5x7文字大小
delay(64);
LcdCommandWrite(0x38); // 设置为8-bit接口,2行显示,5x7文字大小
delay(50);
LcdCommandWrite(0x38); // 设置为8-bit接口,2行显示,5x7文字大小
delay(20);
LcdCommandWrite(0x06); // 输入方式设定
// 自动增量,没有显示移位
delay(20);
LcdCommandWrite(0x0E); // 显示设置
// 开启显示屏,光标显示,无闪烁
delay(20);
LcdCommandWrite(0x01); // 屏幕清空,光标位置归零
delay(100);
LcdCommandWrite(0x80); // 显示设置
// 开启显示屏,光标显示,无闪烁
delay(20);
}
void loop (void)
{
LcdCommandWrite(0x01); // 屏幕清空,光标位置归零
delay(10);
LcdCommandWrite(0x80+3);
delay(10);
// 写入欢迎信息
LcdDataWrite('W');
LcdDataWrite('e');
LcdDataWrite('l');
LcdDataWrite('c');
LcdDataWrite('o');
LcdDataWrite('m');
LcdDataWrite('e');
LcdDataWrite(' ');
LcdDataWrite('t');
LcdDataWrite('o');
delay(10);
LcdCommandWrite(0xc0+1); // 定义光标位置为第二行第二个位置
delay(10);
LcdDataWrite('g');
LcdDataWrite('e');
LcdDataWrite('e');
LcdDataWrite('k');
LcdDataWrite('-');
LcdDataWrite('w');
LcdDataWrite('o');
LcdDataWrite('r');
LcdDataWrite('k');
LcdDataWrite('s');
LcdDataWrite('h');
LcdDataWrite('o');
LcdDataWrite('p');
delay(5000);
LcdCommandWrite(0x01); // 屏幕清空,光标位置归零
delay(10);
LcdDataWrite('I');
LcdDataWrite(' ');
LcdDataWrite('a');
LcdDataWrite('m');
LcdDataWrite(' ');
LcdDataWrite('h');
LcdDataWrite('o');
LcdDataWrite('n');
LcdDataWrite('g');
LcdDataWrite('y');
LcdDataWrite('i');
delay(3000);
LcdCommandWrite(0x02); //设置模式为新文字替换老文字,无新文字的地方显示不变。
delay(10);
LcdCommandWrite(0x80+5); //定义光标位置为第一行第六个位置
delay(10);
LcdDataWrite('t');
LcdDataWrite('h');
LcdDataWrite('e');
LcdDataWrite(' ');
LcdDataWrite('a');
LcdDataWrite('d');
LcdDataWrite('m');
LcdDataWrite('i');
LcdDataWrite('n');
delay(5000);
}
[/codesyntax]
- 使用LiquidCrystal库来连接
使用LiquidCrystal这个库的话,代码会简洁很多,而且还封装还一些常用函数。 [codesyntax lang=”cpp”]
/**
7 gpios demo
The circuit:
VSS GND
VDD 5V
V0 10K resistor or GND
RS 12
RW 11
EN 10
D4 9
D3 8
D2 7
D1 6
from www.surenpi.com
created 2015/3/25
*/
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 10, 9, 8, 7, 6);
void setup() {
lcd.begin(16, 2);
lcd.print("www.surenpi.com");
}
void loop() {
lcd.setCursor(0, 1);
lcd.print(millis()/1000);
}
[/codesyntax] 注意:上面的代码中使用了7个GPIO接口,其实把RW接到GND上也行,这样的就可以节省一个GPIO。如果要把RW接GND话,就要使用另外一个构造函数:LiquidCrystal lcd(12, 10, 9, 8, 7, 6)
- 参考
转载于:https://my.oschina.net/surenpi/blog/604780
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/190509.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...