☛ 功能说明
使用 Arduino 开发板控制 LCD 1602 显示字元 ” Hello “,并且左右来回移动。
☛ 使用材料
Arduino UNO R3 开发板 × 1、LCD 1602A 显示器模块 × 1、可变电阻 20KΩ × 1、电阻 220Ω × 1 。
☛ 电路图及面包板接线图
☛ 程式码
#include<LiquidCrystal.h> // 使用 LiquidCrystal.h 函式库 LiquidCrystal lcd(12,11,5,4,3,2); // 宣告 lcd 变数并定义使用接脚 void setup() { lcd.begin(16,2); // 使用 16 行 × 2 列 LCD } void loop() { lcd.setCursor(10,0); // 设定游标位置在第 0 列第 10 行 lcd.print("Hello!"); // 显示字元 Hello! for(int i =0;i<10;i++) // 左移 10 次 { lcd.scrollDisplayLeft(); // LCD 内容向左卷动一行 delay(200); // 延迟 0.2 秒 } for(int i=0;i<10;i++) // 右移 10 次 { lcd.scrollDisplayRight(); // LCD 内容向左卷动一行 delay(200); // 延迟 0.2 秒 } }
☛ 练习
⑴ 设计 Arduino 程式,控制 LCD 显示 ” Hello World! “,并且由左向右移动。
#include<LiquidCrystal.h> // 使用 LiquidCrystal.h 函式库 LiquidCrystal lcd(12,11,5,4,3,2); // 宣告 lcd 变数并定义使用接脚 void setup() { lcd.begin(16,2); // 使用 16 行 × 2 列 LCD lcd.setCursor(16,0); // 设定游标位置在第 0 列第 16 行 lcd.print("Hello World!"); // 显示字元 Hello World! } void loop() { for(int i =0;i<16;i++) // 左移 16 次 { lcd.scrollDisplayLeft(); // LCD 内容向左卷动一行 delay(200); // 延迟 0.2 秒 } lcd.clear(); // 清除 LCD 内容 lcd.setCursor(16,0); // 设定游标位置在第 0 列第 16 行 lcd.print("Hello World!"); // 显示字元 Hello World! }
⑵ 设计 Arduino 程式,控制 LCD 显示 ” Hello World! “,并且由右向左移动。
#include<LiquidCrystal.h> // 使用 LiquidCrystal.h 函式库 LiquidCrystal lcd(12,11,5,4,3,2); // 宣告 lcd 变数并定义使用接脚 void setup() { lcd.begin(16,2); // 使用 16 行 × 2 列 LCD lcd.setCursor(0,0); // 设定游标位置在第 0 列第 0 行 lcd.print("Hello World!"); // 显示字元 Hello World! } void loop() { for(int i =0;i<16;i++) // 右移 16 次 { lcd.scrollDisplayRight(); // LCD 内容向右卷动一行 delay(200); // 延迟 0.2 秒 } lcd.clear(); // 清除 LCD 内容 lcd.setCursor(0,0); // 设定游标位置在第 0 列第 0 行 lcd.print("Hello World!"); // 显示字元 Hello World! }