标题:
C51单片机&DB18S20使用
[打印本页]
作者:
writtenmap
时间:
2018-1-7 22:09
标题:
C51单片机&DB18S20使用
源代码
#include <AT89x52.h>
#include <Intrins.h>
//---------------------------------------
//1602液晶相关I/O设置
sbit E=P2^3; //1602液晶的E脚接在P2.3口上
sbit RW=P2^4; //1602液晶的RW脚接在P2.4口上
sbit RS=P2^5; //1602液晶的RS脚接在P2.5口上
//---------------------------------------
sbit ds18b20=P3^7;
unsigned char DISbuf; //设置8位的unsigendchar型寄存器用来暂存1602要显示的内容
unsigned char temp1;
unsigned char temp2;
unsigned char temp1buf1;
unsigned char temp1buf2;
bit tc;
bit no;
unsigned char codetablexiao[16]={0,0,1,2,2,3,4,4,5,6,6,7,8,8,9,9};
void Delay(unsigned int nTimeDelay)
{
unsigned int i;
while (nTimeDelay--)
for (i=0;i<125;i++)
{}
}
void Reset()
{
unsigned int i;
ds18b20=0; //拉低DQ总线开始复位
i=206;
while(i>0)
i--; //保持DQ低大约900uS
ds18b20=1; //释放DQ位
i=8;
while(i>0)
i--;
}
bit WaitPresence()
{
unsigned char i;
bit k;
i=16;
while(i>0)
i--;
if(ds18b20==0)
k=1;
else
k=0;
i=72;
while(i>0)
i--;
return k;
}
bit ReadBit()
{
unsigned int i;
bit dat;
ds18b20=0;
i++;
ds18b20=1;
i+=2;
dat=ds18b20;
i=16;
while(i>0)
i--;
return dat;
}
unsigned char ReadByte()
{
unsigned char i,j,dat;
dat=0;
for(i=1;i<=8;i++)
{
j=ReadBit();
dat=(j<<7) | (dat>>1);
}
return dat;
}
void WriteByte(unsigned char dat)
{
unsigned int i;
unsigned char j;
bit testb;
for(j=1;j<=8;j++)
{
testb=dat & 0x01;
dat=dat>>1;
if(testb)
{
ds18b20=0;
i+=2;
ds18b20=1;
i=8;
while(i>0)
i--;
}
else {
ds18b20=0;
i=8;
while(i>0)
i--;
ds18b20=1;
i+=2;
} }}
bit Convert()
{
Reset();
if(WaitPresence()==1)
{
Delay(1);
WriteByte(0xcc);//skip rom
WriteByte(0x44);//convert
return 1;
}
else
{
return 0;
} }
bit ReadFlash()
{
unsigned char nLsb,nMsb;
Reset();
if(WaitPresence()==1)
{
Delay(1);
WriteByte(0xcc);//skip rom
WriteByte(0xbe);//convert
nLsb=ReadByte();//LSB
nMsb=ReadByte();//MSB
if(no==1) return 1;
temp2=nLsb&0x0f;
temp1=(nLsb>>4)|(nMsb<<4);
return 1;
}
else
{
return 0;
}
}
void LCD1602_busy(void)
{
P0_7=1; //将P0.7置1,为读状态做准备
RS=0; //RS=0、RW=1、E=1时,忙信号输出到DB7,由P0.7读入
RW=1; //RS=0、RW=1、E=1时,忙信号输出到DB7,由P0.7读入
E=1; //RS=0、RW=1、E=1时,忙信号输出到DB7,由P0.7读入
while(P0_7==1); //由P0.7读入1,表示1602液晶忙,需要等待
E=0; //读完以后,恢复E的电平
}
void LCD1602_Write_com(unsigned charcombuf)
{
RS=0; //选择指令寄存器
RW=0; //选择写状态
P0=combuf; //将命令字通过P0口送至DB
E=1; //E高电平将命令字写入1602液晶
E=0; //写完以后,恢复E的电平
}
void LCD1602_Write_com_busy(unsigned charcombuf)
{
LCD1602_busy(); //调用忙检测函数
LCD1602_Write_com(combuf); //调用忙检测函数
}
void LCD1602_Write_data_busy(unsigned chardatabuf)
{
LCD1602_busy(); //调用忙检测函数
RS=1; //选择数据寄存器
RW=0; //选择写状态
P0=databuf; //将命令字通过P0口送至DB
E=1; //E高电平将命令字写入1602液晶
E=0; //写完以后,恢复E的电平
}
void LCD1602_Write_address(unsigned charx,unsigned char y)
{
x&=0x0f; //列地址限制在0-15间
y&=0x01; //行地址限制在0-1间
if(y==0) //如果是第一行
LCD1602_Write_com_busy(x|0x80); //将列地址写入
else //如果是第二行
LCD1602_Write_com_busy((x+0x40)|0x80); //将列地址写入
}
void LCD1602_init(void)
{
Delay(150); //调用延时函数
LCD1602_Write_com(0x38); //8位数据总线,两行显示模式,5*7点阵显示
Delay(50); //调用延时函数
LCD1602_Write_com(0x38); //8位数据总线,两行显示模式,5*7点阵显示
Delay(50); //调用延时函数
LCD1602_Write_com(0x38); //8位数据总线,两行显示模式,5*7点阵显示
LCD1602_Write_com_busy(0x38); //8位数据总线,两行显示模式,5*7点阵显示
LCD1602_Write_com_busy(0x08); //显示功能关,无光标
LCD1602_Write_com_busy(0x01); //清屏
LCD1602_Write_com_busy(0x06); //写入新的数据后,光标右移,显示屏不移动
LCD1602_Write_com_busy(0x0C); //显示功能开,无光标
}
void LCD1602_Disp(unsigned char x,unsignedchar y,unsigned char buf)
{
LCD1602_Write_address(x,y); //先将地址信息写入
LCD1602_Write_data_busy(buf); //再写入要显示的数据
}
void main(void) //主函数,单片机开机后就是从这个函数开始运行
{
unsigned char buf;
LCD1602_init(); //调用1602液晶初始化函数
while(1) //死循环,单片机初始化后,将一直运行这个死循环
{
tc=Convert(); //启动转换
if(tc==1) //转换结束
{
ReadFlash(); //读取温度
if(temp1>80) temp1=0;
if(temp2>15)temp2=0;
buf=temp1/10;
buf+=0x30;
LCD1602_Disp(0,0,buf); //温度整数部分十位
buf=temp1%10;
buf+=0x30;
LCD1602_Disp(1,0,buf); //温度整数部分个位
LCD1602_Disp(2,0,0x2e); //.
buf=tablexiao[temp2&0x0f];
buf+=0x30;
LCD1602_Disp(3,0,buf); //温度小数部分
LCD1602_Disp(4,0,0xdf); //.
LCD1602_Disp(5,0,0x43); //C
}
}}
复制代码
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1