相关代码:
主函数模块:
#include “STC15F2K60S2.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 char temp;
void Display_SMG_Temp();
void Delay(unsigned char t)
{
while(t–);
}
void Delay1(unsigned char t)
{
while(t–)
{
Display_SMG_Temp();
}
}
void Close_All(unsigned char dat)
{
XBYTE[0xc000] = 0xf8;
XBYTE[0xe000] = dat;
}
void Display_SMG_Bit(unsigned char pos ,unsigned char dat)
{
XBYTE[0xc000] = 0x01 << pos;
XBYTE[0xe000] = dat;
}
void Read_DS18B20_Temp()
{
unsigned char LSB,MSB;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
Delay1(1000);
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
LSB = Read_DS18B20();
MSB = Read_DS18B20();
temp = MSB << 8 | LSB;
temp = temp >> 4;
// if((temp & 0xf800) == 0x0000)
// {
// temp = temp >> 4;
// temp *= 10;
// temp = temp + (LSB & 0x0f) * 0.625;
// }
}
void Display_SMG_Temp()
{
// Close_All(0xff);
Display_SMG_Bit(7,SMGNoDot_CA[temp % 10]);
Delay(100);
Display_SMG_Bit(6,SMGDot_CA[temp / 10 % 10]);
Delay(100);
Display_SMG_Bit(5,SMGNoDot_CA[temp / 100]);
Delay(100);
Display_SMG_Bit(4,0xff);
Delay(100);
Display_SMG_Bit(3,0xff);
Delay(10);
// Display_SMG_Bit(2,0xff);
// Delay(50);
// Display_SMG_Bit(1,0xff);
// Delay(50);
// Display_SMG_Bit(0,0xff);
// Delay(50);
}
void main()
{
while(1)
{
Read_DS18B20_Temp();
Display_SMG_Temp();
}
}
DS18B20驱动.c文件:
/*
程序说明: 单总线驱动程序
软件环境: Keil uVision 4.10
硬件环境: CT107单片机综合实训平台(外部晶振12MHz) STC89C52RC单片机
日 期: 2011-8-9
*/
#include “reg52.h”
sbit DQ = P1^4; //单总线接口
//单总线延时函数
void Delay_OneWire(unsigned int t) //STC89C52RC
{
while(t–);
}
//通过单总线向DS18B20写一个字节
void Write_DS18B20(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
DQ = 0;
DQ = dat&0x01;
Delay_OneWire(60);
DQ = 1;
dat >>= 1;
}
Delay_OneWire(60);
}
//从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(60);
}
return dat;
}
//DS18B20设备初始化
bit init_ds18b20(void)
{
bit initflag = 0;
DQ = 1;
Delay_OneWire(144);
DQ = 0;
Delay_OneWire(960 );
DQ = 1;
Delay_OneWire(120);
initflag = DQ;
Delay_OneWire(60);
return initflag;
}
DS18B20驱动.h文件:
#ifndef __ONEWIRE_H
#define __ONEWIRE_H
bit init_ds18b20(void); //; ;
unsigned char Read_DS18B20(void);
void Write_DS18B20(unsigned char dat);
#endif