首页 > 编程知识 正文

lcd1602只亮不显示(1602液晶显示器怎么连面包板)

时间:2023-05-05 22:49:21 阅读:76609 作者:4530

接线图[ caption id=' attachment _ 1183 ' align=' align none ' width=' 1108 ' ] LCD 1602 a接线图[4位[/caption] 4位接线法[ codeded kgnd * from http://sure NPI.com * created 2015/3/23 */int LCD 1602 int LCD 1602 _ rw=11; int LCD1602_EN=10; int db [ ]={ 6,7,8,9 }; char str1[ ]='欢迎到'; char str2[]='surenpi '; char str3[]='find me '; char str4[]='surenpi.com '; void LCD _ command _ write (int command ) { int i,temp; 数字写入(LCD 1602 _ RS,LOW; 数字写入(LCD 1602 _ rw,LOW ); 数字写入(LCD 1602 _ en,LOW ); temp=command0xf0; for(I=db[0]; i=9; I ) {数字写入(I,temp0x80 ); 时间=1; }数字写入(LCD 1602 _ en,HIGH ); Dlaymicroseconds(1; 数字写入(LCD 1602 _ en,LOW ); temp=(command0x0f ) 4; for(I=db[0]; i=9; I ) {数字写入(I,temp0x80 ); 时间=1; }数字写入(LCD 1602 _ en,HIGH ); Dlaymicroseconds(1; 数字写入(LCD 1602 _ en,LOW ); } voidcleanscreen (int delay time=50 ) LCD_command_write(0x01 ); 延迟(延迟时间); }voidLCD_data_write(intdat ) { int i=0,temp; 数字写入(LCD 1602 _ RS,HIGH ); 数字写入(LCD 1602 _ rw,LOW ); 数字写入(LCD 1602 _ en,LOW ); temp=dat0xf0; for(I=db[0]; i=9; I ) {数字写入(I,temp0x80 ); 时间=1; }数字写入(LCD 1602 _ en,HIGH ); Dlaymicroseconds(1; 数字写入(LCD 1602 _ en,LOW ); temp=(dat0x0f ) 4; for(I=db[0]; i=9; I ) {数字写入(I,temp0x80 ); 时间=1; }数字写入(LCD 1602 _ en,HIGH ); Dlaymicroseconds(1; 数字写入(LCD 1602 _ en,LOW ); }voidLCD_set_xy(intx,int y ) { int address; if(y==0) address=0x80 x; else address=0xC0 x; 液晶屏_ command _ write (地址; }voidLCD_write_char(intx,int y,int dat ) ) LCD_set_xy,y ); 液晶屏_ data _ write (dat; }voidLCD_write_string(intx,int y,char *s,int delayTime=50 ) LCD_set_xy ) x,y ); //设定地址int i=0; while(sI=16 ) /写入字符串(LCD_data_write ) ) s; s; (延迟)延迟时间; if(s ) ) { cleanScreen; LCD_write_string(x,y,s,delayTime ); }voidsetup(void ) { int i=0; for(I=6; i=12; I ) {pinMode(i,OUTPUT); } delay(100); LCD_Command_Write(0x28);//4线 2行 5x7 delay(50); LCD_Command_Write(0x06); delay(50); LCD_Command_Write(0x0c); delay(50); LCD_Command_Write(0x80); delay(50); LCD_Command_Write(0x01); delay(50);} void loop (void){ cleanScreen(); LCD_Write_String(3,0,str1);//第1行,第4个地址起 LCD_Write_String(0,1,str2, 3000);//第2行,第2个地址起 cleanScreen(); LCD_Write_String(0,0,str3); LCD_Write_String(0,1,str4, 3000);} [/codesyntax]  8位接线法 [codesyntax lang="cpp"] /*** 8 bit* from http://surenpi.com*/int DI = 12;int RW = 11;int DB[] = {3, 4, 5, 6, 7, 8, 9, 10};//使用数组来定义总线需要的管脚int Enable = 2; void LcdCommandWrite(int value){ // 定义所有引脚 int i = 0; for (i=DB[0]; i <= DI; i++) //总线赋值 { digitalWrite(i,value & 01);//因为1602液晶信号识别是D7-D0(不是D0-D7),这里是用来反转信号。 value >>= 1; } digitalWrite(Enable,LOW); delayMicroseconds(1); digitalWrite(Enable,HIGH); delayMicroseconds(1); // 延时1ms digitalWrite(Enable,LOW); delayMicroseconds(1); // 延时1ms} void LcdDataWrite(int value){ // 定义所有引脚 int i = 0; digitalWrite(DI, HIGH); digitalWrite(RW, LOW); for (i=DB[0]; i <= DB[7]; i++) { digitalWrite(i,value & 01); value >>= 1; } digitalWrite(Enable,LOW); delayMicroseconds(1); digitalWrite(Enable,HIGH); delayMicroseconds(1); digitalWrite(Enable,LOW); delayMicroseconds(1); // 延时1ms} void setup (void){ int i = 0; for(i=Enable; i <= DI; i++) { pinMode(i,OUTPUT); } delay(100); // 短暂的停顿后初始化LCD // 用于LCD控制需要 LcdCommandWrite(0x38); // 设置为8-bit接口,2行显示,5x7文字大小 delay(64); LcdCommandWrite(0x38); // 设置为8-bit接口,2行显示,5x7文字大小 delay(50); LcdCommandWrite(0x38); // 设置为8-bit接口,2行显示,5x7文字大小 delay(20); LcdCommandWrite(0x06); // 输入方式设定 // 自动增量,没有显示移位 delay(20); LcdCommandWrite(0x0E); // 显示设置 // 开启显示屏,光标显示,无闪烁 delay(20); LcdCommandWrite(0x01); // 屏幕清空,光标位置归零 delay(100); LcdCommandWrite(0x80); // 显示设置 // 开启显示屏,光标显示,无闪烁 delay(20); } void loop (void){ LcdCommandWrite(0x01); // 屏幕清空,光标位置归零 delay(10); LcdCommandWrite(0x80+3); delay(10); // 写入欢迎信息 LcdDataWrite('W'); LcdDataWrite('e'); LcdDataWrite('l'); LcdDataWrite('c'); LcdDataWrite('o'); LcdDataWrite('m'); LcdDataWrite('e'); LcdDataWrite(' '); LcdDataWrite('t'); LcdDataWrite('o'); delay(10); LcdCommandWrite(0xc0+1); // 定义光标位置为第二行第二个位置 delay(10); LcdDataWrite('g'); LcdDataWrite('e'); LcdDataWrite('e'); LcdDataWrite('k'); LcdDataWrite('-'); LcdDataWrite('w'); LcdDataWrite('o'); LcdDataWrite('r'); LcdDataWrite('k'); LcdDataWrite('s'); LcdDataWrite('h'); LcdDataWrite('o'); LcdDataWrite('p'); delay(5000); LcdCommandWrite(0x01); // 屏幕清空,光标位置归零 delay(10); LcdDataWrite('I'); LcdDataWrite(' '); LcdDataWrite('a'); LcdDataWrite('m'); LcdDataWrite(' '); LcdDataWrite('h'); LcdDataWrite('o'); LcdDataWrite('n'); LcdDataWrite('g'); LcdDataWrite('y'); LcdDataWrite('i'); delay(3000); LcdCommandWrite(0x02); //设置模式为新文字替换老文字,无新文字的地方显示不变。 delay(10); LcdCommandWrite(0x80+5); //定义光标位置为第一行第六个位置 delay(10); LcdDataWrite('t'); LcdDataWrite('h'); LcdDataWrite('e'); LcdDataWrite(' '); LcdDataWrite('a'); LcdDataWrite('d'); LcdDataWrite('m'); LcdDataWrite('i'); LcdDataWrite('n'); delay(5000);} [/codesyntax] 使用LiquidCrystal库来连接 使用LiquidCrystal这个库的话,代码会简洁很多,而且还封装还一些常用函数。 [codesyntax lang="cpp"] /**7 gpios demoThe circuit:VSS GNDVDD 5VV0 10K resistor or GNDRS 12RW 11EN 10D4 9D3 8D2 7D1 6from www.surenpi.comcreated 2015/3/25*/#include <LiquidCrystal.h>LiquidCrystal lcd(12, 11, 10, 9, 8, 7, 6);void setup() { lcd.begin(16, 2); lcd.print("www.surenpi.com");}void loop() { lcd.setCursor(0, 1); lcd.print(millis()/1000);} [/codesyntax] 注意:上面的代码中使用了7个GPIO接口,其实把RW接到GND上也行,这样的就可以节省一个GPIO。如果要把RW接GND话,就要使用另外一个构造函数:LiquidCrystal lcd(12, 10, 9, 8, 7, 6) 参考  树莓派上怎么连接LCD1602呢,请看这里。

转载于:https://my.oschina.net/surenpi/blog/604780

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。