更新时间:2016-11-13 17:31:50人气:968
#define uchar unsigned char /*宏定义 字符型数据 整型数据 */
#define uint unsigned int
uint DD;
sbit DQ="P2^2; " //定义DS18B20总线I/O
unsigned int temputer;
char Co
0x7d, 0x07, 0x7f, 0x6f};
uchar co
/*****延时子程序*****/
void Delay_DS18B20(int num)
{
while(num--) ;
}
void delay(unsigned int time) //延时子程序 入口参数 time
{
unsigned int n;
n="0; while(n {n++;}
return;
}
/*****初始化DS18B20*****/
void Init_DS18B20(void)
{
unsigned char x="0; DQ = 1; //DQ复位
Delay_DS18B20(2); //稍做延时
DQ = 0; //单片机将DQ拉低
Delay_DS18B20(80); //精确延时,大于480us
DQ = 1; //拉高总线
Delay_DS18B20(14);
x = DQ; //稍做延时后,如果x="0则初始化成功,x=1则初始化失败 Delay_DS18B20(20);
}
/*****读一个字节*****/
unsigned char ReadOneChar(void)
{
unsigned char i="0; unsigned char dat = 0;
for (i="8;i">0;i--)
{
DQ = 0; // 给脉冲信号
dat>>=1;
DQ = 1; // 给脉冲信号
if(DQ)
dat|=0x80;
Delay_DS18B20(8);
}
return(dat);
}
/*****写一个字节*****/
void WriteOneChar(unsigned char dat)
{
unsigned char i="0; for (i="8;" i>0; i--)
{
DQ = 0;
DQ = dat&0x01;
Delay_DS18B20(10);
DQ = 1;
dat>>=1;
}
}
/*****读取温度*****/
unsigned int ReadTemperature(void)
{
unsigned char a="0; unsigned char b="0; unsigned int t="0; float tt="0; Init_DS18B20();
// Init_DS18B20();
WriteOneChar(0xCC); //跳过读序号列号的操作
WriteOneChar(0x44); //启动温度转换
Delay_DS18B20(20);
Init_DS18B20();
WriteOneChar(0xCC); //跳过读序号列号的操作
WriteOneChar(0xBE); //读取温度寄存器
a="ReadOneChar(); " //读低8位
b="ReadOneChar(); " //读高8位
t="b; t<<=8;
t="t|a; tt="t*0.0625; t=" tt*10+0.5; " //放大10倍输出并四舍五入
return(t);
}
void main()
{
while(1)
{
temputer="ReadTemperature(); //temputer" 是3位数的 是温度扩大了10倍
P0 = Co
P1 = C[1];
delay(100);
DD="temputer/100; temputer="temputer-DD*100;
P0 = Co
P1 = C[2];
delay(100);
P0 = 0x80;
P1 = C[3];
delay(100);
P0 = Co
P1 = C[4];
delay(10);
PREVIOUS优质NDA系列称重传感器技术参数
优质NDA系列称重传感器技术参数适用于台秤 包装秤 料斗秤量程60kg; 100kg; 150kg200kg; 300kg; 500kg800kg; 1.2t■材质:铝合金■额定输出:3mv/v±0.25%■综合误...
球阀插入式电磁流量计的工作原理和产品安装要求:工作原理:球阀插入式电磁流量计根据法拉第电磁感应原理,在与测量管轴线和磁力线相垂直的管壁上安装了一对检测电极,当导电液体沿测量管轴线运动时,导电液体切割磁力线产生感应电势,此感应电势由两个检测电极检出,数值大小与...