这个时候(单排方块)通常要考虑接线是否正确。 |
只有一排方块,初始化不成功 |
普中开发板 有些 LCD 是用4位的。 |
有没有可能插反了? |
调对比度电位器 |
这是程序代码,求指点 void Lcd1602_Delay1ms(uint c) //?? 0us { uchar a,b; for (; c>0; c--) { for (b=199;b>0;b--) { for(a=1;a>0;a--); } } } #ifndef LCD1602_4PINS //???????LCD1602_4PINS? void LCD_WriteCmd(uchar com) //???? { LCD1602_E = 0; //?? LCD1602_RS = 0; //?????? LCD1602_RW = 0; //???? LCD1602_DATAPINS = com; //???? Lcd1602_Delay1ms(1); //?????? LCD1602_E = 1; //???? Lcd1602_Delay1ms(5); //???? LCD1602_E = 0; } #else void LCD_WriteCmd(uchar com) //???? { LCD1602_E = 0; //???? LCD1602_RS = 0; //?????? LCD1602_RW = 0; //???? LCD1602_DATAPINS = com; //??4???????P0?????,?????????? Lcd1602_Delay1ms(2); LCD1602_E = 1; //???? Lcd1602_Delay1ms(5); LCD1602_E = 0; // Lcd1602_Delay1ms(1); LCD1602_DATAPINS = com << 4; //????? Lcd1602_Delay1ms(1); LCD1602_E = 1; //???? Lcd1602_Delay1ms(5); LCD1602_E = 0; } #endif /******************************************************************************* * ? ? ? : LcdWriteData * ???? : ?LCD????????? * ? ? : dat * ? ? : ? *******************************************************************************/ #ifndef LCD1602_4PINS void LCD_WriteData(uchar dat) //???? { LCD1602_E = 0; //???? LCD1602_RS = 1; //?????? LCD1602_RW = 0; //???? LCD1602_DATAPINS = dat; //???? Lcd1602_Delay1ms(1); LCD1602_E = 1; //???? Lcd1602_Delay1ms(5); //???? LCD1602_E = 0; } #else void LCD_WriteData(uchar dat) //???? { LCD1602_E = 0; //???? LCD1602_RS = 1; //?????? LCD1602_RW = 0; //???? LCD1602_DATAPINS = dat; //??4???????P0?????,?????????? Lcd1602_Delay1ms(1); LCD1602_E = 1; //???? Lcd1602_Delay1ms(5); LCD1602_E = 0; LCD1602_DATAPINS = dat << 4; //????? Lcd1602_Delay1ms(1); LCD1602_E = 1; //???? Lcd1602_Delay1ms(5); LCD1602_E = 0; } #endif /******************************************************************************* * ? ? ? : LcdInit() * ???? : ???LCD? * ? ? : ? * ? ? : ? *******************************************************************************/ #ifndef LCD1602_4PINS void LcdInit() //LCD?????? { LCD_WriteCmd(0x38); //??? LCD_WriteCmd(0x0c); //???????? LCD_WriteCmd(0x06); //??????1 LCD_WriteCmd(0x01); //?? LCD_WriteCmd(0x80); //???????? } #else void LCD_Init() //LCD?????? { LCD_WriteCmd(0x32); //?8?????4??? LCD_WriteCmd(0x28); //????????? LCD_WriteCmd(0x0c); //???????? LCD_WriteCmd(0x06); //??????1 LCD_WriteCmd(0x01); //?? LCD_WriteCmd(0x80); //???????? } #endif void LCD_Clear() { LCD_WriteCmd(0x01); LCD_WriteCmd(0x80); } //?????????? void LCD_Dispstring(u8 x,u8 line,u8 *p) { char i=0; if(line<1) //????? { while(*p!='\0') { if(i<16-x) { LCD_WriteCmd(0x80+i+x); } else { LCD_WriteCmd(0x40+0x80+i+x-16); } LCD_WriteData(*p); p++; i++; } } else //?2??? { while(*p!='\0') { if(i<16-x) { LCD_WriteCmd(0x80+0x40+i+x); } else { LCD_WriteCmd(0x80+i+x-16); } LCD_WriteData(*p); p++; i++; } } } ??????: #include "public.h" #include "lcd.h" #include "temp.h" #include "i2c.h" sbit k3=P3^1; //??????? sbit k1=P3^0; //? sbit k2=P3^2; //? sbit led=P2^0; //????? sbit beep=P1^5; //????? sbit relay=P1^4; //???? sbit moto=P1^0; //???? char set_templ=25,set_temph=32; //?????????? u16 temp_val; //??????? u8 mode; //???? void Temp_DataPros() { short temp; u8 temp_buf[5]; temp=Ds18b20ReadTemp(); temp_val=temp; if(temp<0) { temp=-temp; LCD_Dispstring(2+5,0,"-"); } else { LCD_Dispstring(2+5,0," "); } temp_buf[0]=temp/100+0x30; temp_buf[1]=temp%100/10+0x30; temp_buf[2]='.'; temp_buf[3]=temp%100%10+0x30; temp_buf[4]='\0'; LCD_Dispstring(2+6,0,temp_buf); //???????xx.x temp_buf[0]=set_temph/10+0x30; temp_buf[1]=set_temph%10+0x30; temp_buf[2]='\0'; LCD_Dispstring(5,1,temp_buf); //??????????xx temp_buf[0]=set_templ/10+0x30; temp_buf[1]=set_templ%10+0x30; temp_buf[2]='\0'; LCD_Dispstring(14,1,temp_buf); //??????????xx } #define K1_MODE 1 #define K2_ADD 2 #define K3_DEC 3 //mode: 0-???? 1-???? u8 KEY_Scan(u8 mode) { static u8 key=1; if(key&&(k1==0||k2==0||k3==0)) { delay(300); //?? key=0; if(k3==0) { return K1_MODE; } else if(k1==0) { return K2_ADD; } else if(k2==0) { return K3_DEC; } } else if(k1==1&&k2==1&&k3==1) { key=1; } if(mode) { key=1; } return 0; } void KEY_Pros() { u8 key; u8 temph_buf[3]; key=KEY_Scan(0); if(key==K1_MODE) //???? { mode++; LCD_Clear(); if(mode==1) { LCD_Dispstring(0,0,"SETH: C"); } else if(mode==2) { LCD_Dispstring(0,1,"SETL: C"); } else { mode=0; LCD_Dispstring(2,0,"Temp: C"); LCD_Dispstring(0,1,"SETH: "); LCD_Dispstring(9,1,"SETL: "); } } if(mode==1) //?????? { switch(key) { case K2_ADD: //? set_temph++; if(set_temph>=80)set_temph=80; break; case K3_DEC: //? set_temph--; if(set_temph<=0)set_temph=0; break; } temph_buf[0]=set_temph/10+0x30; temph_buf[1]=set_temph%10+0x30; temph_buf[2]='\0'; LCD_Dispstring(6,0,temph_buf); At24c02Write(0,set_temph); } else if(mode==2) //?????? { switch(key) { case K2_ADD: //? set_templ++; if(set_templ>=80)set_templ=80; break; case K3_DEC: //? set_templ--; if(set_templ<=0)set_templ=0; break; } temph_buf[0]=set_templ/10+0x30; temph_buf[1]=set_templ%10+0x30; temph_buf[2]='\0'; LCD_Dispstring(6,1,temph_buf); At24c02Write(2,set_templ); } } void sound() { u8 i=50; while(i--) { beep=!beep; delay(10); } } void TempData_Compare() { if(temp_val>set_temph*10) //????????? ?? ?? { led=0; moto=1; relay=1; sound(); } else if(temp_val<set_templ*10) //????????? ?? ?? { led=1; moto=0; relay=0; sound(); } else //?????????????? ???? ???? ???? { moto=0; led=1; relay=1; } } void kai_display() { if(At24c02Read(255)!=18) { At24c02Write(0,set_temph); At24c02Write(2,set_templ); At24c02Write(255,18); } else { set_temph=At24c02Read(0); set_templ=At24c02Read(2); } LCD_Dispstring(2,0,"Temp: C"); LCD_Dispstring(0,1,"SETH: "); LCD_Dispstring(9,1,"SETL: "); } void main() { moto=0; led=1; relay=1; LCD_Init(); kai_display(); while(1) { if(mode==0) Temp_DataPros(); KEY_Pros(); TempData_Compare(); } } |