除了LED之外,液晶也是ARDUIN经常使用的显示器材。本节我们介绍常用的1602液晶显示的使用:
![](https://www.arduino.cc/en/uploads/Tutorial/lcd_photo.png)
对应的硬件原理图
![](https://www.arduino.cc/en/uploads/Tutorial/LCD_Base_bb_Schem.png)
虽然液晶显示比LED更复杂,但是由于内部都有显示控制芯片所以反而使用起来比LED更简单(当然,也有LED控制芯片,如果不怕增加成本使用这样的芯片,这样arduino和LED的编程也可以简单了)。由于使用了arduino液晶显示库,所以液晶控制部分比较简单。我们平时也可以参考硬件设计,就是将液晶的数字和控制端依次由arduino数字管脚连接,然后将这些管脚的数字编号在初始化部分赋值给控制库程序。就这样,我们就可以在主程序中调用各种显示指令了。不很难的,试一下就知道!
```
/*
LiquidCrystal Library - Hello World
Demonstrates the use a 16x2 LCD display. The LiquidCrystal
library works with all LCD displays that are compatible with the
Hitachi HD44780 driver. There are many of them out there, and you
can usually tell them by the 16-pin interface.
This sketch prints "Hello World!" to the LCD
and shows the time.
The circuit:
* LCD RS pin to digital pin 12
* LCD Enable pin to digital pin 11
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* LCD R/W pin to ground
* LCD VSS pin to ground
* LCD VCC pin to 5V
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
Library originally added 18 Apr 2008
by David A. Mellis
library modified 5 Jul 2009
by Limor Fried (http://www.ladyada.net)
example added 9 Jul 2009
by Tom Igoe
modified 22 Nov 2010
by Tom Igoe
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/LiquidCrystal
*/
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("hello, world!");
}
void loop() {
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
// print the number of seconds since reset:
lcd.print(millis() / 1000);
}
```
综合范例小游戏,用户通过拨码开关给出对应的数字的二进制换算结果,每次由程序给出随机的数字让用户换算。学习ARDUINO外部中断的使用,巩固以前的硬件知识。
![](https://img.kancloud.cn/19/88/1988249c33348c5075ac2627a9c4b7c8_1005x744.png)
```
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
const int Pin0 = 6; // Binary number 2^1 or 1
const int Pin1 = 5; // Binary number 2^2 or 2
const int Pin2 = 4; // Binary number 2^3 or 4
const int Pin3 = 3; // Binary number 2^4 or 8
const int Pin4 = A4; // Binary number 2^5 or 16
const int Pin5 = A3; // Binary number 2^6 or 32
const int Pin6 = A2; // Binary number 2^7 or 64
const int Pin7 = A1; // Binary number 2^8 or 128
const int easyMode = A0; // Easy mode is 0-15, Hard is 0-255
int BinaryValue; // Value for adding up numbers to compare to random number
int correctNumber = 0; // Flag to see if the number was correct
int wrongNumber = 0; // Flag to see if the number was wrong
const int buzzer = 13; //Buzzer pin
int freq; //frequency out
const int buttonPin = 2; // the number of the pushbutton pin
int buttonState; // variable for reading the pushbutton status
long randNumber; // variable for the random number
void setup() {
lcd.begin(16, 2); // set up the LCD's number of columns and rows
pinMode(Pin0, INPUT_PULLUP);
pinMode(Pin1, INPUT_PULLUP);
pinMode(Pin2, INPUT_PULLUP);
pinMode(Pin3, INPUT_PULLUP);
pinMode(Pin4, INPUT_PULLUP);
pinMode(Pin5, INPUT_PULLUP);
pinMode(Pin6, INPUT_PULLUP);
pinMode(Pin7, INPUT_PULLUP);
pinMode(easyMode, INPUT_PULLUP);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(buzzer, OUTPUT); // Set buzzer pin as OUTPUT
// if analog input pin 5 is unconnected, random analog
// noise will cause the call to randomSeed() to generate
// different seed numbers each time the sketch runs.
// randomSeed() will then shuffle the random function.
randomSeed(analogRead(5));
if (digitalRead(easyMode) == HIGH)
{
randNumber = random(0, 15);
}
else
{
randNumber = random(0, 255);
}
// Print a message to the LCD.
lcd.print("Your number is");
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
lcd.print(randNumber); // Print the random number
}
void checkNumber() // Check switches for correct number
{
if (digitalRead(Pin0) == HIGH)
{
BinaryValue = 1;
}
else
{
BinaryValue = 0;
}
if (digitalRead(Pin1) == HIGH)
{
BinaryValue = BinaryValue + 2;
}
if (digitalRead(Pin2) == HIGH)
{
BinaryValue = BinaryValue + 4;
}
if (digitalRead(Pin3) == HIGH)
{
BinaryValue = BinaryValue + 8;
}
if (digitalRead(Pin4) == HIGH)
{
BinaryValue = BinaryValue + 16;
}
if (digitalRead(Pin5) == HIGH)
{
BinaryValue = BinaryValue + 32;
}
if (digitalRead(Pin6) == HIGH)
{
BinaryValue = BinaryValue + 64;
}
if (digitalRead(Pin7) == HIGH)
{
BinaryValue = BinaryValue + 128;
}
if (BinaryValue == randNumber) // Check if switches match random number
{
correctNumber = 1;
}
else
{
wrongNumber = 1;
}
}
void printBinary() // Displays status of switches
{
if (digitalRead(Pin7) == LOW)
{
lcd.print("0");
}
else
{
lcd.print("1");
}
if (digitalRead(Pin6) == LOW)
{
lcd.print("0");
}
else
{
lcd.print("1");
}
if (digitalRead(Pin5) == LOW)
{
lcd.print("0");
}
else
{
lcd.print("1");
}
if (digitalRead(Pin4) == LOW)
{
lcd.print("0");
}
else
{
lcd.print("1");
}
lcd.print(" ");
if (digitalRead(Pin3) == LOW)
{
lcd.print("0");
}
else
{
lcd.print("1");
}
if (digitalRead(Pin2) == LOW)
{
lcd.print("0");
}
else
{
lcd.print("1");
}
if (digitalRead(Pin1) == LOW)
{
lcd.print("0");
}
else
{
lcd.print("1");
}
if (digitalRead(Pin0) == LOW)
{
lcd.print("0");
}
else
{
lcd.print("1");
}
}
void loop() {
attachInterrupt(digitalPinToInterrupt(buttonPin), checkNumber, FALLING); // Wait for pushbutton to be pressed, when pressed check to see if correct number is inputted
lcd.setCursor(7, 1);
printBinary(); // Display status of switches
if (wrongNumber == 1)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Try again");
lcd.setCursor(0, 1);
lcd.print(randNumber);
tone(buzzer, 200); // Play wrong answer tone
delay(400);
noTone(buzzer); // Stop sound...
wrongNumber = 0;
correctNumber = 0;
}
if (correctNumber == 1)
{
tone(buzzer, 600); // Play correct answer tone
delay(100);
tone(buzzer, 1000);
delay(100);
tone(buzzer, 800);
delay(100);
noTone(buzzer); // Stop sound...
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Correct!");
lcd.setCursor(0, 1);
lcd.print(randNumber);
lcd.print(" is ");
printBinary(); // Display status of switches
delay(3000);
lcd.clear();
lcd.setCursor(0, 0);
// Print a message to the LCD.
lcd.print("Your number is");
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
if (digitalRead(easyMode) == HIGH)
{
randNumber = random(0, 15);
}
else
{
randNumber = random(0, 255);
}
lcd.print(randNumber);
correctNumber = 0;
wrongNumber = 0;
}
}
```
- 序言
- 第一章 arduino简介和学习
- 第一节 arduino介绍
- 第二节 学习方法概述
- 第三节 计算机的智能
- 第二章 开始学习之旅
- 第一节 安装环境和购买硬件的说明
- 第二节 介绍一个具体的范例
- 第三章 分类官网教程讲解
- 第一节 Basics类的示例项目
- 第二节 数字端口的示例
- 第三节 模拟端口的示例
- 第四节 串口通信的示例
- 第五节 LED显示的示例
- 第六节 arduino处理矩阵键盘
- 第七节 LCD显示范例
- 第四章 综合实践项目制作教程
- 第一节 简单的计算器
- 第五章 复习题
- 单选题
- 编程题目
- 单选题答案
- 编程题答案
- 第1题
- 第2题
- 第3题
- 第4题
- 第5题
- 第6题
- 第7题
- 第8题
- 第9题
- 第10题
- 第11题
- 第12题
- 第13题
- 第14题
- 第15题
- 第16题
- 第17题
- 复习题包含翻译
- 第六章 快速复习
- 第一节 英文解说