📄 ds18b20.c
字号:
#include <reg52.h>
#include <DS18B20.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
uchar temp_value,current_temp[2],wei;
//uchar code table[]={0x03,0x9F,0x25,0x0D,0x99,0x49,0x41,0x1F,0x01,0x09};
//uchar code ditab[16]=
//{0x00,0x01,0x01,0x02,0x03,0x03,0x04,0x04,0x05,0x06,0x06,0x07,0x08,0x08,0x09,0x09};
//
uchar code table[12]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0xbf};
//uchar code scan_con[4]={0xf7,0xfb,0xfd,0xfe}; //列扫描控制字
uchar data temp_data[2]={0x00,0x00}; //读出温度暂放
uchar data display[5]={0x00,0x00,0x00,0x00,0x00}; //显示单元数据,共4个数据和一个运算暂用
void delay(uint n) //延时子程序
{
uint j,k;
for(k=0;k<n;k++)
for(j=0;j<500;j++);
}
void display1(uchar bai,uchar shi,uchar ge) //显示子程序
{
P0=table[bai];
wei=0xfd;
P2=wei;
delay(1);
P2=0xff;
P0=table[shi];
wei=0xfb;
P2=wei;
delay(1);
P2=0xff;
P0=table[ge];
wei=0xf7;
P2=wei;
delay(1);
P2=0xff;
}sbit DQ18B20 = P2^7;
void Delayus(unsigned int t)
{
for (;t>0;t--);
}
void Write_18b20(uchar val)
{
uchar i;
for(i=8;i>0;i--)
{
DQ18B20=1;_nop_();_nop_(); //从高拉倒低
DQ18B20=0;_nop_();_nop_();_nop_();_nop_(); //5 us
DQ18B20=val&0x01; //最低位移出
Delayus(5); //55 us 15us~120us
val=val/2; //右移1位
}
DQ18B20=1;
Delayus(1);
}
/*void Write_18b20(unsigned char ddata) //写18B20数据
{
unsigned char i;
for(i=0;i<8;i++)
{
DQ18B20=1;_nop_();_nop_(); //从高拉倒低
DQ18B20 = 0;
_nop_();_nop_();_nop_();_nop_(); //5 us
DQ18B20 = ddata&0x01;
ddata=ddata >> 1;
Delayus(6); // Delay68us
DQ18B20 = 1;
}
DQ18B20 = 1;
Delayus(1); // Delay 18us
}
/*Write_18b20(unsigned char WriteData)
{
unsigned char i;
unsigned char tmpData;
for(i=0;i<8;i++)
{
tmpData=WriteData&0x01;
WriteData>>=1;
if(tmpData)
{
DQ18B20=0;
DQ18B20=1;
Delayus(4);
}
else
{
DQ18B20=0;
Delayus(4);
DQ18B20=1;
}
}
}
unsigned char Read_18b20(void) //读18B20
{
unsigned char i,temp;
for(i=0;i<8;i++)
{
DQ18B20 =1;
temp=temp>>1;
DQ18B20 = 0;
Delayus(1);
DQ18B20 =1;
Delayus(1);
if(DQ18B20 ==0)
{
temp=temp&0x7f;
}
else
{
temp=temp|0x80;
}
Delayus(5); //Delay 58us
}
DQ18B20=1;
return temp;
}
/*
//读一个字节
unsigned char Read_18b20()
{
unsigned char i;
unsigned char ReadData=0;
for(i=0;i<8;i++)
{
DQ18B20=0;
ReadData>>=1;
DQ18B20=1;
if(DQ18B20)
ReadData|=0x80;
Delayus(4);
}
return ReadData;
} */
unsigned char Read_18b20(void)
{
unsigned char i;
unsigned char value=0;
for(i=8;i>0;i--)
{
DQ18B20=1;_nop_();_nop_();
value>>=1;
DQ18B20=0;_nop_();_nop_();_nop_();_nop_(); //4 us
DQ18B20=1;_nop_();_nop_();_nop_();_nop_(); //4 us
if(DQ18B20)value|=0x80;
Delayus(6); //66 us
}
DQ18B20=1;
return(value);
}
/*void Init_18b20(void) //18b20的初始化
{
DQ18B20 =0;
Delayus(50); //Delay 508us
DQ18B20 =1;
Delayus(5); //Delay 108us
while(DQ18B20);
Delayus(10); //Delay 108us
DQ18B20 =1;
}*/
void Init_18b20()
{
DQ18B20=0;
Delayus(60); //下拉500us
DQ18B20=1; //释放总线
Delayus(6);
while(DQ18B20); //等待应答信号
while(~DQ18B20); //等待释放总线
}
/*Init_18b20(void)
{
char presence=1;
while(presence)
{
while(presence)
{
DQ18B20=1;_nop_();_nop_();//从高拉倒低
DQ18B20=0;
Delayus(50); //550 us
DQ18B20=1;
Delayus(6); //66 us
presence=DQ18B20; //presence=0 复位成功,继续下一步
}
Delayus(45); //延时500 us
presence=~DQ18B20;
}
DQ18B20=1; //拉高电平
} */
unsigned char Get_temp_value() //获取温度值
{
Init_18b20();
Delayus(200);
Write_18b20(0xcc); //跳过ROM地址
Write_18b20(0x44); //温度变换
Init_18b20();
Delayus(1);
Write_18b20(0xcc);
Write_18b20(0xbe);//读温度
databuff[0]=Read_18b20();
databuff[1]=Read_18b20();
temp=databuff[1];
temp<<=8;
temp=temp|databuff[0]; // 两字节合成一个整型变量。
//temp_value_integer=(((databuff[0]<<4)&0xf0)|((databuff[1]>>4)&0x0f));
// temp_value_decimal=(databuff[0]&0x000f)*625;
return temp;
}
void main()
{
Init_18b20( );
Write_18b20(0xcc); //跳过ROM地址
Write_18b20(0x44);
while(1)
{
//Get_temp_value();//获取温度值
display[3]=Get_temp_value()%100; // 取后两位数据暂存
display[2]=Get_temp_value()/100; // 取百位数据暂存
display[1]=display[2]/10; // 取十位数据暂存
display[0]=display[2]%10;
display1(display[2],display[1],display[0]) ;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -