標(biāo)題: 求DS18B20測(cè)溫C程序 [打印本頁(yè)]
作者: kai0215 時(shí)間: 2011-8-13 00:11
標(biāo)題: 求DS18B20測(cè)溫C程序
作者: kai0215 時(shí)間: 2011-8-13 00:13
要求:寄生供電,51單片機(jī)系統(tǒng)C語(yǔ)言程序,數(shù)碼管或1602顯示,聯(lián)要求很復(fù)雜,只要能測(cè)溫度了就可以,不要求做其它的控制,但一定要是寄生供電的,因?yàn)槲业氖荄S18B20P。 |
作者: zhangtaopsjpsj 時(shí)間: 2011-8-15 09:00
#include<reg51.h>
#define Disdata P0
#define weima P1
sbit dq=P3^4;
unsigned flag;
unsigned char code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0xbf};
void delay(unsigned int useconds)
{unsigned int s;
for(s=0;s<useconds;s++);
}
/********復(fù)位函數(shù)*********/
unsigned char ow_reset(void)
{unsigned char presence;
dq=0;
delay(73);
dq=1;
delay(12);
presence=dq;
delay(70);
return(presence);
}
/********寫(xiě)1位值*********/
void write_bit(unsigned char bitval)
{dq=0;
if(bitval==1)
dq=1;
delay(15);
dq=1;
}
/********寫(xiě)入字節(jié)函數(shù)*********/
void write_byte(unsigned char val)
{unsigned char i;
unsigned char temp;
for(i=0;i<8;i++)
{temp=val>>i;
temp&=0x01;
write_bit(temp);
}
delay(25);
}
/********位讀取函數(shù)*********/
unsigned char read_bit(void)
{unsigned char i;
dq=0;
dq=1;
for(i=0;i<10;i++);
return(dq);
}
/********字節(jié)讀取函數(shù)*********/
unsigned char read_byte(void)
{unsigned char i;
unsigned char value=0;
for(i=0;i<8;i++)
{if (read_bit())
value|=0x01<<i;
delay(120);
}
return(value);
}
/********讀暫存器*********/
void read_scratchpad(void)
{unsigned char j=0,pad[10];
write_byte(0xBE);
for(j=0;j<9;j++)
pad[j]=read_byte();
}
/********讀取溫度函數(shù)*********/
float read_temperature(void)
{unsigned char get[10];
unsigned char temp_lsb,temp_msb;
unsigned char k;
float t;
ow_reset();
write_byte(0xCC);
write_byte(0x44);
delay(60);
ow_reset();
write_byte(0xCC);
write_byte(0xBE);
for(k=0;k<9;k++)
get[k]=read_byte();
temp_msb=get[1];
temp_lsb=get[0];
if(temp_msb>=0x08)
{temp_lsb=(~temp_lsb)+1;
temp_msb&=0x07;
temp_msb=(~temp_msb)+1;
t=(-1)*(temp_msb*16+temp_lsb*0.0625);
}
else
{t=temp_msb*16+temp_lsb*0.0625;
}
return t;
}
作者: solo中的陸遜 時(shí)間: 2011-8-15 14:48
那你得好好修改程序才行,因?yàn)橛布赡懿灰粯,不如端口什么的?/p>
作者: l834492956 時(shí)間: 2011-9-17 12:33
我是搞單片機(jī)產(chǎn)品開(kāi)發(fā)的,我QQ:834492956
作者: 王翠芳 時(shí)間: 2011-10-23 21:45
我的是很久以前編寫(xiě)的,應(yīng)該可以,附帶PROTUS仿真圖,可以參考一下!
ds18b20_12m.rar
(50.72 KB, 下載次數(shù): 99)
2011-10-23 21:45 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
作者: 雕獼獅狒 時(shí)間: 2015-3-24 16:45
你的按鈕有什么用的,還有怎樣調(diào)它不一閃一閃,有沒(méi)有7段數(shù)碼管顯示的程序
作者: 雕獼獅狒 時(shí)間: 2015-3-24 16:47
問(wèn)題答案是什么怎樣加你啊
作者: 少年和他的AK47 時(shí)間: 2016-8-27 13:31
void Lcd1602_Delay1ms(uint c) //誤差 0us { uchar a,b; for (; c>0; c--) { for (b=199;b>0;b--) { for(a=1;a>0;a--); } } } /******************************************************************************* * 函 數(shù) 名 : LcdWriteCom * 函數(shù)功能 : 向LCD寫(xiě)入一個(gè)字節(jié)的命令 * 輸 入 : com * 輸 出 : 無(wú) *******************************************************************************/ #ifndef LCD1602_4PINS //當(dāng)沒(méi)有定義這個(gè)LCD1602_4PINS時(shí) void LcdWriteCom(uchar com) //寫(xiě)入命令 { LCD1602_E = 0; //使能 LCD1602_RS = 0; //選擇發(fā)送命令 LCD1602_RW = 0; //選擇寫(xiě)入 LCD1602_DATAPINS = com; //放入命令 Lcd1602_Delay1ms(1); //等待數(shù)據(jù)穩(wěn)定 LCD1602_E = 1; //寫(xiě)入時(shí)序 Lcd1602_Delay1ms(5); //保持時(shí)間 LCD1602_E = 0; } #else void LcdWriteCom(uchar com) //寫(xiě)入命令 { LCD1602_E = 0; //使能清零 LCD1602_RS = 0; //選擇寫(xiě)入命令 LCD1602_RW = 0; //選擇寫(xiě)入 LCD1602_DATAPINS = com; //由于4位的接線是接到P0口的高四位,所以傳送高四位不用改 Lcd1602_Delay1ms(1); LCD1602_E = 1; //寫(xiě)入時(shí)序 Lcd1602_Delay1ms(5); LCD1602_E = 0; // Lcd1602_Delay1ms(1); LCD1602_DATAPINS = com << 4; //發(fā)送低四位 Lcd1602_Delay1ms(1); LCD1602_E = 1; //寫(xiě)入時(shí)序 Lcd1602_Delay1ms(5); LCD1602_E = 0; } #endif /******************************************************************************* * 函 數(shù) 名 : LcdWriteData * 函數(shù)功能 : 向LCD寫(xiě)入一個(gè)字節(jié)的數(shù)據(jù) * 輸 入 : dat * 輸 出 : 無(wú) *******************************************************************************/ #ifndef LCD1602_4PINS void LcdWriteData(uchar dat) //寫(xiě)入數(shù)據(jù) { LCD1602_E = 0; //使能清零 LCD1602_RS = 1; //選擇輸入數(shù)據(jù) LCD1602_RW = 0; //選擇寫(xiě)入 LCD1602_DATAPINS = dat; //寫(xiě)入數(shù)據(jù) Lcd1602_Delay1ms(1); LCD1602_E = 1; //寫(xiě)入時(shí)序 Lcd1602_Delay1ms(5); //保持時(shí)間 LCD1602_E = 0; } #else void LcdWriteData(uchar dat) //寫(xiě)入數(shù)據(jù) { LCD1602_E = 0; //使能清零 LCD1602_RS = 1; //選擇寫(xiě)入數(shù)據(jù) LCD1602_RW = 0; //選擇寫(xiě)入 LCD1602_DATAPINS = dat; //由于4位的接線是接到P0口的高四位,所以傳送高四位不用改 Lcd1602_Delay1ms(1); LCD1602_E = 1; //寫(xiě)入時(shí)序 Lcd1602_Delay1ms(5); LCD1602_E = 0; LCD1602_DATAPINS = dat << 4; //寫(xiě)入低四位 Lcd1602_Delay1ms(1); LCD1602_E = 1; //寫(xiě)入時(shí)序 Lcd1602_Delay1ms(5); LCD1602_E = 0; } #endif /******************************************************************************* * 函 數(shù) 名 : LcdInit() * 函數(shù)功能 : 初始化LCD屏 * 輸 入 : 無(wú) * 輸 出 : 無(wú) *******************************************************************************/ #ifndef LCD1602_4PINS void LcdInit() //LCD初始化子程序 { LcdWriteCom(0x38); //開(kāi)顯示 LcdWriteCom(0x0c); //開(kāi)顯示不顯示光標(biāo) LcdWriteCom(0x06); //寫(xiě)一個(gè)指針加1 LcdWriteCom(0x01); //清屏 LcdWriteCom(0x80); //設(shè)置數(shù)據(jù)指針起點(diǎn) } #else void LcdInit() //LCD初始化子程序 { LcdWriteCom(0x32); //將8位總線轉(zhuǎn)為4位總線 LcdWriteCom(0x28); //在四位線下的初始化 LcdWriteCom(0x0c); //開(kāi)顯示不顯示光標(biāo) LcdWriteCom(0x06); //寫(xiě)一個(gè)指針加1 LcdWriteCom(0x01); //清屏 LcdWriteCom(0x80); //設(shè)置數(shù)據(jù)指針起點(diǎn) } #endif
作者: xcd824855788 時(shí)間: 2017-4-26 18:04
/*******************************************
程序功能:利用單線制DS18B20溫度傳感器
LED數(shù)碼管顯示當(dāng)前的溫度值
********************************************/
#include <reg51.h> //DS18B20溫度傳感器程序
code unsigned char seg7code[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,
0x82,0xf8,0x80,0x90,0xff}; //不帶小數(shù)點(diǎn)的共陽(yáng)數(shù)碼管段碼
code unsigned char seg7codeB[]={0x40,0x79,0x24,0x30,0x19,0x12,
0x02,0x78,0x00,0x10,0xff}; //帶小數(shù)點(diǎn)的共陽(yáng)數(shù)碼管段碼
sbit DQ=P3^6; //數(shù)據(jù)傳輸線接單片機(jī)的相應(yīng)的引腳
unsigned char tempL=0; //設(shè)全局變量
unsigned char tempH=0;
unsigned int sdata;//測(cè)量到的溫度的整數(shù)部分
unsigned char xiaoshu1;//小數(shù)第一位
unsigned char xiaoshu2;//小數(shù)第二位
unsigned char xiaoshu;//兩位小數(shù)
bit fg=1; //溫度正負(fù)標(biāo)志
//******************延時(shí)子程序 *******************************
//這個(gè)延時(shí)程序的具體延時(shí)時(shí)間是time=i*8+10,適用于小于2ms的延時(shí)
//************************************************************
void delay(unsigned char i)
{
for(i;i>0;i--);
}
//***********************************************************
// 延時(shí)子程序
//************************************************************
void delay1ms()
{
unsigned char i;
for(i=124;i>0;i--); //延時(shí)124*8+10=1002us
}
//*****************************初始化程序 *********************************//
Init_DS18B20(void)
{
unsigned char x=0;
DQ=1; //DQ先置高
delay(8); //稍延時(shí)
DQ=0; //發(fā)送復(fù)位脈沖
delay(80); //延時(shí)(>480us)
DQ=1; //拉高數(shù)據(jù)線
delay(5); //等待(15~60us)
x=DQ; //用X的值來(lái)判斷初始化有沒(méi)有成功,18B20存在的話X=0,否則X=1
delay(20);
}
//**********讀一個(gè)字節(jié)************//
ReadOneChar(void) //主機(jī)數(shù)據(jù)線先從高拉至低電平1us以上,再使數(shù)據(jù)線升為高電平,從而產(chǎn)生讀信號(hào)
{
unsigned char i=0; //每個(gè)讀周期最短的持續(xù)時(shí)間為60us,各個(gè)讀周期之間必須有1us以上的高電平恢復(fù)期
unsigned char dat=0;
for (i=8;i>0;i--) //一個(gè)字節(jié)有8位
{
DQ=1;
delay(1);
DQ=0;
dat>>=1;
DQ=1;
if(DQ)
dat|=0x80;
delay(4);
}
return(dat);
}
//*********************** **寫(xiě)一個(gè)字節(jié)**************************//
WriteOneChar(unsigned char dat)
{
unsigned char i=0; //數(shù)據(jù)線從高電平拉至低電平,產(chǎn)生寫(xiě)起始信號(hào)。15us之內(nèi)將所需寫(xiě)的位送到數(shù)據(jù)線上,
for(i=8;i>0;i--) //在15~60us之間對(duì)數(shù)據(jù)線進(jìn)行采樣,如果是高電平就寫(xiě)1,低寫(xiě)0發(fā)生。
{
DQ=0; //在開(kāi)始另一個(gè)寫(xiě)周期前必須有1us以上的高電平恢復(fù)期。
DQ=dat&0x01;
delay(5);
DQ=1;
dat>>=1;
}
delay(4);
}
//讀溫度值(低位放tempL;高位放tempH;)
ReadTemperature(void)
{
Init_DS18B20(); //初始化
WriteOneChar(0xcc); //跳過(guò)讀序列號(hào)的操作
WriteOneChar(0x44); //啟動(dòng)溫度轉(zhuǎn)換
delay(125); //轉(zhuǎn)換需要一點(diǎn)時(shí)間,延時(shí)
Init_DS18B20(); //初始化
WriteOneChar(0xcc); //跳過(guò)讀序列號(hào)的操作
WriteOneChar(0xbe); //讀溫度寄存器(頭兩個(gè)值分別為溫度的低位和高位)
tempL=ReadOneChar(); //讀出溫度的低位LSB
tempH=ReadOneChar(); //讀出溫度的高位MSB
if(tempH>0x7f) //最高位為1時(shí)溫度是負(fù)
{
tempL=~tempL; //補(bǔ)碼轉(zhuǎn)換,取反加一
tempH=~tempH+1;
fg=0; //讀取溫度為負(fù)時(shí)fg=0
}
sdata = tempL/16+tempH*16; //整數(shù)部分
xiaoshu1 = (tempL&0x0f)*10/16; //小數(shù)第一位
xiaoshu2 = (tempL&0x0f)*100/16%10;//小數(shù)第二位
xiaoshu=xiaoshu1*10+xiaoshu2; //小數(shù)兩位
}
//********************顯示函數(shù)****************
void Led(unsigned int date)
{
if(fg==1)
{
P2=0xfe; //P1.0=0,選通第一位
P0=seg7code[date/10]; //十位數(shù),查表,輸出
delay1ms();
P0=0xff; //消隱
P2=0xfd; //P1.1=0,選通第二位,個(gè)位數(shù)
P0=seg7codeB[date%10];
delay1ms();
P0=0xff; //消隱
P2=0xfb; //P1.3=0,選通第三位,小數(shù)點(diǎn)第一位
P0=seg7code[xiaoshu1];
delay1ms();
P0=0xff; //消隱
P2=0xf7; //P1.3=0,選通第四位,小數(shù)點(diǎn)第二位
P0=seg7code[xiaoshu2];
delay1ms();
P0=0xff; //消隱
}
if(fg==0) //溫度為負(fù)時(shí)顯示的數(shù)據(jù)
{
P2=0xfe; //P1.0=0,選通第一位
P0=seg7code[11]; //顯示負(fù)號(hào)
delay1ms();
P0=0xff; //消隱
P2=0xfd; //P1.1=0,選通第二位,十位數(shù)
P0=seg7code[date/10];
delay1ms();
P0=0xff; //消隱
P2=0xfb; //P1.3=0,選通第三位,個(gè)位數(shù)
P0=seg7codeB[date%10];
delay1ms();
P0=0xff; //消隱
P2=0xf7; //P1.3=0,選通第四位,小數(shù)點(diǎn)第一位
P0=seg7code[xiaoshu1];
delay1ms();
P0=0xff; //消隱
}
}
//**************************** //主程序
main()
{
while(1)
{
ReadTemperature();
Led(sdata);
}
}
不知道對(duì)你有沒(méi)有幫助
作者: xcd824855788 時(shí)間: 2017-5-9 16:11
同問(wèn)怎么讓它不要閃爍和有沒(méi)有數(shù)碼管顯示的
作者: 1240683710 時(shí)間: 2017-5-22 21:43
#define DS18B20 PA5 void Delayus(unsigned int US) { unsigned int i; US=US*5/4; //5/4是在8MHz晶振下,通過(guò)軟件仿真反復(fù)實(shí)驗(yàn)得到的數(shù)值 for( i=0;i<US;i++); } /******************************************* 函數(shù)名稱: B20_init 功 能: 復(fù)位DS18B20 參 數(shù): 無(wú) 返回值 : 無(wú) /********************************************/ void B20_init(void) { DDRA|=BIT(DS18B20); //配置為輸出 PORTA&=~BIT(DS18B20); //拉低 Delayus(600); //等待600微秒 PORTA|=BIT(DS18B20); //釋放總線 Delayus(60); //等待60微秒 DDRA&=~BIT(DS18B20); //配置為輸入 while((PINA&(BIT(DS18B20)))); //等待DS18B20拉低 while(!(PINA&(BIT(DS18B20)))); //等待DS18B20釋放總線 } /******************************************* 函數(shù)名稱: B20_readB 功 能: 讀取一個(gè)字節(jié)的數(shù)據(jù) 參 數(shù): 無(wú) 返回值 : retd--返回的一個(gè)字節(jié)數(shù)據(jù) /********************************************/ unsigned char B20_readB(void) { unsigned char i,retd=0; for(i=0;i<8;i++) //位計(jì)數(shù)值 { retd>>=1; //右移,準(zhǔn)備接受新的數(shù)據(jù)位 DDRA|=BIT(DS18B20); //配置為輸出 PORTA&=~BIT(DS18B20); //拉低,啟動(dòng)讀數(shù)據(jù)位 PORTA|=BIT(DS18B20); //釋放總線 Delayus(5); //等待5微秒 DDRA&=~BIT(DS18B20); //配置為輸入,開(kāi)始讀取數(shù)據(jù)位 if(PINA&BIT(DS18B20)) //該位是否為高 { retd|=0x80; //是就將此位置高 } Delayus(50); //等待50微秒 } return retd; //將讀到的一個(gè)字節(jié)返回 } /******************************************* 函數(shù)名稱: B20_writeB 功 能: 寫(xiě)入一個(gè)字節(jié)的數(shù)據(jù) 參 數(shù): wrd--要寫(xiě)入的數(shù)據(jù) 返回值 : 無(wú) /********************************************/ void B20_writeB(unsigned char wrd) { unsigned char i; for(i=0;i<8;i++) //位計(jì)數(shù)值 { DDRA|=BIT(DS18B20); //配置為輸出 PORTA&=~BIT(DS18B20); //拉低,啟動(dòng)寫(xiě)數(shù)據(jù)位 Delayus(1); //等待1微秒 if(wrd&0x01) //此位數(shù)據(jù)是否為高 { PORTA|=BIT(DS18B20); //是高則將單總線拉高 } else { PORTA&=~BIT(DS18B20); //是低則將單總線拉低 } Delayus(50); //等待50微秒 PORTA|=BIT(DS18B20); //釋放總線 wrd>>=1; //右移,為寫(xiě)入新的數(shù)據(jù)位做準(zhǔn)備 } Delayus(50); //等待50微秒 } /******************************************* 函數(shù)名稱: Read_temp 功 能: 讀取溫度值 參 數(shù): 無(wú) 返回值 : rettemp--返回的溫度值 /********************************************/ unsigned int Read_temp(void) { unsigned char templ,temph; unsigned int temp; B20_init(); //初始化,每次寫(xiě)命令都從初始化開(kāi)始 B20_writeB(0xcc); //跳過(guò)ROM B20_writeB(0x44); //啟動(dòng)溫度轉(zhuǎn)換 B20_init(); //初始化,每次寫(xiě)命令都從初始化開(kāi)始 B20_writeB(0xcc); //跳過(guò)ROM B20_writeB(0xbe); //讀寄存器 templ=B20_readB(); //讀溫度低字節(jié) temph=B20_readB(); //讀溫度高字節(jié) temp=templ+temph*256;//將溫度整理成16位變量 return temp; //返回16位變量 }
歡迎光臨 (http://www.torrancerestoration.com/bbs/) |
Powered by Discuz! X3.1 |