DS18B20
基础知识
代码流程实现
将官方提供例程文件添加到工程中
添加onewire.c文件到keil4里面
一些代码补充知识
代码
#include "reg52.h"
#include "onewire.h"
#include "absacc.h"
unsigned char num[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; //0-9
unsigned char num2[10]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};//0-9
unsigned int temp = 0;
void DelaySMG(unsigned int t)
{
while(t--);
}
void DisPlaySMG_Bit(unsigned pos, unsigned char dat)
{
XBYTE[0xE000] = 0xff;
XBYTE[0xC000] = 0x01 << pos;
XBYTE[0xE000] = dat;
}
void DisPlay_All(unsigned char dat)
{
XBYTE[0xC000] = 0xff;
XBYTE[0xE000] = dat;
}
void DisPlaySMG_Temp()
{
DisPlaySMG_Bit(7, num[(temp % 10)]);
DelaySMG(100);
DisPlaySMG_Bit(6, num2[(temp % 100) / 10]);
DelaySMG(100);
DisPlaySMG_Bit(5, num[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 = (temp << 8) | LSB;
if((temp & 0xf800) == 0x0000)
{
// °ÑСÊýÒÆ×ß
temp >>= 4;
temp = temp*10;
temp = temp + (LSB & 0x0f)*0.625;
}
}
void main()
{
XBYTE[8000] = 0xff;
while(1)
{
Read_DS18B20_temp();
DisPlaySMG_Temp();
}
}