Ardino基础教程 21_LCD1602液晶屏
實驗二十一:LCD1602液晶屏
本次試驗使用arduino 直接驅動1602 液晶顯示文字
1602 液晶在應用中非常廣泛,最初的1602 液晶使用的HD44780 控制器,現在各個廠家的1602 模塊基本上都是采用了與之兼容的IC,所以特性上基本都是一致的。
1602LCD 主要技術參數
- 顯示容量為16×2 個字符;
- 芯片工作電壓為4.5~5.5V;
- 工作電流為2.0mA(5.0V);
- 模塊最佳工作電壓為5.0V;
- 字符尺寸為2.95×4.35(W×H)mm。
1602 液晶接口引腳定義
接口說明:
1、兩組電源 一組是模塊的電源 一組是背光板的電源 一般均使用5V 供電。本次試驗背光使用3.3V 供電也可以工作。
2、VL 是調節對比度的引腳,串聯不大于5KΩ 的電位器進行調節。本次實驗使用1KΩ 的電阻來設定對比度。其連接分高電位與低電位接法,本次使用低電位接法,串聯1KΩ 電阻后接GND。
3、RS 是很多液晶上都有的引腳 是命令/數據選擇引腳 該腳電平為高時表示將進行數據操作;為低時表示進行命令操作。
4、RW 也是很多液晶上都有的引腳 是讀寫選擇端 該腳電平為高是表示要對液晶進行讀操作;為低時表示要進行寫操作。
5、E 同樣很多液晶模塊有此引腳 通常在總線上信號穩定后給一正脈沖通知把數據讀走,在此腳為高電平的時候總線不允許變化。
6、D0—D7 8 位雙向并行總線,用來傳送命令和數據。
7、BLA 是背光源正極,BLK 是背光源負極。
1602 液晶的基本操作分以下四種:
下圖就是1602 液晶實物圖
實驗連接圖
1602 直接與arduino 通信,根據產品手冊描述,分8 位連接法與4 位連接法,咱們先使用8位連接法進行實驗。硬件連接方式如下圖
代碼如下:
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);
}
///
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);
}
4 位接法
在正常使用下,8 位接法基本把arduino 的數字端口占滿了,如果想要多接幾個傳感器就沒有端口了,這種情況下怎么處理呢,咱們可以使用4 位接法。
實驗接線圖
4 位接法的硬件連接方法如下圖
硬件接好后把下面的代碼上傳到控制板上,看看效果。
int LCD1602_RS=12;
int LCD1602_RW=11;
int LCD1602_EN=10;
int DB[] = { 6, 7, 8, 9};
char str1[]=“Welcome to”;
char str2[]=“geek-workshop”;
char str3[]=“this is the”;
char str4[]=“4-bit interface”;
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 <= 10; i++)
{
digitalWrite(i,temp & 0x80);
temp <<= 1;
}
digitalWrite( LCD1602_EN,HIGH);
delayMicroseconds(1);
digitalWrite( LCD1602_EN,LOW);
}
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 <= 10; 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)
{
LCD_SET_XY( X, Y ); //設置地址
while (*s) //寫字符串
{
LCD_Data_Write(*s);
s ++;
}
}
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)
{
LCD_Command_Write(0x01);
delay(50);
LCD_Write_String(3,0,str1);//第1 行,第4 個地址起
delay(50);
LCD_Write_String(1,1,str2);//第2 行,第2 個地址起
delay(5000);
LCD_Command_Write(0x01);
delay(50);
LCD_Write_String(0,0,str3);
delay(50);
LCD_Write_String(0,1,str4);
delay(5000);
}
普通瀏覽復制代碼保存代碼打印代碼
int LCD1602_RS=12;
int LCD1602_RW=11;
int LCD1602_EN=10;
int DB[] = { 6, 7, 8, 9};
char str1[]=“Welcome to”;
char str2[]=“geek-workshop”;
char str3[]=“this is the”;
char str4[]=“4-bit interface”;
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 <= 10; i++)
{
digitalWrite(i,temp & 0x80);
temp <<= 1;
}
digitalWrite( LCD1602_EN,HIGH);
delayMicroseconds(1);
digitalWrite( LCD1602_EN,LOW);
}
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 <= 10; 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)
{
LCD_SET_XY( X, Y ); //設置地址
while (*s) //寫字符串
{
LCD_Data_Write(*s);
s ++;
}
}
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)
{
LCD_Command_Write(0x01);
delay(50);
LCD_Write_String(3,0,str1);//第1 行,第4 個地址起
delay(50);
LCD_Write_String(1,1,str2);//第2 行,第2 個地址起
delay(5000);
LCD_Command_Write(0x01);
delay(50);
LCD_Write_String(0,0,str3);
delay(50);
LCD_Write_String(0,1,str4);
delay(5000);
}
總結
以上是生活随笔為你收集整理的Ardino基础教程 21_LCD1602液晶屏的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Ardino基础教程 20_红外遥控
- 下一篇: Ardino基础教程 21_最简单最快控