最近做的一個設計在仿真中lcd1602是可以顯示的,如圖一
但在普中V3.0開發板上lcd1602不能正確顯示,如圖二
普中lcd1602的RS、RW、E管腳分別是p2.7,p2.5,p2.6 仿真中lcd1602的RS、RW、E也是
普中開發板沒有問題,可以使用光盤中的程序
希望大佬能夠指導一下我哪里有錯誤,萬分感謝,拜托了,找了一下午都沒找出來
下面貼出lcd1602相關的單片機程序
- /***********************************************************************************************************
- LCD1602相關函數
- ***********************************************************************************************************/
- sbit LCDRS = P2^6;
- sbit LCDEN = P2^7;
- sbit LCDRW = P2^5;
- sbit D0 = P0^0;
- sbit D1 = P0^1;
- sbit D2 = P0^2;
- sbit D3 = P0^3;
- sbit D4 = P0^4;
- sbit D5 = P0^5;
- sbit D6 = P0^6;
- sbit D7 = P0^7;
- //LCD延時
- void LCDdelay(uint z) //該延時大約100us(不精確,液晶操作的延時不要求很精確)
- {
- uint x,y;
- for(x=z;x>0;x--)
- for(y=2;y>0;y--);
- }
- void LCD_WriteData(uchar dat)
- {
- if(dat&0x01)D0=1;else D0=0;
- if(dat&0x02)D1=1;else D1=0;
- if(dat&0x04)D2=1;else D2=0;
- if(dat&0x08)D3=1;else D3=0;
- if(dat&0x10)D4=1;else D4=0;
- if(dat&0x20)D5=1;else D5=0;
- if(dat&0x40)D6=1;else D6=0;
- if(dat&0x80)D7=1;else D7=0;
- }
- //寫命令
- void write_com(uchar com)
- {
- LCDRS=0;
- LCDRW = 0;
- LCD_WriteData(com);
- LCDdelay(2);
- LCDEN=1;
- LCDdelay(2);
- LCDEN=0;
- }
- //寫數據
- void write_data(uchar date)
- {
- LCDRS=1;
- LCDRW = 0;
- LCD_WriteData(date);
- // DAT=date;
- LCDdelay(2);
- LCDEN=1;
- LCDdelay(2);
- LCDEN=0;
- }
- /*------------------------------------------------
- 選擇寫入位置
- ------------------------------------------------*/
- void SelectPosition(unsigned char x,unsigned char y)
- {
- if (x == 0)
- {
- write_com(0x80 + y); //表示第一行
- }
- else
- {
- write_com(0xC0 + y); //表示第二行
- }
- }
- /*------------------------------------------------
- 寫入字符串函數
- ------------------------------------------------*/
- void LCD_Write_String(unsigned char x,unsigned char y,unsigned char *s)
- {
- SelectPosition(x,y);
- while (*s)
- {
- write_data( *s);
- s ++;
- }
- }
- //========================================================================
- // 函數: void LCD_Write_Char(uchar x,uchar y,uint s,uchar l)
- // 應用: LCD_Write_Char(0,1,366,4) ;
- // 描述: 在第0行第一個字節位置顯示366的后4位,顯示結果為 0366
- // 參數: x:行,y:列,s:要顯示的字,l:顯示的位數
- // 返回: none.
- //========================================================================
- void LCD_Write_Char(uchar x,uchar y,uint s,uchar l)
- {
- SelectPosition(x,y) ;
- if(l>=5)
- write_data(0x30+s/10000%10); //萬位
- if(l>=4)
- write_data(0x30+s/1000%10); //千位
- if(l>=3)
- write_data(0x30+s/100%10); //百位
- if(l>=2)
- write_data(0x30+s/10%10); //十位
- if(l>=1)
- write_data(0x30+s%10); //個位
- }
- //1602初始化
- void Init1602()
- {
- uchar i=0;
- write_com(0x38);//屏幕初始化
- write_com(0x0c);//打開顯示 無光標 無光標閃爍
- write_com(0x06);//當讀或寫一個字符是指針后一一位
- write_com(0x01);//清屏
- }
- void Display1(c,temp,temp2,temp3) //溫度,煙霧,PM2.5,甲醛
- {
- //時時溫度
- LCD_Write_Char(0,6,c/10,2);
- write_data('.');
- LCD_Write_Char(0,9,c%10,1);
- //時時煙霧
- LCD_Write_Char(0,13,temp,3);
- //時時pm25
- LCD_Write_Char(1,6,temp2,3);
- //時時甲醛
- LCD_Write_Char(1,13,temp3,3);
- }
- void Display2(yushe_wendu,yushe_yanwu,yushe_pm25,yushe_jiaquan)
- {
- //顯示預設溫度
- LCD_Write_Char(0,6,yushe_wendu,2);
- //顯示預設煙霧
- LCD_Write_Char(0,13,yushe_yanwu,3);
- //顯示預設PM2.5
- LCD_Write_Char(1,6,yushe_pm25,3);
-
- //顯示預設甲醛
- LCD_Write_Char(1,13,yushe_jiaquan,3);
- }
復制代碼 |