📄 ds1820_temp.c
字号:
//---------------------------------------
// Interfacing DS1820 and LCD
// To AT89S51
// KEIL C51 v7.5
// www.HBeonLabs.com
//---------------------------------------
#include<reg51.h>
sbit RS=P3^7; //lcd instruction/data
sbit EN=P3^6; // lcd enable
sbit DQ=P3^5; // connect with DS1820 Data pin
sbit fan=P3^0;
sbit heater=P3^1;
void DelayMs(unsigned int count);
void ReadTemp(unsigned char * buff);
void ConvertAndDisplay(unsigned char value1,value2);
void display(unsigned char s,t,u,v);
void lcdData(unsigned char l);
void lcdcmd(unsigned char k);
void lcdinit(void);
void lcdhdr(void);
unsigned char MyTemp[9];
//---------------------------------------
// Main program
//---------------------------------------
void main(void)
{
unsigned char tp,tpd;
lcdinit(); // Initialize lcd
lcdhdr(); // call lcd header
fan=0;
heater=0;
while(1)
{
ReadTemp(&MyTemp[0]);
tp = MyTemp[0] >> 1;
tpd = ((MyTemp[0])& 0x01) ? 5:0;
ConvertAndDisplay(tp,tpd);
if(MyTemp[0]>0x46)
{
fan=1;
heater=0;
}
else if(MyTemp[0]<0x3C)
{
fan=0;
heater=1;
}
else
{
fan=0;
heater=0;
}
}
}
//---------------------------------------
// Lcd initialization subroutine
//---------------------------------------
void lcdinit(void)
{
lcdcmd(0x38);
DelayMs(20);
lcdcmd(0x0E);
DelayMs(20);
lcdcmd(0x01);
DelayMs(20);
lcdcmd(0x06);
DelayMs(20);
lcdcmd(0x80);
DelayMs(20);
}
//---------------------------------------
// Lcd header
//---------------------------------------
void lcdhdr(void)
{
unsigned char i=0,w[]="TEMP= CENT";
while(w[i]!='\0')
{
lcdData(w[i]);
i++;
DelayMs(50);
}
}
//---------------------------------------
// Lcd display
//---------------------------------------
void display(unsigned char s,t,u,v)
{
s=s+0x30; //convert each digit to equivalent ASCII value
t=t+0x30;
u=u+0x30;
v=v+0x30;
lcdcmd(0x85); //Move the cursor to position 5 on LCD
DelayMs(50);
lcdData(u); //Display the digits one by one on LCD
DelayMs(50);
lcdData(t);
DelayMs(50);
lcdData(s);
DelayMs(50);
lcdData('.'); //Display decimal point on LCD
DelayMs(50);
lcdData(v);
DelayMs(50);
}
//---------------------------------------
// Lcd data display
//---------------------------------------
void lcdData(unsigned char l)
{
P2=l;
RS=1;
EN=1;
DelayMs(1);
EN=0;
return;
}
//---------------------------------------
// Lcd command
//---------------------------------------
void lcdcmd(unsigned char k)
{
P2=k;
RS=0;
EN=1;
DelayMs(1);
EN=0;
return;
}
//---------------------------------------
// Conver and Display the data on LCD
//---------------------------------------
void ConvertAndDisplay(unsigned char value1, value2)
{
unsigned int i,a=0;
unsigned char d1,d2,d3,d4;
for(i=0;i<value1;i++)
a=a+1;
d1=a%10; //digits before desible point
a=a/10;
d2=a%10;
a=a/10;
d3=a%10;
d4=value2;
display(d1,d2,d3,d4);
}
//---------------------------------------
// Delay mS function
//---------------------------------------
void DelayMs(unsigned int count)
{ // mSec Delay 11.0592 Mhz
unsigned int i; // Keil v7.5a
while(count) {
i = 115;
while(i>0) i--;
count--;
}
}
//----------------------------------------
// DELAY at 11.0592MHz crystal.
// Calling the routine takes about 22us, and then
// each count takes another 17us.
// test with KEIL C51 V7.5
//----------------------------------------
void DelayUs(int us)
{
int i;
for (i=0; i<us; i++);
}
//----------------------------------------
// Reset DS1820
//----------------------------------------
bit ResetDS1820(void)
{
bit presence;
DQ=0; //pull DQ line low
DelayUs(29); // leave it low for about 490us
DQ=1; // allow line to return high
DelayUs(3); // wait for presence 55 uS
presence = DQ; // get presence signal
DelayUs(25); // wait for end of timeslot 316 uS
return(presence); // presence signal returned
} // 0=presence, 1 = no part
//-----------------------------------------
// Read one bit from DS1820
//-----------------------------------------
bit ReadBit(void)
{
unsigned char i=0;
DQ=0; // pull DQ low to start timeslot
DQ=1;
for (i=0; i<3; i++); // delay 17 us from start of timeslot
return(DQ); // return value of DQ line
}
//-----------------------------------------
// Write one bit to DS1820
//-----------------------------------------
void WriteBit(bit Dbit)
{
unsigned char i=0;
DQ=0;
DQ=Dbit ? 1:0;
DelayUs(5); // delay about 39 uS
DQ=1;
}
//-----------------------------------------
// Read 1 byte from DS1820
//-----------------------------------------
unsigned char ReadByte(void)
{
unsigned char i;
unsigned char Din = 0;
for (i=0;i<8;i++)
{
Din|=ReadBit()? 0x01<<i:Din;
DelayUs(6);
}
return(Din);
}
//-----------------------------------------
// Write 1 byte
//-----------------------------------------
void WriteByte(unsigned char Dout)
{
unsigned char i;
for (i=0; i<8; i++) // writes byte, one bit at a time
{
WriteBit((bit)(Dout & 0x1)); // write bit in temp into
Dout = Dout >> 1;
}
DelayUs(5);
}
//-----------------------------------------
// Read temperature
//-----------------------------------------
void ReadTemp(unsigned char * buff)
{
unsigned char n;
EA=0; // disable all interrupt
ResetDS1820();
WriteByte(0xcc); // skip ROM
WriteByte(0x44); // perform temperature conversion
while (ReadByte()==0xff); // wait for conversion complete
ResetDS1820();
WriteByte(0xcc); // skip ROM
WriteByte(0xbe); // read the result
for (n=0; n<9; n++) // read 9 bytes but, use only one byte
{
buff[n]=ReadByte(); // read DS1820
}
EA=1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -