流程:①:DS18B20复位。
②:写入字节0xcc,跳过ROM指令。
③:写入字节0x44,开始温度转换。
④:延时700~900ms
⑤:DS18B20复位。
⑥:写入字节0xcc,跳过ROM指令。
⑦:写入字节0xbe,读取高速暂存器。
⑧:读取暂存器的第0字节,即温度数据的LSB。
⑨:读取暂存器的第1字节,即温度数据的MSB。
⑩:DS18B20复位,表示读取数据结束。
11:将LSB和MSB整合成为一个16位数据。
12:判断读取结果的符号,进行正负温度的数据处理。
void Read_DS18B20_temp()
{
unsigned char LSB,MSB;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
Delay(1000);
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
LSB=Read_DS18B20();
MSB=Read_DS18B20();
temp=MSB;
temp<<8;
temp=temp|LSB;
if((temp&0xf800)==0x0000)
{
temp>>=4;
temp=temp*10;
temp=temp+(LSB &0x0f)*0.0625;
}
}
如果最高5位都为0,说明这个温度是个整数。如果都是1,说明温度是个负数。
DS18B20的分辨率为0.0625.读出的数据是正数时,将LSB和MSB整合的16位整数,直接乘以0.0625即可。读出的数据为负数时,则需要将LSB和MSB整合的16位整数取反加1后,再乘以0.0625。
下面是完整代码:
DS18B20.c:
#include "reg52.h"
#include "absacc.h"
#include "onewire.h"
unsigned char SMGNoDot_CA[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
unsigned char SMGDot_CA[10]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};
unsigned int temp=0;
void DisPlay_All(unsigned char dat)
{
P0=0xff;
P2=P2&0x1f|0xc0;
P2=P2&0x1f;
P0=dat;
P2=P2&0x1f|0xe0;
P2=P2&0x1f;
}
void DelaySMG(unsigned int t)
{
while(t--);
}
void DisPlaySMG_Bit(unsigned char pos,unsigned char dat)
{
P0=0xff;
P2=P2&0x1f|0xe0;
P2=P2&0x1f;
P0=0x01<<pos;
P2=P2&0x1f|0xc0;
P2=P2&0x1f;
P0=dat;
P2=P2&0x1f|0xe0;
P2=P2&0x1f;
}
void DisPlaySMG_Temp()
{
DisPlaySMG_Bit(7,SMGNoDot_CA[temp%10]);
DelaySMG(100);
DisPlaySMG_Bit(6,SMGDot_CA[(temp%100)/10]);
DelaySMG(100);
DisPlaySMG_Bit(5,SMGNoDot_CA[temp/100]);
DelaySMG(100);
DisPlaySMG_Bit(4,0xff);
DelaySMG(100);
DisPlaySMG_Bit(3,0xff);
DelaySMG(100);
DisPlaySMG_Bit(2,0xff);
DelaySMG(100);
DisPlaySMG_Bit(1,0xff);
DelaySMG(100);
DisPlaySMG_Bit(0,0xff);
DelaySMG(100);
DisPlay_All(0xff);
}
void Delay(unsigned int t)
{
while(t--);
{
DisPlaySMG_Temp();
}
}
void Read_DS18B20_temp()
{
unsigned char LSB,MSB;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
Delay(1000);
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
LSB=Read_DS18B20();
MSB=Read_DS18B20();
temp=MSB;
temp<<8;
temp=temp|LSB;
if((temp&0xf800)==0x0000)
{
temp>>=4;
temp=temp*10;
temp=temp+(LSB &0x0f)*0.0625;
}
}
void main()
{
P0=0;
P2=P2&0x1f|0xa0;
P2=P2&0x1f;
P0=0xff;
P2=P2&0x1f|0x80;
P2=P2&0x1f;
while(1)
{
Read_DS18B20_temp();
DisPlaySMG_Temp();
}
}
?onewire.h:
#include "reg52.h"
sbit DQ = P1^4;
//μ¥×ü???óê±oˉêy
#ifndef STC12
void Delay_OneWire(unsigned int t) //STC89C52RC
{
while(t--);
}
#else
void Delay_OneWire(unsigned int t) //STC12C5260S2
{
unsigned char i;
while(t--){
for(i=0;i<12;i++);
}
}
#endif
//í¨1yμ¥×ü???òDS18B20D′ò???×??ú
void Write_DS18B20(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
DQ = 0;
DQ = dat&0x01;
Delay_OneWire(50);
DQ = 1;
dat >>= 1;
}
Delay_OneWire(50);
}
//′óDS18B20?áè?ò???×??ú
unsigned char Read_DS18B20(void)
{
unsigned char i;
unsigned char dat;
for(i=0;i<8;i++)
{
DQ = 0;
dat >>= 1;
DQ = 1;
if(DQ)
{
dat |= 0x80;
}
Delay_OneWire(50);
}
return dat;
}
//DS18B203?ê??ˉ
bit init_ds18b20(void)
{
bit initflag = 0;
DQ = 1;
Delay_OneWire(120);
DQ = 0;
Delay_OneWire(800); // ?óê±′óóú480us
DQ = 1;
Delay_OneWire(100); // 14
initflag = DQ; // initflagμèóú13?ê??ˉ꧰ü
Delay_OneWire(50);
return initflag;
}
//DS18B20???è2é?ˉ3ìDò£o??êy
// unsigned char rd_temperature(void)
// {
// unsigned char low,high;
// char temp;
//
// init_ds18b20();
// Write_DS18B20(0xCC);
// Write_DS18B20(0x44); //???ˉ???è×a??
// Delay_OneWire(200);
// init_ds18b20();
// Write_DS18B20(0xCC);
// Write_DS18B20(0xBE); //?áè???′??÷
// low = Read_DS18B20(); //μí×??ú
// high = Read_DS18B20(); //??×??ú
// /** ???è?a1é?ê??è */
// temp = high<<4;
// temp |= (low>>4);
//
// return temp;
// }
//DS18B20???è2é?ˉ3ìDò£o??μ?êy
float rd_temperature_f(void)
{
unsigned int temp;
float temperature;
unsigned char low,high;
init_ds18b20();
Write_DS18B20(0xCC);
Write_DS18B20(0x44); //???ˉ???è×a??
Delay_OneWire(200);
init_ds18b20();
Write_DS18B20(0xCC);
Write_DS18B20(0xBE); //?áè???′??÷
low = Read_DS18B20(); //μí×??ú
high = Read_DS18B20(); //??×??ú
/** ???è?a0.0625é?ê??è */
temp = (high&0x0f);
temp <<= 8;
temp |= low;
temperature = temp*0.0625;
return temperature;
}
|