佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 3038|回复: 11

Arduino控制LCD

[复制链接]
发表于 27-4-2013 09:26 PM | 显示全部楼层 |阅读模式
此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
HD44780.jpg
GueyWee 大大也有出售Arduino相关产品,可以比较一下价钱。


Arduino与LCD1602连接图
连接非常简单,代码编写也容易,用法与serial.print()指令相似。

注意:
不同型号的Arduino必需连接不同的引脚,比如使用Arduino Mega,SDA及SCL则分别连接在Digital 20和Digital 21

下载与安装程序库
  • 下载LCD1602程序库
  • 把下载了的文件解压至Arduino的libraries文件夹

代码示范(一)

Hello, world示范
  1. //Sample code for i2c/TWI LCD1602 module
  2. //Compatible with Arduino IDE 1.0
  3. #include <Wire.h>
  4. #include <LiquidCrystal_I2C.h>

  5. LiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address to  0x27 for a 16 chars and 2 line display

  6. void setup()
  7. {
  8.   lcd.init();                      // initialize the lcd
  9.   lcd.backlight();
  10.   lcd.print("Hello, world!");      //print a message to the LCD
  11. }

  12. void loop()
  13. {
  14. }
复制代码
代码示范(二)
打印不同的数据类型在不同的位置
  1. //Sample code for I2C/TWI LCD1602 Module
  2. //Compatible with the Arduino IDE 1.0
  3. #include <Wire.h>
  4. #include <LiquidCrystal_I2C.h>

  5. LiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display

  6. String myString = "ediy.com.my";
  7. int myInteger = 123;
  8. float myFloat = 123.45;
  9. void setup()
  10. {
  11.   lcd.init();                 // initialize the lcd
  12.   lcd.backlight();
  13.   lcd.print("Hello, world!"); //Print a message to the LCD.
  14.   delay(1000);                // delay 1 second
  15.   lcd.clear();
  16.   lcd.setCursor(2, 0);        //first line (x=2, y=0)
  17.   lcd.print(myString);
  18.   lcd.setCursor(0, 1);        //second line (x=0, y=1)
  19.   lcd.print(myInteger);
  20.   lcd.setCursor(6, 1);        //second line (x=6, y=1)
  21.   lcd.print(myFloat);
  22. }

  23. void loop()
  24. {
  25. }
复制代码
本帖最后由 西门庆33 于 27-4-2013 09:30 PM 编辑

回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 28-4-2013 10:55 AM | 显示全部楼层
这不就是传说中的收银机(Cash drawer)LCD吗?
Cash drawer LCD.jpg

从串口取得的字串(比如12.30),将会显示在LCD
  1. // Sample code for I2C/TWI LCD1602 Module
  2. // read data from serial port & print to LCD
  3. // Compatible with the Arduino IDE 1.0
  4. #include <Wire.h>
  5. #include <LiquidCrystal_I2C.h>

  6. LiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display

  7. String inputString = "";         // a string to hold incoming data
  8. boolean stringComplete = false;  // whether the string is complete

  9. void setup()
  10. {
  11.   lcd.init();                 // initialize the lcd
  12.   lcd.backlight();            // turn on LCD back light
  13.   Serial.begin(9600);
  14.   Serial.println("Welcome");
  15.   lcd.clear();
  16.   lcd.setCursor(0, 0);        //first line (x=0, y=0)
  17.   lcd.print("Welcome");  
  18. }

  19. void loop()
  20. {
  21.   // process data when a carriage return arrives:
  22.   if (stringComplete) {
  23.     processData();
  24.     // clear the string:
  25.     inputString = "";
  26.     stringComplete = false;
  27.   }  
  28. }

  29. void processData () {
  30.   float amount = StrToFloat(inputString);  //convert inputString to float
  31.   lcd.clear();
  32.   lcd.setCursor(0, 0);        //first line (x=0, y=0)
  33.   lcd.print("Amount: ");
  34.   lcd.setCursor(8, 0);        //first line (x=8, y=0)
  35.   lcd.print(amount);
  36.   //lcd.setCursor(0, 1);        //second line (x=0, y=1)
  37.   //lcd.print("Change: ");  
  38.   //lcd.setCursor(8, 1);        //second line (x=9, y=1)
  39.   //lcd.print(change);   
  40. }

  41. /*
  42. SerialEvent occurs whenever a new data comes in the
  43. hardware serial RX.  This routine is run between each
  44. time loop() runs, so using delay inside loop can delay
  45. response.  Multiple bytes of data may be available.
  46. */
  47. void serialEvent() {
  48.   while (Serial.available()) {
  49.     // get the new byte:
  50.     char inChar = (char)Serial.read();
  51.     // add it to the inputString:
  52.     inputString += inChar;
  53.     // if the incoming character is a newline, set a flag
  54.     // so the main loop can do something about it:
  55.     if (inChar == '\r') {
  56.       stringComplete = true;
  57.     }
  58.   }
  59. }

  60. // convert string to float
  61. float StrToFloat(String str){
  62.   char carray[str.length() + 1]; //determine size of the array
  63.   str.toCharArray(carray, sizeof(carray)); //put str into an array
  64.   return atof(carray);
  65. }
复制代码
本帖最后由 西门庆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
回复

使用道具 举报

Follow Us
 楼主| 发表于 20-7-2013 07:44 PM | 显示全部楼层
angels1026 发表于 20-7-2013 03:08 PM
大哥我找到了
1602A

我使用的是TWI LCD1602,和你的1602A一样,只是在后面连接了一IC2模块,Arduino与TWI LCD1602之间必须使用I2C协议沟通,好处是只用二条通信线,坏外是价钱比较贵。
c18989c3e0525dcce6313a01f83e.jpg


你的LCD可以参考这网页
http://assiss.github.io/arduino- ... stalAutoscroll.html
LCD_bb.png
LCD_schem.png
  1. /*
  2.   LiquidCrystal Library - Autoscroll

  3. Demonstrates the use a 16x2 LCD display.  The LiquidCrystal
  4. library works with all LCD displays that are compatible with the
  5. Hitachi HD44780 driver. There are many of them out there, and you
  6. can usually tell them by the 16-pin interface.

  7. This sketch demonstrates the use of the autoscroll()
  8. and noAutoscroll() functions to make new text scroll or not.

  9. The circuit:
  10. * LCD RS pin to digital pin 12
  11. * LCD Enable pin to digital pin 11
  12. * LCD D4 pin to digital pin 5
  13. * LCD D5 pin to digital pin 4
  14. * LCD D6 pin to digital pin 3
  15. * LCD D7 pin to digital pin 2
  16. * LCD R/W pin to ground
  17. * 10K resistor:
  18. * ends to +5V and ground
  19. * wiper to LCD VO pin (pin 3)

  20. Library originally added 18 Apr 2008
  21. by David A. Mellis
  22. library modified 5 Jul 2009
  23. by Limor Fried (http://www.ladyada.net)
  24. example added 9 Jul 2009
  25. by Tom Igoe
  26. modified 22 Nov 2010
  27. by Tom Igoe

  28. This example code is in the public domain.

  29. http://arduino.cc/en/Tutorial/LiquidCrystal
  30. */

  31. // include the library code:
  32. #include <LiquidCrystal.h>

  33. // initialize the library with the numbers of the interface pins
  34. LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

  35. void setup() {
  36.   // set up the LCD's number of columns and rows:
  37.   lcd.begin(16,2);
  38. }

  39. void loop() {
  40.   // set the cursor to (0,0):
  41.   lcd.setCursor(0, 0);
  42.   // print from 0 to 9:
  43.   for (int thisChar = 0; thisChar < 10; thisChar++) {
  44.    lcd.print(thisChar);
  45.    delay(500);
  46.   }

  47.   // set the cursor to (16,1):
  48.   lcd.setCursor(16,1);
  49.   // set the display to automatically scroll:
  50.   lcd.autoscroll();
  51.   // print from 0 to 9:
  52.   for (int thisChar = 0; thisChar < 10; thisChar++) {
  53.     lcd.print(thisChar);
  54.     delay(500);
  55.   }
  56.   // turn off automatic scrolling
  57.   lcd.noAutoscroll();
  58.   
  59.   // clear screen for the next loop:
  60.   lcd.clear();
  61. }
复制代码
至于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 ...

感谢大哥.. 等货到了再来研究
回复

使用道具 举报


ADVERTISEMENT

发表于 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~~


之前寫的沒有左右移動的:

Capture.PNG

本帖最后由 李云 于 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屏
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 25-4-2024 10:17 PM , Processed in 0.104133 second(s), 26 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表