單總線掛接4路ds18B20溫度監測系統
0.png (426.52 KB, 下載次數: 37)
下載附件
2016-6-3 15:28 上傳
4路ds18B20溫度監測51單片機程序:
- /*******************************************************************
- **函數功能:ds18b20的驅動函數 **
- **創建人:xingyuegu **
- **創建日期:7-12 **
- **版本:1.0 **
- **修改日期:10-30 **
- **版本:2.0 **
- *******************************************************************/
- #include <reg51.h>
- #include <intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- void serial_set(void);
- void process(unsigned char,unsigned char);
- char volatile xiaoshu_temp[5],zhen_temp[4]; //
- bit flag;
- bit dot_dis=1;
- sbit DQ=P1^0;
- //sbit DQ=P2^3;
- extern unsigned char temp[5];
- /*************************************************************
- **功能:延時600us **
- **參數:無 **
- *************************************************************/
- void delay600us(void)
- {
- uchar i;
- for(i=0;i<255;i++);
- }
- /*************************************************************
- **功能:延時60us **
- **參數:無 **
- *************************************************************/
- void delay60us(void)
- {
- uchar i;
- for(i=0;i<20;i++);
- }
- /*************************************************************
- **功能:延時18us **
- **參數:無 **
- *************************************************************/
- /*void delay15us(void)
- {
- uchar i;
- for(i=0;i<2;i++);
- }*/
- /*************************************************************
- **功能:復位脈沖 **
- **參數:bool **
- *************************************************************/
- bit resetpulse(void)
- {
-
- DQ=0;
- delay600us(); //延時500us
- DQ=1;
- delay60us(); // 延時60us
- return(DQ); //讀取P1.0的狀態
- }
- /*************************************************************
- **功能:ds18b20的初始化 **
- **參數:無 **
- *************************************************************/
- void ds18b20_init(void)
- {
- while(1)
- {
- if(!resetpulse()) //收到ds18b20的應答信號
- {
- //printf("reset successful!");
- //delay240us(); //延時240us
- DQ=1;
- delay600us(); //延時240us
- break;
- }
- else
- resetpulse(); //否則再發復位信號
- }
- }
- /*************************************************************
- **功能:向ds18b20寫命令 **
- **參數:無 **
- *************************************************************/
- void ds18b20_writecommand(uchar command)
- {
- uchar i;
- for(i=0;i<8;i++)
- {
- if((command & 0x01)==0)
- {
- DQ=0; //寫0
- delay60us(); //延時60us
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- DQ=1;
- _nop_();
- _nop_();
- }
-
- else //寫1
- {
- DQ=0;
- _nop_();
- _nop_(); //延時2us
- DQ=1;
- delay60us(); //延時60us
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- }
- command=_cror_(command,1); // 右移1位
- }
-
-
- }
- /*************************************************************
- **功能:讀ds18b20數據 **
- **參數:返回讀到的數據 **
- *************************************************************/
- uchar ds18b20_readdata(void)
- {
- uchar readdata;
- uchar i;
- for(i=0;i<8;i++)
- {
- DQ=0;
- _nop_();
- _nop_();
- _nop_();
- DQ=1; //釋放總線
- //delay15us(); 注意不需要
- if(DQ==0) //如果讀到的是0
- {
- readdata=readdata&0x7f;
- delay60us();
- }
- else //讀到的是1
- {
- readdata=readdata|0x80;
- delay60us();
- }
- if(i<7)
-
- readdata=_cror_(readdata,1);
- }
- return readdata;
- }
- /*************************************************************
- **功能:溫度處理函數 **
- **參數:無返回 **
- *************************************************************/
- void temperature_process(uchar low,uchar high)
- {
- uint temp1,temp2,temp3;
- if(high&0x80)//判斷正負
- {
- flag=1;
- temp3=temp3|high;
- temp3=temp3&0x00ff;
- temp3=temp3<<8;
- temp1=temp3;
- temp1=temp1|low;
-
- temp1=(temp1^0xffff);
- temp1=temp1+1; //取反加1
- low=temp1&0x000f;
- high=temp1>>4;
- process(high,low);
- }
- else
- {
- flag=0; //zhen
- temp1=high;
- temp2=low;
- temp1=temp1<<4;
- temp2=temp2>>4;
- temp3=temp1|temp2;
- high=temp3;
- low=low&0x0f;
- process(high,low);
-
- }
- }
- /*************************************************************
- **功能:數值處理函數 **
- **參數:無返回 **
- *************************************************************/
- void process(unsigned char high,unsigned char low)
- {
- uint temp1;
- uchar i;
- temp1=low*625;
- xiaoshu_temp[0]=temp1/1000+'0';
- //xiaoshu_temp[1]=temp1/100%10+'0';
- //xiaoshu_temp[2]=temp1%100/10+'0';
- //xiaoshu_temp[3]=temp1%10+'0';
- xiaoshu_temp[1]='\0';
- dot_dis=1;
- //if(xiaoshu_temp[3]=='0')
- //{
- // xiaoshu_temp[3]='\0';
- // if(xiaoshu_temp[2]=='0')
- // {
- // xiaoshu_temp[2]='\0';
- // if(xiaoshu_temp[1]=='0')
- // {
- // xiaoshu_temp[1]='\0';
- if(xiaoshu_temp[0]=='0')
- {
- xiaoshu_temp[0]='\0';
- dot_dis=0;
- }
- else
- dot_dis=1;
-
-
- // }
- // }
-
- // }
-
-
- zhen_temp[0]=high/100+'0';
- zhen_temp[1]=high%100/10+'0';
- zhen_temp[2]=high%10+'0';
- zhen_temp[3]='\0';
- for(i=0;i<2;i++)
- if(zhen_temp[0]=='0')
- {
- zhen_temp[0]=zhen_temp[1];
- zhen_temp[1]=zhen_temp[2];
- zhen_temp[2]='\0';
- }
- else
- break;
- }
- /****************************************/
- //serial_set func
- void serial_set(void)
- {
-
- SCON=0x50;
- TMOD=TMOD&0x0f;
- TMOD=TMOD|0x20;
- TH1=0xfd;
- TL1=0xfd;
- TR1=1;
- TI=1; //用PRINTF時,TI要置1
- }
- /*************************************************************
- **功能:序列號匹配子程序 **
- **參數:無返回 **
- *************************************************************/
- bit match_rom(uchar *rom)
- {
- uchar i;
- ds18b20_init();
- ds18b20_writecommand(0x55);
- for(i=8;i>0;i--)
- {
- ds18b20_writecommand(*(rom+i-1));
- // rom++;
- }
- return 1;
- }
-
復制代碼
0.png (74.51 KB, 下載次數: 53)
下載附件
2016-6-3 15:28 上傳
所有資料下載:
單總線掛接4路ds18B20溫度監測系統.zip
(271.41 KB, 下載次數: 23)
2016-5-25 14:23 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|