|
單片機DS18B20溫度傳感器
- ////////////////////////////////////////////////////////////////////////////
- // 給力者單片機開發學習系統,開發學習都給力! //
- ////////////////////////////////////////////////////////////////////////////
- // 學習51單片機,其實可以很簡單 //
- ////////////////////////////////////////////////////////////////////////////
- // 寧波芯動電子有限公司 //
- ////////////////////////////////////////////////////////////////////////////
- #include <AT89X52.h> //調用51單片機的頭文件
- #include <Intrins.h>
- //---------------------------------------
- //1602液晶相關I/O設置
- sbit E=P2^3; //1602液晶的E腳接在P2.3口上
- sbit RW=P2^4; //1602液晶的RW腳接在P2.4口上
- sbit RS=P2^5; //1602液晶的RS腳接在P2.5口上
- //---------------------------------------
- sbit ds18b20=P3^7;
- //---------------------------------------
- //1602液晶寄存器設置
- unsigned char DISbuf; //設置8位的unsigend char型寄存器用來暫存1602要顯示的內容
- //---------------------------------------
-
-
- unsigned char temp1;
- unsigned char temp2;
- unsigned char temp1buf1;
- unsigned char temp1buf2;
- bit tc;
- bit no;
- unsigned char code tablexiao[16]={0,0,1,2,2,3,4,4,5,6,6,7,8,8,9,9};
- //---------------------------------------
- //名稱:延時函數
- //適用:給力者GL9單片機開發學習系統
- //公司:寧波芯動電子有限公司
- //日期:20120916
- //---------------------------------------
- void Delay(unsigned int nTimeDelay)
- {
- unsigned int i;
- while (nTimeDelay--)
- for (i=0;i<125;i++)
- {}
- }
- //---------------------------------------
- //名稱:復位DS18B20函數
- //適用:給力者GL9單片機開發學習系統
- //公司:寧波芯動電子有限公司
- //日期:20120916
- //---------------------------------------
- void Reset()
- {
- unsigned int i;
-
- ds18b20=0; //拉低DQ總線開始復位
- i=206;
- while(i>0)
- i--; //保持DQ低大約900uS
- ds18b20=1; //釋放DQ位
- i=8;
- while(i>0)
- i--;
- }
- //---------------------------------------
- //名稱:等待芯片應答信號函數
- //適用:給力者GL9單片機開發學習系統
- //公司:寧波芯動電子有限公司
- //日期:20120916
- //---------------------------------------
- bit WaitPresence()
- {
- unsigned char i;
- bit k;
- i=16;
- while(i>0)
- i--;
-
- if(ds18b20==0)
- k=1;
- else
- k=0;
- i=72;
- while(i>0)
- i--;
- return k;
-
- }
- //---------------------------------------
- //名稱:讀一位數據函數
- //適用:給力者GL9單片機開發學習系統
- //公司:寧波芯動電子有限公司
- //日期:20120916
- //---------------------------------------
- bit ReadBit()
- {
- unsigned int i;
- bit dat;
- ds18b20=0;
- i++;
-
- ds18b20=1;
- i+=2;
-
- dat=ds18b20;
- i=16;
- while(i>0)
- i--;
- return dat;
- }
- //---------------------------------------
- //名稱:讀一字節函數
- //適用:給力者GL9單片機開發學習系統
- //公司:寧波芯動電子有限公司
- //日期:20120916
- //---------------------------------------
- unsigned char ReadByte()
- {
- unsigned char i,j,dat;
- dat=0;
- for(i=1;i<=8;i++)
- {
- j=ReadBit();
- dat=(j<<7) | (dat>>1);
- }
- return dat;
- }
- //---------------------------------------
- //名稱:寫一字節函數
- //適用:給力者GL9單片機開發學習系統
- //公司:寧波芯動電子有限公司
- //日期:20120916
- //---------------------------------------
- void WriteByte(unsigned char dat)
- {
- unsigned int i;
- unsigned char j;
- bit testb;
- for(j=1;j<=8;j++)
- {
- testb=dat & 0x01;
- dat=dat>>1;
- if(testb)
- {
- ds18b20=0;
- i+=2;
-
- ds18b20=1;
- i=8;
- while(i>0)
- i--;
- }
- else
- {
- ds18b20=0;
- i=8;
- while(i>0)
- i--;
-
- ds18b20=1;
- i+=2;
- }
- }
- }
- //---------------------------------------
- //名稱:DS18B20溫度轉換函數
- //適用:給力者GL9單片機開發學習系統
- //公司:寧波芯動電子有限公司
- //日期:20120916
- //---------------------------------------
- bit Convert()
- {
- Reset();
- if(WaitPresence()==1)
- {
- Delay(1);
- WriteByte(0xcc);//skip rom
- WriteByte(0x44);//convert
- return 1;
- }
- else
- {
- return 0;
- }
- }
- //---------------------------------------
- //名稱:轉換結束處理函數
- //適用:給力者GL9單片機開發學習系統
- //公司:寧波芯動電子有限公司
- //日期:20120916
- //---------------------------------------
- bit ReadFlash()
- {
- unsigned char nLsb,nMsb;
- Reset();
- if(WaitPresence()==1)
- {
- Delay(1);
- WriteByte(0xcc);//skip rom
- WriteByte(0xbe);//convert
- nLsb=ReadByte();//LSB
- nMsb=ReadByte();//MSB
- if(no==1) return 1;
- temp2=nLsb&0x0f;
- temp1=(nLsb>>4)|(nMsb<<4);
- return 1;
- }
- else
- {
- return 0;
- }
- }
- //---------------------------------------
- //名稱:1602液晶忙檢測函數
- //適用:給力者GL9單片機開發學習系統
- //公司:寧波芯動電子有限公司
- //日期:20120916
- //---------------------------------------
- void LCD1602_busy(void)
- {
- P0_7=1; //將P0.7置1,為讀狀態做準備
- RS=0; //RS=0、RW=1、E=1時,忙信號輸出到DB7,由P0.7讀入
- RW=1; //RS=0、RW=1、E=1時,忙信號輸出到DB7,由P0.7讀入
- E=1; //RS=0、RW=1、E=1時,忙信號輸出到DB7,由P0.7讀入
- while(P0_7==1); //由P0.7讀入1,表示1602液晶忙,需要等待
- E=0; //讀完以后,恢復E的電平
- }
- //---------------------------------------
- //名稱:1600寫命令函數
- //適用:給力者GL9單片機開發學習系統
- //公司:寧波芯動電子有限公司
- //日期:20120916
- //---------------------------------------
- void LCD1602_Write_com(unsigned char combuf)
- {
- RS=0; //選擇指令寄存器
- RW=0; //選擇寫狀態
- P0=combuf; //將命令字通過P0口送至DB
- E=1; //E高電平將命令字寫入1602液晶
- E=0; //寫完以后,恢復E的電平
- }
- //---------------------------------------
- //名稱:1602寫命令函數(帶忙檢測)
- //適用:給力者GL9單片機開發學習系統
- //公司:寧波芯動電子有限公司
- //日期:20120916
- //---------------------------------------
- void LCD1602_Write_com_busy(unsigned char combuf)
- {
- LCD1602_busy(); //調用忙檢測函數
- LCD1602_Write_com(combuf); //調用忙檢測函數
- }
- //---------------------------------------
- //名稱:1602寫數據函數(帶忙檢測)
- //適用:給力者GL9單片機開發學習系統
- //公司:寧波芯動電子有限公司
- //日期:20120916
- //---------------------------------------
- void LCD1602_Write_data_busy(unsigned char databuf)
- {
- LCD1602_busy(); //調用忙檢測函數
- RS=1; //選擇數據寄存器
- RW=0; //選擇寫狀態
- P0=databuf; //將命令字通過P0口送至DB
- E=1; //E高電平將命令字寫入1602液晶
- E=0; //寫完以后,恢復E的電平
- }
- //---------------------------------------
- //名稱:1602液晶顯示地址寫函數
- //適用:給力者GL9單片機開發學習系統
- //公司:寧波芯動電子有限公司
- //日期:20120916
- //---------------------------------------
- void LCD1602_Write_address(unsigned char x,unsigned char y)
- {
- x&=0x0f; //列地址限制在0-15間
- y&=0x01; //行地址限制在0-1間
- if(y==0) //如果是第一行
- LCD1602_Write_com_busy(x|0x80); //將列地址寫入
- else //如果是第二行
- LCD1602_Write_com_busy((x+0x40)|0x80); //將列地址寫入
- }
- //---------------------------------------
- //名稱:1602液晶初始化函數
- //適用:給力者GL9單片機開發學習系統
- //公司:寧波芯動電子有限公司
- //日期:20120916
- //---------------------------------------
- void LCD1602_init(void)
- {
- Delay(150); //調用延時函數
- LCD1602_Write_com(0x38); //8位數據總線,兩行顯示模式,5*7點陣顯示
- Delay(50); //調用延時函數
- LCD1602_Write_com(0x38); //8位數據總線,兩行顯示模式,5*7點陣顯示
- Delay(50); //調用延時函數
- LCD1602_Write_com(0x38); //8位數據總線,兩行顯示模式,5*7點陣顯示
- LCD1602_Write_com_busy(0x38); //8位數據總線,兩行顯示模式,5*7點陣顯示
- LCD1602_Write_com_busy(0x08); //顯示功能關,無光標
- LCD1602_Write_com_busy(0x01); //清屏
- LCD1602_Write_com_busy(0x06); //寫入新的數據后,光標右移,顯示屏不移動
- LCD1602_Write_com_busy(0x0C); //顯示功能開,無光標
- }
- //---------------------------------------
- //名稱:1602液晶指定地址顯示函數
- //適用:給力者GL9單片機開發學習系統
- //公司:寧波芯動電子有限公司
- //日期:20120916
- //---------------------------------------
- void LCD1602_Disp(unsigned char x,unsigned char y,unsigned char buf)
- {
- LCD1602_Write_address(x,y); //先將地址信息寫入
- LCD1602_Write_data_busy(buf); //再寫入要顯示的數據
- }
- //---------------------------------------
- //名稱: 主函數
- //適用:給力者GL9單片機開發學習系統
- //公司:寧波芯動電子有限公司
- //日期:20120916
- //---------------------------------------
- void main(void) //主函數,單片機開機后就是從這個函數開始運行
- {
- unsigned char buf;
- LCD1602_init(); //調用1602液晶初始化函數
- while(1) //死循環,單片機初始化后,將一直運行這個死循環
- {
- tc=Convert(); //啟動轉換
- if(tc==1) //轉換結束
- {
- ReadFlash(); //讀取溫度
- if(temp1>80) temp1=0;
- if(temp2>15) temp2=0;
- buf=temp1/10;
- buf+=0x30;
- LCD1602_Disp(0,0,buf); //溫度整數部分十位
- buf=temp1%10;
- buf+=0x30;
- LCD1602_Disp(1,0,buf); //溫度整數部分個位
- LCD1602_Disp(2,0,0x2e); //.
- buf=tablexiao[temp2&0x0f];
- buf+=0x30;
- LCD1602_Disp(3,0,buf); //溫度小數部分
- LCD1602_Disp(4,0,0xdf); //.
- LCD1602_Disp(5,0,0x43); //C
- }
- }
- }
復制代碼
|
-
-
DS18B20溫度傳感器.rar
2015-11-25 16:14 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
22.59 KB, 下載次數: 13, 下載積分: 黑幣 -5
DS18B20溫度傳感器
評分
-
查看全部評分
|