查看: 3040|回复: 11
|
Arduino控制LCD
[复制链接]
|
|
此project使用了一个Arduino duemilanove或者Arduino Mega和一个DFRobot生t产的i2C/TWI LCD1602(如图)
LCD1602规则说明
接口:I2C
I2C地址:0X27
电源电压:5V
LCD1602(16×2字符LCD)使用i2c接口,价钱大约RM50左右,是HD44780 LCD的二至三倍。如果控制器拥有足够的引脚,且预算有限,建议使用HD44780相容的LCD。
GueyWee 大大也有出售Arduino相关产品,可以比较一下价钱。
Arduino与LCD1602连接图
连接非常简单,代码编写也容易,用法与serial.print()指令相似。
注意:
不同型号的Arduino必需连接不同的引脚,比如使用Arduino Mega,SDA及SCL则分别连接在Digital 20和Digital 21
下载与安装程序库
- 下载LCD1602程序库
- 把下载了的文件解压至Arduino的libraries文件夹
代码示范(一)
Hello, world示范- //Sample code for i2c/TWI LCD1602 module
- //Compatible with Arduino IDE 1.0
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h>
-
- LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
-
- void setup()
- {
- lcd.init(); // initialize the lcd
- lcd.backlight();
- lcd.print("Hello, world!"); //print a message to the LCD
- }
-
- void loop()
- {
- }
复制代码 代码示范(二)
打印不同的数据类型在不同的位置- //Sample code for I2C/TWI LCD1602 Module
- //Compatible with the Arduino IDE 1.0
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h>
-
- LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
-
- String myString = "ediy.com.my";
- int myInteger = 123;
- float myFloat = 123.45;
- void setup()
- {
- lcd.init(); // initialize the lcd
- lcd.backlight();
- lcd.print("Hello, world!"); //Print a message to the LCD.
- delay(1000); // delay 1 second
- lcd.clear();
- lcd.setCursor(2, 0); //first line (x=2, y=0)
- lcd.print(myString);
- lcd.setCursor(0, 1); //second line (x=0, y=1)
- lcd.print(myInteger);
- lcd.setCursor(6, 1); //second line (x=6, y=1)
- lcd.print(myFloat);
- }
-
- void loop()
- {
- }
复制代码 本帖最后由 西门庆33 于 27-4-2013 09:30 PM 编辑
|
|
|
|
|
|
|
|
楼主 |
发表于 28-4-2013 10:55 AM
|
显示全部楼层
这不就是传说中的收银机(Cash drawer)LCD吗?
从串口取得的字串(比如12.30),将会显示在LCD- // Sample code for I2C/TWI LCD1602 Module
- // read data from serial port & print to LCD
- // Compatible with the Arduino IDE 1.0
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h>
-
- LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
- String inputString = ""; // a string to hold incoming data
- boolean stringComplete = false; // whether the string is complete
- void setup()
- {
- lcd.init(); // initialize the lcd
- lcd.backlight(); // turn on LCD back light
- Serial.begin(9600);
- Serial.println("Welcome");
- lcd.clear();
- lcd.setCursor(0, 0); //first line (x=0, y=0)
- lcd.print("Welcome");
- }
-
- void loop()
- {
- // process data when a carriage return arrives:
- if (stringComplete) {
- processData();
- // clear the string:
- inputString = "";
- stringComplete = false;
- }
- }
-
- void processData () {
- float amount = StrToFloat(inputString); //convert inputString to float
- lcd.clear();
- lcd.setCursor(0, 0); //first line (x=0, y=0)
- lcd.print("Amount: ");
- lcd.setCursor(8, 0); //first line (x=8, y=0)
- lcd.print(amount);
- //lcd.setCursor(0, 1); //second line (x=0, y=1)
- //lcd.print("Change: ");
- //lcd.setCursor(8, 1); //second line (x=9, y=1)
- //lcd.print(change);
- }
-
- /*
- SerialEvent occurs whenever a new data comes in the
- hardware serial RX. This routine is run between each
- time loop() runs, so using delay inside loop can delay
- response. Multiple bytes of data may be available.
- */
- void serialEvent() {
- while (Serial.available()) {
- // get the new byte:
- char inChar = (char)Serial.read();
- // add it to the inputString:
- inputString += inChar;
- // if the incoming character is a newline, set a flag
- // so the main loop can do something about it:
- if (inChar == '\r') {
- stringComplete = true;
- }
- }
- }
-
- // convert string to float
- float StrToFloat(String str){
- char carray[str.length() + 1]; //determine size of the array
- str.toCharArray(carray, sizeof(carray)); //put str into an array
- return atof(carray);
- }
复制代码 本帖最后由 西门庆33 于 28-4-2013 09:28 PM 编辑
|
|
|
|
|
|
|
|
发表于 20-7-2013 03:08 PM
|
显示全部楼层
大哥我找到了
1602A
是不是那种比较难用的啊 ?
为什么 1602 不是 serial 的.. 看来我这次惨了
|
|
|
|
|
|
|
|
发表于 20-7-2013 06:45 PM
|
显示全部楼层
angels1026 发表于 20-7-2013 03:08 PM
大哥我找到了
1602A
serial的不是更难用吗!!!(serial 是serial communication?)
|
|
|
|
|
|
|
|
发表于 20-7-2013 07:13 PM
来自手机
|
显示全部楼层
weitao 发表于 20-7-2013 06:45 PM
serial的不是更难用吗!!!(serial 是serial communication?)
serial 的只是 写
serial print()
完成 ~在arduino 很好用 |
|
|
|
|
|
|
|
发表于 20-7-2013 07:19 PM
|
显示全部楼层
angels1026 发表于 20-7-2013 07:13 PM
serial 的只是 写
serial print()
完成 ~在arduino 很好用
不错!!!只可惜我下次才能学arduino!!!之前用pic来control LCD
|
|
|
|
|
|
|
|
楼主 |
发表于 20-7-2013 07:44 PM
|
显示全部楼层
angels1026 发表于 20-7-2013 03:08 PM
大哥我找到了
1602A
我使用的是TWI LCD1602,和你的1602A一样,只是在后面连接了一IC2模块,Arduino与TWI LCD1602之间必须使用I2C协议沟通,好处是只用二条通信线,坏外是价钱比较贵。
你的LCD可以参考这网页
http://assiss.github.io/arduino- ... stalAutoscroll.html
- /*
- LiquidCrystal Library - Autoscroll
-
- 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 demonstrates the use of the autoscroll()
- and noAutoscroll() functions to make new text scroll or not.
-
- 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
- * 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://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);
- }
- void loop() {
- // set the cursor to (0,0):
- lcd.setCursor(0, 0);
- // print from 0 to 9:
- for (int thisChar = 0; thisChar < 10; thisChar++) {
- lcd.print(thisChar);
- delay(500);
- }
- // set the cursor to (16,1):
- lcd.setCursor(16,1);
- // set the display to automatically scroll:
- lcd.autoscroll();
- // print from 0 to 9:
- for (int thisChar = 0; thisChar < 10; thisChar++) {
- lcd.print(thisChar);
- delay(500);
- }
- // turn off automatic scrolling
- lcd.noAutoscroll();
-
- // clear screen for the next loop:
- lcd.clear();
- }
复制代码 至于Serial指令不用我讲你也懂了
本帖最后由 西门庆33 于 20-7-2013 07:56 PM 编辑
|
|
|
|
|
|
|
|
发表于 20-7-2013 07:47 PM
|
显示全部楼层
西门庆33 发表于 20-7-2013 07:44 PM
我使用的是TWI LCD1602,和你的1602A一样,只是在后面连接了一IC2模块,Arduino ...
感谢大哥.. 等货到了再来研究 |
|
|
|
|
|
|
|
发表于 3-11-2013 02:42 AM
|
显示全部楼层
請問要怎樣讓字左右左右走~~digitalWrite可以嗎?我不會寫~~可以指教一下嗎?
|
|
|
|
|
|
|
|
楼主 |
发表于 3-11-2013 11:42 AM
|
显示全部楼层
李云 发表于 3-11-2013 02:42 AM
請問要怎樣讓字左右左右走~~digitalWrite可以嗎?我不會寫~~可以指教一下嗎?
不能用digitalWrite
你必须使用
lcd.setCursor(x, y);
lcd.print(chartoDisplay);
delay(yourDesiredSpeed);
只要变动x值就行了
|
|
|
|
|
|
|
|
发表于 3-11-2013 03:13 PM
|
显示全部楼层
西门庆33 发表于 3-11-2013 11:42 AM
不能用digitalWrite
你必须使用
這是老師教的~~但是老師給我們的題目是~上面和下面要左右左右走~~
我用lcd.scrollDisplayLeft() 和 lcd.scrollDisplayRight() 寫~老師說做不到上面下面不同速度左右走。
類似youtube裡面的Demo hello~~
之前寫的沒有左右移動的:
本帖最后由 李云 于 3-11-2013 03:45 PM 编辑
|
|
|
|
|
|
|
|
楼主 |
发表于 4-11-2013 09:54 AM
|
显示全部楼层
李云 发表于 3-11-2013 03:13 PM
這是老師教的~~但是老師給我們的題目是~上面和下面要左右左右走~~
我用lcd.scrollDisplayLeft() 和 lcd. ...
lcd.setCursor(x+i,0);
lcd.print(mdu);
每次显示完mdu後,清洗lcd屏,x加一为向右移,x减一为向左移。
当然你必须自己计算x的位置,别超出lcd屏 |
|
|
|
|
|
|
| |
本周最热论坛帖子
|