新手学Arduino_第1页
新手学Arduino_第2页
新手学Arduino_第3页
新手学Arduino_第4页
新手学Arduino_第5页
已阅读5页,还剩11页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、新手学Arduino【液晶显示】发表于 2011 年 12 月 31 日 由 sacheo本次试验使用arduino直接驱动1602液晶显示文字。1602液晶在应用中非常广泛,最初的1602液晶使用的是HD44780控制器,现在各个厂家的1602模块基本上都是采用了与之兼容的IC,所以特性上基本都是一致的。1602LCD主要技术参数:显示容量为16×2个字符;芯片工作电压为4.55.5V;工作电流为2.0mA(5.0V);模块最佳工作电压为5.0V;字符尺寸为2.95×4.35(W×H)mm。1602液晶接口引脚定义:接口说明:1、两

2、组电源 一组是模块的电源 一组是背光板的电源 一般均使用5V供电。本次试验背光使用3.3V供电也可以工作。2、VL是调节对比度的引脚,串联不大于5K的电位器进行调节。本次实验使用1K的电阻来设定对比度。其连接分高电位与低电位接法,本次使用低电位接法,串联1K电阻后接GND。3、RS 是很多液晶上都有的引脚 是命令/数据选择引脚 该脚电平为高时表示将进行数据操作;为低时表示进行命令操作。4、RW 也是很多液晶上都有的引脚 是读写选择端 该脚电平为高是表示要对液晶进行读操作;为低时表示要进行写操作。5、E 同样很多液晶模块有此引脚 通常在总线上信号稳定后给一正脉冲通知把数据读走,在此脚为高电平的时

3、候总线不允许变化。6、D0D7 8 位双向并行总线,用来传送命令和数据。7、BLA是背光源正极,BLK是背光源负极。1602液晶的基本操作分以下四种:下图就是1602液晶实物图1602直接与arduino通信,根据产品手册描述,分8位连接法与4位连接法,咱们先使用8位连接法进行实验。硬件连接方式如下图代码如下1. int DI = 12;2. int RW = 11;3. int DB = 3, 4, 5, 6, 7, 8, 9, 10;/使用数组来定义总线需要的管脚4. int Enable = 2;5.6. void LcdCommandWrite(int value) 7. / 定义所有

4、引脚8. int i = 0;9. for (i=DB0; i <= DI; i+) /总线赋值10. 11.    digitalWrite(i,value & 01);/因为1602液晶信号识别是D7-D0(不是D0-D7),这里是用来反转信号。12.    value >>= 1;13. 14. digitalWrite(Enable,LOW);15. delayMicroseconds(1);16. digitalWrite(Enable,HIGH);17. delayMicroseconds(1); 

5、0;/ 延时1ms18. digitalWrite(Enable,LOW);19. delayMicroseconds(1);  / 延时1ms20. 21.22. void LcdDataWrite(int value) 23. / 定义所有引脚24. int i = 0;25. digitalWrite(DI, HIGH);26. digitalWrite(RW, LOW);27. for (i=DB0; i <= DB7; i+) 28.    digitalWrite(i,value & 01);29.    v

6、alue >>= 1;30. 31. digitalWrite(Enable,LOW);32. delayMicroseconds(1);33. digitalWrite(Enable,HIGH);34. delayMicroseconds(1);35. digitalWrite(Enable,LOW);36. delayMicroseconds(1);  / 延时1ms37. 38.39. void setup (void) 40. int i = 0;41. for (i=Enable; i <= DI; i+) 42.    pi

7、nMode(i,OUTPUT);43. 44. delay(100);45. / 短暂的停顿后初始化LCD46. / 用于LCD控制需要47. LcdCommandWrite(0×38);  / 设置为8-bit接口,2行显示,5×7文字大小48. delay(64);49. LcdCommandWrite(0×38);  / 设置为8-bit接口,2行显示,5×7文字大小50. delay(50);51. LcdCommandWrite(0×38);  / 设置为8-bit接口,2行

8、显示,5×7文字大小52. delay(20);53. LcdCommandWrite(0×06);  / 输入方式设定54.                          / 自动增量,没有显示移位55. delay(20);56. LcdCommandWrite(0x0E);  / 显示设置57.         

9、60;                / 开启显示屏,光标显示,无闪烁58. delay(20);59. LcdCommandWrite(0×01);  / 屏幕清空,光标位置归零60. delay(100);61. LcdCommandWrite(0×80);  / 显示设置62.                   

10、;       / 开启显示屏,光标显示,无闪烁63. delay(20);64. 65.66. void loop (void) 67.   LcdCommandWrite(0×01);  / 屏幕清空,光标位置归零68.   delay(10);69.   LcdCommandWrite(0×80+3);70.   delay(10);71.   / 写入欢迎信息72.   LcdDataWr

11、ite(W);73.   LcdDataWrite(e);74.   LcdDataWrite(l);75.   LcdDataWrite(c);76.   LcdDataWrite(o);77.   LcdDataWrite(m);78.   LcdDataWrite(e);79.   LcdDataWrite( );80.   LcdDataWrite(t);81.   LcdDataWrite(o);82. &

12、#160; delay(10);83.   LcdCommandWrite(0xc0+1);  / 定义光标位置为第二行第二个位置84.   delay(10);85.   LcdDataWrite(g);86.   LcdDataWrite(e);87.   LcdDataWrite(e);88.   LcdDataWrite(k);89.   LcdDataWrite(-);90.   LcdDataW

13、rite(w);91.   LcdDataWrite(o);92.   LcdDataWrite(r);93.   LcdDataWrite(k);94.   LcdDataWrite(s);95.   LcdDataWrite(h);96.   LcdDataWrite(o);97.   LcdDataWrite(p);98.   delay(5000);99.   LcdCommandWrite(0×0

14、1);  / 屏幕清空,光标位置归零100.   delay(10);101.   LcdDataWrite(I);102.   LcdDataWrite( );103.   LcdDataWrite(a);104.   LcdDataWrite(m);105.   LcdDataWrite( );106.   LcdDataWrite(h);107.   LcdDataWrite(o);108.  

15、60;LcdDataWrite(n);109.   LcdDataWrite(g);110.   LcdDataWrite(y);111.   LcdDataWrite(i);112.   delay(3000);113.   LcdCommandWrite(0×02); /设置模式为新文字替换老文字,无新文字的地方显示不变。114.   delay(10);115.   LcdCommandWrite(0×80+5); /定义光标位置

16、为第一行第六个位置116.   delay(10);117.   LcdDataWrite(t);118.   LcdDataWrite(h);119.   LcdDataWrite(e);120.   LcdDataWrite( );121.   LcdDataWrite(a);122.   LcdDataWrite(d);123.   LcdDataWrite(m);124.   LcdDataWrite(i);

17、125.   LcdDataWrite(n);126.   delay(5000);127. 实验效果如下 4位接法在正常使用下,8位接法基本把arduino的数字端口占满了,如果想要多接几个传感器就没有端口了,这种情况下怎么处理呢,咱们可以使用4位接法。4位接法的硬件连接方法如下图硬件接好后把下面的代码上传到控制板上,看看效果。1. int LCD1602_RS=12;2. int LCD1602_RW=11;3. int LCD1602_EN=10;4. int DB = 6, 7, 8, 9;5. char str1=”Welcome t

18、o”;6. char str2=”geek-workshop”;7. char str3=”this is the”;8. char str4=”4-bit interface”;9.10. void LCD_Command_Write(int command)11. 12. int i,temp;13. digitalWrite( LCD1602_RS,LOW);14. digitalWrite( LCD1602_RW,LOW);15. digitalWrite( LCD1602_EN,LOW);16.17. temp=command & 0xf0;18. for (i=DB0; i

19、 <= 9; i+)19. 20.    digitalWrite(i,temp & 0×80);21.    temp <<= 1;22. 23.24. digitalWrite( LCD1602_EN,HIGH);25. delayMicroseconds(1);26. digitalWrite( LCD1602_EN,LOW);27.28. temp=(command & 0x0f)<<4;29. for (i=DB0; i <= 10; i+)30. 31.    

20、digitalWrite(i,temp & 0×80);32.    temp <<= 1;33. 34.35. digitalWrite( LCD1602_EN,HIGH);36. delayMicroseconds(1);37. digitalWrite( LCD1602_EN,LOW);38. 39.40. void LCD_Data_Write(int dat)41. 42. int i=0,temp;43. digitalWrite( LCD1602_RS,HIGH);44. digitalWrite( LCD1602_RW,LOW)

21、;45. digitalWrite( LCD1602_EN,LOW);46.47. temp=dat & 0xf0;48. for (i=DB0; i <= 9; i+)49. 50.    digitalWrite(i,temp & 0×80);51.    temp <<= 1;52. 53.54. digitalWrite( LCD1602_EN,HIGH);55. delayMicroseconds(1);56. digitalWrite( LCD1602_EN,LOW);57.58. temp=(da

22、t & 0x0f)<<4;59. for (i=DB0; i <= 10; i+)60. 61.    digitalWrite(i,temp & 0×80);62.    temp <<= 1;63. 64.65. digitalWrite( LCD1602_EN,HIGH);66. delayMicroseconds(1);67. digitalWrite( LCD1602_EN,LOW);68. 69.70. void LCD_SET_XY( int x, int y )71. 72. 

23、60; int address;73.   if (y =0)    address = 0×80 + x;74.   else          address = 0xC0 + x;75.   LCD_Command_Write(address);76. 77.78. void LCD_Write_Char( int x,int y,int dat)79. 80.   LCD_SET_XY( x, y );81.

24、   LCD_Data_Write(dat);82. 83.84. void LCD_Write_String(int X,int Y,char *s)85. 86.     LCD_SET_XY( X, Y );    /设置地址87.     while (*s)             /写字符串88.     89.       LCD_Data_Write(*s);9

25、0.       s +;91.     92. 93.94. void setup (void)95. 96.   int i = 0;97.   for (i=6; i <= 12; i+)98.    99.      pinMode(i,OUTPUT);100.    101.   delay(100);102.   LCD_Command_Write(0

26、5;28);/4线 2行 5×7103.   delay(50);104.   LCD_Command_Write(0×06);105.   delay(50);106.   LCD_Command_Write(0x0c);107.   delay(50);108.   LCD_Command_Write(0×80);109.   delay(50);110.   LCD_Command_Write(0×01);111.   delay(50);112.113. 114.115. void loop (void)116. 117.    LCD_C

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论