|
常用的溫度傳感器DS18B20
單片機(jī)源程序如下:
- #include "reg52.h"
- #include "temp.h"
- typedef unsigned int u16;
- typedef unsigned char u8;
- sbit LSA=P2^2;
- sbit LSB=P2^3;
- sbit LSC=P2^4;
- u8 code smgduan[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
- u8 DisplayData[8];
- void delay(u16 i)
- {
- while(i--);
- }
- void datapros(int temp) //顯示內(nèi)容
- {
- float tp;
- if(temp<0) //高四位符號(hào)決定
- {
- DisplayData[0]=0x40;
- temp=temp-1;
- temp=~temp; //還原數(shù)據(jù)
- tp=temp;
- temp=tp*0.0625*100+0.5;//因?yàn)閠emp是int類型的,*100是為了保留兩位小數(shù),加0.5是為了補(bǔ)那直接約去的小數(shù)位,可不加0.5
- }else{
- DisplayData[0]=0x00;
- tp=temp;
- temp=tp*0.0625*100+0.5;
- }
-
-
- DisplayData[1]=smgduan[temp/10000];//百位
- DisplayData[2]=smgduan[temp%10000/1000];//十位
- DisplayData[3]=smgduan[temp%10000%1000/100]|0x80;//個(gè)位
- DisplayData[4]=smgduan[temp%100/10];
- DisplayData[5]=smgduan[temp%100%10];
- }
- void DigDisplay() //用作顯示動(dòng)態(tài)晶碼管
- {
- u8 i;
- for(i=0;i<6;i++)
- {
- switch(i) //位選,選擇點(diǎn)亮的數(shù)碼管
- {
- case(0):
- LSA=0;LSB=0;LSC=0; break;//顯示第0位
- case(1):
- LSA=1;LSB=0;LSC=0; break;//顯示第1位
- case(2):
- LSA=0;LSB=1;LSC=0; break;//顯示第2位
- case(3):
- LSA=1;LSB=1;LSC=0; break;//顯示第3位
- case(4):
- LSA=0;LSB=0;LSC=1; break;//顯示第4位
- case(5):
- LSA=1;LSB=0;LSC=1; break;//顯示第5位
- }
- P0=DisplayData[i];//發(fā)送數(shù)據(jù)
- delay(100); //間隔一段時(shí)間掃描
- P0=0x00;//消隱
- }
- }
- void main()
- {
- while(1) //不斷
- {
- datapros(DS18B20ReadTemp()); //唯一一處調(diào)用自定義文件的DS18B20ReadTemp()函數(shù)
- DigDisplay();
- }
- }
復(fù)制代碼
所有資料51hei提供下載:
DS18B20溫度傳感器.zip
(38.5 KB, 下載次數(shù): 20)
2018-7-21 19:13 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
|
|