新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > 基于ARM9的18B20溫度傳感器程序

基于ARM9的18B20溫度傳感器程序

作者: 時間:2016-11-24 來源:網(wǎng)絡(luò) 收藏
18B20溫度傳感器,典型的時序操作器件,用51單片機寫很方便,可是把這現(xiàn)成的程序移植到FPGA、ARM、430上面,還需要一定的翻譯功底。

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

//-------------------------------------------------------------------------------------
//函數(shù)名稱:dmsec //入口參數(shù):count //函數(shù)功能:延時子程序
//-------------------------------------------------------------------------------------
void dmsec(uint count) { uchar i;
while(count--) {
for(i=0;
i<125;
i++);
}
}
//-------------------------------------------------------------------------------------
//函數(shù)名稱:tmreset //入口參數(shù):無 //函數(shù)功能:
//-------------------------------------------------------------------------------------
void tmreset(void) {
ushort i;
dq=0;
i=343;
while(i>0) i--;
//大約900us dq=1;
i=13;
while(i>0) i--;
} //-------------------------------------------------------------------------------------
//函數(shù)名稱:tmpre //入口參數(shù):無 //函數(shù)功能:等待DS18B20應(yīng)答
//-------------------------------------------------------------------------------------
void tmpre(void) {
uchar i;
while(dq);
while(~dq);
i=13;
while(i>0) i--;
} //-------------------------------------------------------------------------------------
//函數(shù)名稱:tmrbit //入口參數(shù):無 //返回值: dat //函數(shù)功能:在總線上讀一位
//-------------------------------------------------------------------------------------
bit tmrbit(void) {
uchar i;
bit dat;
dq=0;
i++;
dq=1;
i++;
i++;
dat=dq;
i=30;
while(i>0) i--;
return(dat);
} //-------------------------------------------------------------------------------------
//函數(shù)名稱:tmrbyte //入口參數(shù):無 //返回值: dat //函數(shù)功能:讀一個字節(jié)
//-------------------------------------------------------------------------------------
uchar tmrbyte(void) {
uchar i,j,dat=0;
for(i=1;i<=8;i++) {
j=tmrbit();
dat=(j<<7)|(dat>>1);
}
return(dat);
} //-------------------------------------------------------------------------------------
//函數(shù)名稱:tmwbyte //入口參數(shù):dat //函數(shù)功能:寫命令
//-------------------------------------------------------------------------------------
void tmwbyte(uchar dat) {
uint i;
uchar j;
bit testb;
for(j=1;j<=8;j++) {
testb=dat&0x01;
dat=dat>>1;
if(testb) {
dq=0;
//寫1 i++;
i++;
dq=1;
i=27;
while(i>0) i--;
}
else {
dq=0;
//寫0 i=27;
while(i>0) i--;
dq=1;
i++;
i++;
}
}
} //-------------------------------------------------------------------------------------
//函數(shù)名稱:tmrbyte //入口參數(shù):無 //函數(shù)功能:讀取溫度值
//-------------------------------------------------------------------------------------
void read_tr() {
uchar a,b;
ushort y;
tmreset();
tmpre();
dmsec(1);
tmwbyte(0xcc);
tmwbyte(0x44);
dmsec(100);
tmreset();
tmpre();
dmsec(1);
tmwbyte(0xcc);
tmwbyte(0xbe);
a=tmrbyte();
//l b=tmrbyte();
//h y=b&0x0f;
y=y<<8;
y=a|y;
if(y>0x800) {
y=0x1000-y;
bjTr=cal12bit(y);
bjTr=bjTr+0x8000;
}
else
{
bjTr=cal12bit(y);
}
} //12位精度
void temp_init(void) {
tmreset();
tmpre();
tmwbyte(0xcc);
tmwbyte(0x4e);
tmwbyte(0x88);
tmwbyte(0x44);
tmwbyte(0x7f);
tmreset();
}



關(guān)鍵詞: ARM918B20溫度傳感

評論


技術(shù)專區(qū)

關(guān)閉