新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設計應用 > 單片機中使用DS18B20溫度傳感器C語言程序(參考5)

單片機中使用DS18B20溫度傳感器C語言程序(參考5)

作者: 時間:2016-11-18 來源:網絡 收藏
#include

#define uchar unsigned char
#define uint unsigned int

本文引用地址:http://butianyuan.cn/article/201611/315671.htm

sbit DQ=P2^7; //define interface 定義接口

uint temp; // variable of temperature 定義一個變量

uchar flag1; // sign of the result positive or negative 定
//義一個標志,標志溫度是否還是正

sbit P2_0=P2^0; //數碼管位選
sbit P2_1=P2^1;
sbit P2_2=P2^2;

unsigned char code table[]={0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82, 0xf8,0x80,0x90}; //數字編碼

unsigned char code table1[]={0x40,0x79,0x24,0x30,
0x19,0x12,0x02, 0x78,0x00,0x10};//帶小數點的編碼

void delay(uint i) //delay 延時子程序
{
while(i--);
}


/*******************************************************************/
/* 初始化ds18b2子函數* */
/*******************************************************************/
Init_DS18B20(void)
{
unsigned char x=0;
DQ = 1; //DQ復位
delay(8); //稍做延時
DQ = 0; //單片機將DQ拉低
delay(80); //精確延時 大于 480us
DQ = 1; //拉高總線
delay(14);
x=DQ; //稍做延時后 如果x=0則初始化成功 x=1則初始化失敗
delay(20);
}


/*******************************************************************/
/* 讀字節(jié)子函數 */
/*******************************************************************/
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(4);
}
return(dat);
}
/********************************************************************/
/* 寫字節(jié)子函數 */
/********************************************************************/
WriteOneChar(unsigned char dat)
{
unsigned char i=0;
for (i=8; i>0; i--)
{
DQ = 0;
DQ = dat&0x01;
delay(5);
DQ = 1;
dat>>=1;
}
}

/*void tmpwritebyte(uchar dat) //write a byte to ds18b20溫度傳感器寫一個字節(jié)
{
uint i;
uchar j;
bit testb;
for(j=1;j<=8;j++)
{
testb=dat&0x01;
dat=dat>>1;
if(testb) //write 1
{
DQ=0;
i++;i++;
DQ=1;
i=8;while(i>0)i--;
}
else
{
DQ=0; //write 0
i=8;while(i>0)i--;
DQ=1;
i++;i++;
}
}
}*/

void tmpchange(void) //DS18B20 begin change 發(fā)送溫度轉換命令
{
Init_DS18B20(); //初始化DS18B20
delay(200); //延時
WriteOneChar(0xcc); // 跳過序列號命令
WriteOneChar(0x44); //發(fā)送溫度轉換命令
}

uint tmp() //get the temperature 得到溫度值
{
float tt;
uchar a,b;
Init_DS18B20();
delay(1);
WriteOneChar(0xcc);
WriteOneChar(0xbe); //發(fā)送讀取數據命令
a=ReadOneChar(); //連續(xù)讀兩個字節(jié)數據
b=ReadOneChar();
temp=b;
temp<<=8;
temp=temp|a; //兩字節(jié)合成一個整型變量。
tt=temp*0.0625; //得到真實十進制溫度值,因為DS18B20
//可以精確到0.0625度,所以讀回數據的最低位代表的是
//0.0625度。
temp=tt*10+0.5; //放大十倍,這樣做的目的將小數點后第一位
//也轉換為可顯示數字,同時進行一個四舍五入操作。
return temp; //返回溫度值
}

/*void readrom() //read the serial 讀取溫度傳感器的序列號
{ //本程序中沒有用到此函數
uchar sn1,sn2;
Init_DS18B20();
delay(1);
WriteOneChar(0x33);
sn1=ReadOneChar();
sn2=ReadOneChar();
}*/

void delay10ms() //delay 延時10MS子函數
{
uchar a,b;
for(a=50;a>0;a--)
for(b=60;b>0;b--);
}
void display(uint tem) //display 顯示子函數
{
uchar A1,A2,A2t,A3;
if(tem>=100)
{
A1=table[tem/100];
A2t=tem%100;
A2=table1[A2t/10];
A3=table[A2t%10];
}
else
{
A2t=tem%100;
A1=table1[A2t/10];
A2=table[A2t%10];
A3=0xFF;
}
P0=A1;
P2_0=0;
P2_1=1;
P2_2=1;
delay10ms();
P0=A2;
P2_1=0;
P2_0=1;
P2_2=1;
delay10ms();
if(A3!=0xFF)
{
P0=A3;
P2_2=0;
P2_0=1;
P2_1=1;
}
}

void main() //主函數
{
do
{
tmpchange(); //溫度轉換,
display(tmp()); //顯示溫度值
}
while(1);
}



評論


技術專區(qū)

關閉