本帖最后由 574605863 于 2015-5-12 21:01 编辑
LCD1602不显示数据,求大神指导。谢谢大家。
#include<reg51.h> //????????????
#include<intrins.h> //??_nop_()????????
#define uchar unsigned char
#define uint unsigned int
#define DelayNOP(){_nop_;_nop_;_nop_;_nop_;}
sbit DQ=P3^3; //Êý¾ÝÏß
sbit HI_LED =P2^3; //¸ßεÍα¨¾¯ÉÁ˸
sbit LO_LED =P2^6;
sbit BEEP=P3^7; //±¨¾¯Æ÷
sbit K1=P1^7;//Õý³£ÏÔʾ
sbit K2=P1^4;//±¨¾¯
sbit K3=P1^1;
sbit RS=P2^0;
sbit RW=P2^1;
sbit E=P2^2;
sbit BF=P0^7;
uchar RomCodeStr[]={"--ROM CODE--"};
uchar RomCode[8] ={0x0C,0x00,0x00,0x00,0x00,0x00,0x00,0x00};//64λROM CODE
uchar code Temp_Disp_
title[] ={"Current Temp: "};
uchar Current_Temp_Display_Buffer[] ={" TEMP: "};
uchar code Temperature_Char[8]={0x0C,0x12,0x12,0x0C,0x00,0x00,0x00,0x00};//ζÈ×Ö·û
uchar Al
ARM_Temp[]={"ALARM TEMP Hi Lo"};
uchar Alarm_HI_LO_STR[]={"Hi: Lo: "};
uchar temp_data[2]={0x00,0x00};
uchar temp_alarm[2]={0x00,0x00};
uchar display[5]={0x00,0x00,0x00,0x00,0x00};//ζÈÖµ
uchar display1[3]={0x0C,0x00,0x00}; //±¨¾¯Öµ
uchar df_Table[] ={0,1,1,2,3,3,4,4,5,6,6,7,8,8,9,9}; //ζÈСÊýλ¶ÔÕÕ±í
char Alarm_Temp_HL[2]={70,-20};
uchar CurrentT=0;
uchar Temp_Value[]={0x00,0x00}; //¶ÁÈ¡µÄζÈÖµ
uchar Display_Digit[]={0,0,0,0}; //´ýÏÔʾµÄ¸÷ζÈÊý
bit HI_Alarm=0,LO_Alarm=0 ; //¸ßµÍα¨¾¯±êÖ¾
bit DS18B20_IS_OK =1; //´«¸ÐÆ÷Õý³£±êÖ¾
uint Time0_Count =0; //¶¨Ê±Æ÷ÑÓʱÀÛ¼Ó
//ÑÓʱ
void DelayXus(int x)
{
uchar i;
while(x--)for(i=0;i<200;i++);
}
bit LCD_Busy_Check()
{
bit result;
RS=0;
RW=1;
E=1;
DelayNOP();
result=(bit)(P0 & 0x80);
E=0;
return result;
}
//ÏòLCDдָÁî
void Write_LCD_Command (uchar cmd)
{
while(LCD_Busy_Check());
RS=0;
RW=0;
E=0;
_nop_();
_nop_();
P0=cmd;
DelayNOP();
E=1;
DelayNOP();
E=0;
}
//ÏòlcdдÊý¾Ý
void Write_lCD_Data(uchar str)
{
while(LCD_Busy_Check()==1);
RS=1;
RW=0;
E=0;
P0=str;
DelayNOP();
E=1;
DelayNOP();
E=0;
}
//ÉèÖóõʼλÖÃ
void Set_LCD_POS(uchar x)
{
Write_LCD_Command (x|0x80);
}
//¶ÔlcdÏÔʾģʽ³õʼ»¯
void LCD_Initialize()
{
DelayXus(5);
Write_LCD_Command (0x38);
DelayXus(5);
Write_LCD_Command (0x0c);
DelayXus(5);
Write_LCD_Command (0x06);
DelayXus(5);
Write_LCD_Command (0x01);
DelayXus(5);
}
//×Ô¶¨Òå×Ö·ûдCGRAM
void Write_NEW_LCD_Char()
{
uchar i;
Write_LCD_Command (0x40);//D'CGRAM
for (i=0;i<8;i++)
Write_lCD_Data(Temperature_Char
);//дÈëζȷûºÅ
}
//ÑÓʱ
void Delay (uint num)
{
while (--num);
}
//³õʼ»¯DS18B20
uchar Init_DS18B20()
{
uchar status;
DQ=1;Delay (8);
DQ=0;Delay (90);
DQ=1;Delay (8);
status = DQ;
Delay (100);
DQ=1;
return status;
}
//¶ÁÒ»×Ö½Ú
uchar ReadOneByte()
{
uchar i,dat =0;
DQ=1;_nop_();
for (i=0;i<8;i++)
{
DQ=0;dat >>=1; DQ=1;_nop_();_nop_();
if(DQ) dat |=0x80;Delay(30); DQ=1;
}
return dat;
}
//д×Ö½Ú
void WriteOneByte(uchar dat)
{
uchar i;
for (i=0;i<8;i++)
DQ=0;DQ=dat&0x01;Delay(5);DQ=1;dat >>=1;
}
//¶ÁȡζÈÖµ
void Read_Temperature()
{
if((Init_DS18B20() ==1))
DS18B20_IS_OK =0;
else
{
WriteOneByte(0xCC); //
WriteOneByte(0x44);
Init_DS18B20();
WriteOneByte(0xCC);
WriteOneByte(0xBE);
}
Temp_Value[0] =ReadOneByte();
Temp_Value[1] =ReadOneByte();
DS18B20_IS_OK =1;
}
//ÉèÖÃζȱ¨¾¯Öµ
void Set_Alarm_Temp_Value()
{
Init_DS18B20();
WriteOneByte(0xCC);
WriteOneByte(0x44);
WriteOneByte(Alarm_Temp_HL[0]);
WriteOneByte(Alarm_Temp_HL[1]);
WriteOneByte(0x7F);
Init_DS18B20();
WriteOneByte(0xCC);
WriteOneByte(0x48);
}
//ÔÚLCDÉÏÏÔʾµ±Ç°Î¶È
void Display_Temperature()
{
uchar i;
uchar t=150 ; //ÑÓʱֵ
uchar ng= 0 ;//¸ºÊý±êʶ
char Signed_Current_Temp;
//Èç¹ûΪ¸ºÊý£¬ÔòÈ¡·´¼Ó1£¬²¢ÉèÖøºÊý±êʶ
if ((Temp_Value[1]&0xF8)==0xF8)
{
Temp_Value[1]=~Temp_Value[1];
Temp_Value[0]=~Temp_Value[0]+1;
if(Temp_Value[0]==0x00)Temp_Value[1]++;
ng=1;//É踺Êý±êʶ
}
//ζÈСÊý²¿·Ö
Display_Digit[0]=df_Table[Temp_Value[0]&0x0F];//»ñȡζÈÕûÊý²¿·Ö
CurrentT =((Temp_Value[0]&0x0F)>>4)|((Temp_Value[0]&0x07)<<4);
Signed_Current_Temp=ng? -CurrentT:CurrentT;
HI_Alarm =Signed_Current_Temp >=Alarm_Temp_HL[0] ?1:0;
LO_Alarm =Signed_Current_Temp <=Alarm_Temp_HL[1] ?1:0;
Display_Digit[3] =CurrentT /100;
Display_Digit[2] =CurrentT % 100 /10;
Display_Digit[1] =CurrentT % 10;
Current_Temp_Display_Buffer[11] =Display_Digit[0]+'0';
Current_Temp_Display_Buffer[10] ='.' ;
Current_Temp_Display_Buffer[9] =Display_Digit[1]+'0';
Current_Temp_Display_Buffer[8] =Display_Digit[2]+'0';
Current_Temp_Display_Buffer[7] =Display_Digit[3]+'0';
//¸ßλΪ0²»ÏÔʾ
if(Display_Digit[3]==0) Current_Temp_Display_Buffer[7] =' ' ;
//¸ßλΪ0ºÍ´Î¸ßλΪ0£¬´Î¸ßλ²»ÏÔʾ
if(Display_Digit[2]==0&Display_Digit[3]==0)
Current_Temp_Display_Buffer[8] =' ' ;
//¸ºÊý·ûºÅÏÔʾÔÚÇ¡µ±Î»ÖÃ
if (ng)
{
if(Current_Temp_Display_Buffer[8]==' ')
Current_Temp_Display_Buffer[8] ='-';
else
if(Current_Temp_Display_Buffer[7] ==' ' )
Current_Temp_Display_Buffer[7] ='-' ;
else
Current_Temp_Display_Buffer[6] ='-' ;
}
//ÔÚµÚÒ»ÐÐÏÔʾ±êÌâ
Set_LCD_POS(0X00);
for(i= 0; i< 16; i++) Write_lCD_Data ( Temp_Disp_Title );
//ÔÚµÚ¶þÐÐÏÔʾµ±Ç°Î¶È
Set_LCD_POS(0X40);
for(i= 0; i< 16; i++)
Write_lCD_Data (Current_Temp_Display_Buffer );
//ÏÔʾζȷûºÅ
Set_LCD_POS(0x4D);Write_lCD_Data (0x00);
Set_LCD_POS(0x4E);Write_lCD_Data ('C');
}
//
//¶¨Ê±Æ÷Öжϣ¬¿ØÖƾ¯±¨ÉùÒô
void T0_INT() interrupt 1
{
TH0 =-1000/256;
TL0 =-1000%256;
BEEP = !BEEP;
if (++Time0_Count == 400)
{
Time0_Count = 0;
if (HI_Alarm) HI_LED = ~HI_LED; else HI_LED =0;
if (LO_Alarm) LO_LED = ~LO_LED; else LO_LED =0;
TR0 =0;
}
}
//ROM CODE ת»»ÓëÏÔʾ
void Display_Rom_Code()
{
uchar i,t;
Set_LCD_POS(0x40);
for(i=0 ;i<8;i++)
{
t =((RomCode&0xF0)>>4);
if(t>9)t += 0x37; else t +='0';
Write_lCD_Data(t); //¸ßλÏÔʾ
t= RomCode & 0x0F;
if(t>9)t += 0x37; else t +='0';
Write_lCD_Data(t); //µÍλÏÔʾ
}
}
//¶ÁÈ¡64λÂë
void Read_Rom_Code()
{
uchar i;
Init_DS18B20();
WriteOneByte(0x33); //¶ÁÐòÁÐÂë
for (i =0; i<8; i++) RomCode = ReadOneByte();
}
//ÏÔʾROM CODE
void Display_RomCode()
{
uchar i;
Set_LCD_POS(0x00);
for (i =0; i<16; i++) //ÏÔʾ±êÌâ
Write_lCD_Data (RomCodeStr) ;
Read_Rom_Code() ;//¶Á64λÐòÁÐÂë
Display_Rom_Code();//ÏÔʾ64λROM CODE
}
//ÏÔʾ±¨¾¯Î¶È
void Disp_Alarm_Temperature()
{
uchar i, ng;
ng =0;
if( Alarm_Temp_HL[0]< 0)
{
Alarm_Temp_HL[0] =~Alarm_Temp_HL[0]+1 ;
ng =1;
}
Alarm_HI_LO_STR[4] = Alarm_Temp_HL[0] /100 + '0';
Alarm_HI_LO_STR[5] = Alarm_Temp_HL[0] /10%10 + '0';
Alarm_HI_LO_STR[6] = Alarm_Temp_HL[0] %10 + '0';
//·Ö½â¸ßθ÷Êýλµ½´ýÏÔʾ´®ÖÐ
if(Alarm_HI_LO_STR[4] =='0') Alarm_HI_LO_STR[4] = ' ';
if(Alarm_HI_LO_STR[4] =='0'&&Alarm_HI_LO_STR[5] =='0')
Alarm_HI_LO_STR[5]=' ';
if (ng)
{
if(Alarm_HI_LO_STR[5]==' ')Alarm_HI_LO_STR[5]='-';
else
if(Alarm_HI_LO_STR[4]==' ')Alarm_HI_LO_STR[4]='-';
else
Alarm_HI_LO_STR[3]='-';
}
//µÍα¨¾¯
ng =0;
if (Alarm_HI_LO_STR[1] <0) //Èç¹ûΪ¸ºÊýÔòÈ¡·´¼Ó1
{
Alarm_Temp_HL[1] =~Alarm_Temp_HL[1] +1;
ng =1;
}
//·Ö½âµÍθ÷Êýλµ½´ýÏÔʾ´®ÖÐ
Alarm_HI_LO_STR[12]= Alarm_Temp_HL[1]/100+ '0';
Alarm_HI_LO_STR[13] =Alarm_Temp_HL[1]/10 % 10+ '0';
Alarm_HI_LO_STR[14] = Alarm_Temp_HL[1] % 10+ '0';
//ÆÁ±Î¸ßλ²»ÏÔʾµÄ0
if (Alarm_HI_LO_STR[12]=='0') Alarm_HI_LO_STR[12] =' ';
if (Alarm_HI_LO_STR[12]=='0'&& Alarm_HI_LO_STR[13] =='0')
Alarm_HI_LO_STR[13] =' ';
if(ng)
{
if(Alarm_HI_LO_STR[13] ==' ') Alarm_HI_LO_STR[13] ='-';
else
if (Alarm_HI_LO_STR[12] ==' ') Alarm_HI_LO_STR[12] ='-';
else
Alarm_HI_LO_STR[11] ='-';
}
//ÏÔʾ¸ßµÍα¨¾¯Î¶ÈÖµ
Set_LCD_POS(0x00);for (i =0; i<16; i++) Write_lCD_Data (Alarm_Temp) ;
Set_LCD_POS(0x40);
for (i =0; i<16; i++) Write_lCD_Data (Alarm_HI_LO_STR) ;
}
//Ö÷º¯Êý
void main()
{ uchar Current_Operation =1 ;//??è?µ±?°2ù×÷?a??ê????è
LCD_Initialize();
IE =0x82;
TMOD =0x01;
TH0 = -1000 /256;
TL0 =-1000 % 256;
TR0 =0;
HI_LED =0;
LO_LED =0 ;
Set_Alarm_Temp_Value();
Read_Temperature();
Delay(50000);
Delay(50000);
while(1)
{
if (K1==0) Current_Operation =1 ;
if (K2==0) Current_Operation =2;
if (K3==0) Current_Operation =3 ;
switch (Current_Operation)
{
case 1://Õýµ±ÏÔʾζÈ
Read_Temperature();
if (DS18B20_IS_OK)
{
if(HI_Alarm==1||LO_Alarm ==1) TR0 =1;
else TR0 =0;
Display_Temperature();
}
DelayXus(100);
break;
case 2: //ÏÔʾ±¨¾¯Î¶È
Read_Temperature();
Disp_Alarm_Temperature();
DelayXus(100);
break;
case 3://ÏÔʾROM CODE
Display_RomCode();
DelayXus(50);
break;
}
}
}
-
-
-
wendu.rar
下载积分: 积分 -1 分
37.89 KB, 下载次数: 2, 下载积分: 积分 -1 分
第一:没添加hex文件
第二:#define DelayNOP(){_nop_;_nop_;_nop_;_nop_;}这里写错了
#define DelayNOP(){_nop_();_nop_();_nop_();_nop_();}
第三:内存耗尽
这里改成
uchar code RomCodeStr[]={"--ROM CODE--"};
uchar RomCode[8] ={0x0C,0x00,0x00,0x00,0x00,0x00,0x00,0x00};//64位ROM CODE
uchar code Temp_Disp_Title[] ={"Current Temp: "};
uchar Current_Temp_Display_Buffer[] ={" TEMP: "};
uchar code Temperature_Char[8]={0x0C,0x12,0x12,0x0C,0x00,0x00,0x00,0x00};//温度字符
uchar code Alarm_Temp[]={"ALARM TEMP Hi Lo"};
第四:逻辑问题了,显示好使,我试了!
一周热门 更多>