📄 lcd 18b20.c
字号:
/************************************
连接方式:RS---P2.5 RW---P2.6 E---P2.7
18B20.2---P1.0
***************************************/
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar code table[]="wen du:";
uchar code table1[]="time:";
sbit lcden=P2^7;
sbit lcdrs=P2^5;
sbit lcdrw=P2^6;
sbit DQ=P1^0;
sbit P3_2=P3^2;
unsigned int shi=23;
unsigned int fen=58;
unsigned int miao=55;
unsigned int deda=0;
unsigned char XiSh_Buf[8];
unsigned char wd[6];
uchar num,xz=0;
uchar num1[3];
uint ii,aa;
void delay(void)
{
uint x,y;
for(x=5;x>0;x--)
for(y=7269;y>0;y--);
}
void Delay(int num)//延时函数
{
while(num--) ;
}
void delay1(void)
{
uint y;
for(y=100;y>0;y--);
}
void delayms(void)
{
unsigned char k,y;
for (k=1000; k>0; k--)
for (y=50;y>0;y--);
}
void Init_DS18B20(void)//初始化ds1820
{
unsigned char x=0;
DQ = 1; //DQ复位
Delay(8); //稍做延时
DQ = 0; //单片机将DQ拉低
Delay(80); //精确延时 大于 480us
DQ = 1; //拉高总线
Delay(14);
x=DQ; //稍做延时后 如果x=0则初始化成功 x=1则初始化失败
Delay(20);
}
void WriteOneChar(unsigned char dat)//写一个字节
{
unsigned char i=0;
for (i=8; i>0; i--)
{
DQ = 0;
DQ = dat&0x01;
Delay(5);
DQ = 1;
dat>>=1;
}
}
unsigned char ReadOneChar(void)//读一个字节
{
unsigned char i=0;
unsigned char dat = 0;
for (i=8;i>0;i--)
{
DQ = 0; // 给脉冲信号
dat>>=1;
DQ = 1; // 给脉冲信号
if(DQ)
dat|=0x80;
Delay(4);
}
return(dat);
}
unsigned int ReadTemperature(void)//读取温度
{
unsigned char a=0;
unsigned char b=0;
unsigned int t=0;
float tt=0;
Init_DS18B20();
WriteOneChar(0xCC); // 跳过读序号列号的操作
WriteOneChar(0x44); // 启动温度转换
Init_DS18B20();
WriteOneChar(0xCC); //跳过读序号列号的操作
WriteOneChar(0xBE); //读取温度寄存器
a=ReadOneChar(); //读低8位
b=ReadOneChar(); //读高8位
t=b;
t<<=8;
t=t|a;
tt=t*0.0625;
t= tt*10+0.5; //放大10倍输出并四舍五入
return(t);
}
void write_com(uchar com)
{
lcdrs=0;
lcdrw=0;
P0=com;
delay1();
lcden=1;
delay1();
lcden=0;
}
void write_data(uchar date)
{
lcdrs=1;
lcdrw=0;
P0=date;
delay1();
lcden=1;
delay1();
lcden=0;
}
void init()
{
lcden=0;
write_com(0x38);
delay1();
write_com(0x38);
delay1();
write_com(0x38);
delay1();
write_com(0x38);
write_com(0x08);
write_com(0x01);
write_com(0x0c);
write_com(0x06);
// write_com(0x80+0x03);
// write_com(0x80+0x40);
}
void initTimer (void)
{
// timer0_tick=0;
EA=0;
TMOD=0x01;
TH0=0xb0;
TL0=0;
// TH1=-9460/256;
// TL1=-9460%256; //初始化计数器初值
PT0=1;
TR0=1;
ET0=1;
// TR1=1;
// ET1=1;
EA =1;
}
void timer0 (void) interrupt 1
{
// unsigned int i;
TR0=0;
TH0=0x3c;
TL0=0xb0;
TR0=1;
deda++;
if(deda==20)
{ miao+=1;
deda=0;
if(miao==60)
{fen+=1;
miao=0;
if(fen==60)
{shi+=1;
fen=0;
if(shi==24)
{shi=0;}
}
}
XiSh_Buf[7]= miao%10+'0';
XiSh_Buf[6]= miao/10+'0';
XiSh_Buf[4]= fen%10+'0';
XiSh_Buf[3]= fen/10+'0';
XiSh_Buf[1]= shi%10+'0';
XiSh_Buf[0]= shi/10+'0';
}
}
uchar kbscan(void)
{
unsigned char sccode,recode;
P1=0x07; //发0扫描,列线输入
if ((P1 & 0x07) != 0x07) //有键按下
{
delayms(); //延时去抖动
if ((P1&0x07)!= 0x07)
{
sccode = 0xF7; //逐行扫描初值
while((sccode&0x01)!=0)
{
P1=sccode;
if((P1&0x07)!=0x07)
{
recode=(P1&0x07)|0xf8;
return((~sccode)+(~recode));
}
else
sccode=(sccode<<1)|0x01;
}
}
}
return 0; //无键按下,返回0
}
void dd(void)
{uchar key;
key=kbscan();
if(xz==1){ switch(key)
{
case 0x24: shi-=1; if(shi>=24) shi=23;
break;
case 0x22:
shi+=1; if(shi>=24) shi=0;
break;
default:; } }
else{ switch(key)
{
case 0x24: fen-=1; if(fen>=60) shi=59;
break;
case 0x22:
fen+=1; if(fen>=60) fen=0;
break;
default:; } }
XiSh_Buf[7]= miao%10+'0';
XiSh_Buf[6]= miao/10+'0';
XiSh_Buf[4]= fen%10+'0';
XiSh_Buf[3]= fen/10+'0';
XiSh_Buf[1]= shi%10+'0';
XiSh_Buf[0]= shi/10+'0';
}
void main()
{
XiSh_Buf[5]=0x3A;
XiSh_Buf[2]=0x3A;
wd[3]='.';
wd[5] =0xdf;
init();
initTimer();
// write_data('1') ;
for(num=0;num<7;num++)
{
write_data(table[num]);
//delay1();
}
write_com(0x80+0x40);
for(num=0;num<5;num++)
{
write_data(table1[num]);
//delay1();
}
while(1)
{
aa= ReadTemperature();
wd[0]=(aa/1000)%10+'0';
wd[1]=(aa/100)%10+'0';
wd[2]=(aa/10)%10+'0';
wd[4]=(aa/1)%10+'0';
write_com(0x80+0x08);
for(num=0;num<6;num++)
{
write_data(wd[num]);
// delay2();
}
if(P3_2==0)
{
delayms(); //延时去抖动
if(P3_2==0)
{ xz+=1;
if(xz==3) xz=0; }}
switch(xz)
{
case 1: dd();
break;
case 2: dd();
break;
default:;
}
write_com(0x80+0x46);
for(num=0;num<8;num++)
{
write_data(XiSh_Buf[num]);
//delay2();
}
}
/* for(num=0;num<16;num++)
{
write_com(0x18);
delay1();
} */
/* while(1) {
write_com(0x0c);
for(num=0;num<999;num++)
{ num1[0]=(num/100)%10;
num1[1]=(num/10)%10;
num1[2]=(num/1)%10;
write_com(0x80+0x4d);
for(ii=0; ii<3; ii++)
{write_data(num1[ii]+'0');
delay1(); }
}
} */
// while(1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -