|
arduino可以使用liquidcrystal庫再lcd1602上顯示,也可以自行編輯指令顯示,此處使用的是liquidcrystal庫,使用liquidcrystal庫更加簡(jiǎn)單明了,自行編輯命令更加復(fù)雜,但是自行編輯命令更加有助于對(duì)lcd1602的理解。
代碼如下:
#include <LiquidCrystal.h>
// 創(chuàng)建lcd控制對(duì)象,并指定其引腳與Arduino控制板對(duì)應(yīng)關(guān)系
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
// 定義一個(gè)'a'變量
int thisChar = 'a';
void setup()
{
// lcd初始化,同時(shí)設(shè)置lcd屏幕的列數(shù)和行數(shù)(寬和高)
lcd.begin(16, 2);
// 打開光標(biāo)
lcd.cursor();
}
void loop()
{
//在'm'處轉(zhuǎn)向
if (thisChar == 'm')
{
// go right for the next letter
lcd.rightToLeft();
}
// 在's'處再次反轉(zhuǎn)
if (thisChar == 's')
{
// 向左走到下一個(gè)字母
lcd.leftToRight();
}
// 大于'z'則重置
if (thisChar > 'z')
{
// 回到(0,0)位置:
lcd.home();
//再次從a開始
thisChar = 'a';
}
// 打印字符
lcd.write(thisChar);
// 延時(shí)等待一秒
delay(100);
// thisChar自增
thisChar++;
}
proteus接線圖如下:
1.png (69.48 KB, 下載次數(shù): 58)
下載附件
2023-7-28 02:29 上傳
|
|