《数字式温度测控芯片DS1620的应用》
DS1620的几个主要子函数如下:
#include "AT89x51.h"
#include "intrins.h"
////////////////////////////////////////
#define DQ P1_1
#define CLK_CONV P1_2
#define RST P1_3
////采用11.0592MHz晶体
//////////////////////////////////////
//写字节子程序
void write_byte(unsigned char val)
{
char i;
unsigned char b;
b=1;
for(i=0;i<8;i++)
{
CLK_CONV=0;
DQ=(val&b);
CLK_CONV=1;
b<<=1;
}
}
//////////////////////////////////////
//读字节子程序
unsigned char read_byte(void)
{
unsigned char i;
unsigned char value,b;
value=0;
b=1;
for(i=0;i<8;i++)
{
DQ=1;
CLK_CONV=0;
if (DQ)
value|=b;
CLK_CONV=1;
b<<=1;
}
return(value);
}
//DS1620 开始转换
unsigned char DS1620startConv(void)
{
RST=1;
write_byte(0xEE);
RST=0;
return 0x00;
}
//DS1620 读配置 返回值为配置寄存器内容
unsigned char DS1620ReadConf(void)
{
unsigned char tmp;
RST=1;
write_byte(0xAC);
tmp=read_byte();
RST=0;
return tmp;
}
//DS1620 写配置,入回参数为配置寄存器新配置内容
unsigned char DS1620SetConf(unsigned char val)
{
unsigned char tmp;
RST=1;
write_byte(0x0C);
write_byte(val);
RST=0;
return tmp;
}
//DS1620 读温度转换数据,在返回值的低9位
unsigned int DS1620read(void)
{
unsigned char hbyte,lbyte;
unsigned int temp;
RST=1;
write_byte(0xAA);
lbyte=read_byte();
hbyte=read_byte();
RST=0;
temp=hbyte;
temp<<=8;
temp|=lbyte;
return temp;
} |
|