程序的头文件
#include
#include
#include
sbit CS=P1^0;
sbit SCK=P1^2;
sbit SID=P1^1;
sbit Key=P1^3;
sbit DQ = P2 ^ 7; //定义端口DQ
unsigned char code AC_TABLE[]={
0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,
0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,
0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f,
0x98,0x99,0x9a,0x9b,0x9c,0x9d,0x9e,0x9f,
};
void delay(unsigned int x)
{
while(x--);
}
void SendByte(unsigned char Dbyte)
{
unsigned char i;
for(i=0;i<8;i++)
{
SCK = 0;
Dbyte=Dbyte<<1;
SID = CY;
SCK = 1;
SCK = 0;
}
}
unsigned char ReceiveByte(void)
{
unsigned char i,temp1,temp2;
temp1=temp2=0;
for(i=0;i<8;i++)
{
temp1=temp1<<1;
SCK = 0;
SCK = 1;
SCK = 0;
if(SID) temp1++;
}
for(i=0;i<8;i++)
{
temp2=temp2<<1;
SCK = 0;
SCK = 1;
SCK = 0;
if(SID) temp2++;
}
return ((0xf0&temp1)+(0x0f&temp2));
}
void CheckBusy( void )
{
do SendByte(0xfc); //11111,RW(1),RS(0),0
while(0x80&ReceiveByte());
}
void WriteCommand( unsigned char Cbyte )
{
CS = 1;
CheckBusy();
SendByte(0xf8); //11111,RW(0),RS(0),0
SendByte(0xf0&Cbyte);
SendByte(0xf0&Cbyte<<4);
CS = 0;
}
void WriteData( unsigned char Dbyte )
{
CS = 1;
CheckBusy();
SendByte(0xfa); //11111,RW(0),RS(1),0
SendByte(0xf0&Dbyte);
SendByte(0xf0&Dbyte<<4);
CS = 0;
}
unsigned char ReadData( void )
{
CheckBusy();
SendByte(0xfe); //11111,RW(1),RS(1),0
return ReceiveByte();
}
void LcmInit( void )
{
WriteCommand(0x30);
WriteCommand(0x03);
WriteCommand(0x0C);
WriteCommand(0x01);
WriteCommand(0x06);
}
void LcmClearTXT( void )
{
unsigned char i;
WriteCommand(0x30);
WriteCommand(0x80);
for(i=0;i<64;i++)
WriteData(0x20);
}
void LcmClearBMP( void )
{
unsigned char i,j;
WriteCommand(0x34);
WriteCommand(0x36);
for(i=0;i<32;i++)
{
WriteCommand(0x80|i);
WriteCommand(0x80);
for(j=0;j<32;j++)
WriteData(0);
}
}
void PutStr(unsigned char row,unsigned char col,unsigned char *puts)
{
WriteCommand(0x30);
WriteCommand(AC_TABLE[8*row+col]);
while(*puts != '/0')
{
if(col==8)
{
col=0;
row++;
}
if(row==4) row=0;
WriteCommand(AC_TABLE[8*row+col]);
WriteData(*puts);
puts++;
WriteData(*puts);
puts++;
col++;
}
}
//////////////////////////////////////
///////////////////////////
/////////////////////////////
Init_DS18B20(void)//初始化ds1820
{
unsigned char x = 0;
DQ = 1; //DQ复位
delay(8); //稍做延时
DQ = 0; //单片机将DQ拉低
delay(80); //精确延时 大于 480us
//Delay(400); //精确延时 大于 480us
DQ = 1; //拉高总线
delay(14);
x = DQ; //稍做延时后 如果x=0则初始化成功 x=1则初始化失败
delay(20);
}
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(15);
}
return (dat);
}
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;
}
}
Read_Disp_Temperature(void)//读取并显示温度
{
unsigned int a = 0, b = 0, c = 0, t = 0;
float tt = 0;
Init_DS18B20();
WriteOneChar(0xCC); // 跳过读序号列号的操作
WriteOneChar(0x44); // 启动温度转换
Init_DS18B20();
WriteOneChar(0xCC); //跳过读序号列号的操作
WriteOneChar(0xBE);
a = ReadOneChar(); //读取温度寄存器
b = ReadOneChar();
t = b;
t <<= 8;
t = t | a;
tt = t * 0.0625;
t = tt * 10 + 0.5; //放大10倍输出并四舍五入
a = t / 100; //十位
b = t / 10 - a * 10; //个位
c = t - a * 100 - b * 10; //小数位
// LcmInit();
// LcmClearTXT();
//LcmClearBMP();
WriteCommand(0x30);
PutStr(0,0,"当前温度为");
delay(100);
WriteCommand(AC_TABLE[8*1+0]);
WriteData(a+0x30);
WriteCommand(AC_TABLE[8*1+1]);
WriteData(b+0x30);
PutStr(1,2,". ");
WriteCommand(AC_TABLE[8*1+3]);
WriteData(c+0x30);
PutStr(1,4,"度");
delay(1000);
}
『本文转载自网络,版权归原作者所有,如有侵权请联系删除』