由于DS18B20对时间精度要求很高,所以在写读取温度之前一定要改时序
1. 单总线延时函数
//单总线延时函数
void Delay_OneWire(unsigned int t)
{
t*=12; //t改成t*12
while(t--);
}
2. 读取温度(整数)
unsigned char read_temperature1()
{
unsigned char temp1;
unsigned char low,high;
Init_DS18B20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
Delay_OneWire(200);
Init_DS18B20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
Delay_OneWire(200);
low = Read_DS18B20(); //先读低,再读高
high = Read_DS18B20();
temp1=high<<4;
temp1|=(low&0xf0)>>4;
return temp1;
}
3. 读取温度(带小数)
float read_temperature2()
{
float temp2; //**注意带小数,使用float型**
unsigned int temp; //16位
unsigned char low,high;
Init_DS18B20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
Delay_OneWire(200);
Init_DS18B20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
Delay_OneWire(200);
low = Read_DS18B20();
high = Read_DS18B20();
temp = high&0x0f;
temp = temp<<8|low;
temp2 = temp*0.0625; //最小精度0.0625
return temp2;
}
4. 主函数里的变换
uchar temp1;
uint temp2;
float temp_value;
bit temp_flag;
void main()
{
CloseFucker();
Timer0Init();
while(1)
{
if(temp_flag)
{
temp1 = read_temperature1(); //读取整数温度值
temp_value = 10*read_temperature2(); //float型读取带小数温度值,乘10把小数提取
temp2 = (uint)temp_value; //化为整型可以取余
temp_flag=0;
}
ShowTemp();
}
}
代码
main.c
******************************************************************************
* 文件名:读取温度(带整数和小数)
* 描 述:
* 作 者:思索与猫
* 日 期: 19/3/8
* 备 注:左边:整数 右边:带小数
*
******************************************************************************
#include<stc15f2k60s2.h>
#include<onewire.h>
#define uchar unsigned char
#define uint unsigned int
uchar code duan[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x00,0x40,0x039,
0xBF,0x86,0xDB,0xCF,0xE6,0xED,0xFD,0x87,0xFF,0xEF}; //带小数0~9
uchar dispbuff[8];
uchar temp1; //注意数据类型
uint temp2;
float temp_value;
bit temp_flag;
//function
void CloseFucker();
void Timer0Init();
void Display();
void ShowTemp();
void main()
{
CloseFucker();
Timer0Init();
while(1)
{
if(temp_flag)
{
temp1 = read_temperature1();
temp_value = 10*read_temperature2();
temp2 = (uint)temp_value;
temp_flag=0;
}
ShowTemp();
}
}
void ShowTemp()
{
dispbuff[0] = temp1/10;
dispbuff[1] = temp1%10;
dispbuff[2] = 12;
dispbuff[3] = 10;
dispbuff[4] = temp2/100;
dispbuff[5] = temp2/10%10+13;
dispbuff[6] = temp2%10;
dispbuff[7] = 12;
}
void Display()
{
static uchar index = 0;
P2 = (P2&0x1f)|0xe0;
P0 = 0xff;
P2 = P2&0x1f;
P2 = (P2&0x1f)|0xc0;
P0 = 1<<index;
P2 = P2&0x1f;
P2 = (P2&0x1f)|0xe0;
P0 = ~duan[dispbuff[index]];
P2 = P2&0x1f;
index++;
index &= 0x07;
}
void T0_time() interrupt 1
{
static uint temp_count = 0;
TL0 = 0x40;
TH0 = 0xA2;
Display();
if(++temp_count == 500)
{
temp_count=0;
temp_flag=1;
}
}
void Timer0Init() //12Mhz 2ms
{
AUXR |= 0x80;
TMOD &= 0xF0;
TL0 = 0x40;
TH0 = 0xA2;
TF0 = 0;
TR0 = 1;
ET0=1;
EA=1;
}
void CloseFucker()
{
P2 = (P2&0x1f)|0xa0;
P0 = 0xa0;
P2 = P2&0x1f;
}
onewire.h
#ifndef _ONEWIRE_H
#define _ONEWIRE_H
#include "stc15f2k60s2.h"
#define OW_SKIP_ROM 0xcc
#define DS18B20_CONVERT 0x44
#define DS18B20_READ 0xbe
//pin
sbit DQ = P1^4;
//function
void Delay_OneWire(unsigned int t);
void Write_DS18B20(unsigned char dat);
bit Init_DS18B20(void);
unsigned char Read_DS18B20(void);
unsigned char read_temperature1();
float read_temperature2();
#endif
onewire.c
#include "onewire.h"
void Delay_OneWire(unsigned int t)
{
t*=12;
while(t--);
}
bit Init_DS18B20(void)
{
bit initflag = 0;
DQ = 1;
Delay_OneWire(12);
DQ = 0;
Delay_OneWire(80);
DQ = 1;
Delay_OneWire(10);
initflag = DQ;
Delay_OneWire(5);
return initflag;
}
void Write_DS18B20(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
DQ = 0;
DQ = dat&0x01;
Delay_OneWire(5);
DQ = 1;
dat >>= 1;
}
Delay_OneWire(5);
}
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(5);
}
return dat;
}
unsigned char read_temperature1() //读取温度(整数)
{
unsigned char temp1;
unsigned char low,high;
Init_DS18B20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
Delay_OneWire(200);
Init_DS18B20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
Delay_OneWire(200);
low = Read_DS18B20();
high = Read_DS18B20();
temp1=high<<4;
temp1|=(low&0xf0)>>4;
return temp1;
}
float read_temperature2() //读取温度(小数)
{
float temp2;
unsigned int temp;
unsigned char low,high;
Init_DS18B20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
Delay_OneWire(200);
Init_DS18B20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
Delay_OneWire(200);
low = Read_DS18B20();
high = Read_DS18B20();
temp = high&0x0f;
temp = temp<<8|low;
temp2 = temp*0.0625;
return temp2;
}