分享一個ds18b20傳感器檢測環境溫度,數碼管顯示并且超過一定溫度蜂鳴器蜂鳴報警,下面是部分電路仿真圖。
QQ圖片20170424212354.png (20.7 KB, 下載次數: 74)
下載附件
2017-4-24 21:24 上傳
0.png (51.89 KB, 下載次數: 76)
下載附件
2017-4-24 21:57 上傳
51單片機源程序:
- #include <reg52.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit DS=P2^2; //define interface
- uint temp; // variable of temperature
- uchar flag1; // sign of the result positive or negative
- sbit wela1=P2^4; //段選信號的鎖存器控制
- sbit wela2=P2^5;
- sbit wela3=P2^6; //段選信號的鎖存器控制
- sbit wela4=P2^7; //位選信號的鎖存器控制
- sbit beep=P2^3;
- unsigned char code table[]={0xC0,0xf9,0xa4,0xb0,0x99,0x92,0x82,
- 0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
- unsigned char code table1[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,
- 0x87,0xff,0xef};
- void delay(uint count) //delay
- {
- uint i;
- while(count)
- {
- i=200;
- while(i>0)
- i--;
- count--;
- }
- }
- void dsreset(void) //send reset and initialization command
- {
- uint i;
- DS=0;
- i=103;
- while(i>0)i--;
- DS=1;
- i=4;
- while(i>0)i--;
- }
- bit tmpreadbit(void) //read a bit
- {
- uint i;
- bit dat;
- DS=0;i++; //i++ for delay
- DS=1;i++;i++;
- dat=DS;
- i=8;while(i>0)i--;
- return (dat);
- }
- uchar tmpread(void) //read a byte date
- {
- uchar i,j,dat;
- dat=0;
- for(i=1;i<=8;i++)
- {
- j=tmpreadbit();
- dat=(j<<7)|(dat>>1); //讀出的數據最低位在最前面,這樣剛好一個字節在DAT里
- }
- return(dat);
- }
- void tmpwritebyte(uchar dat) //write a byte to ds18b20
- {
- uint i;
- uchar j;
- bit testb;
- for(j=1;j<=8;j++)
- {
- testb=dat&0x01;
- dat=dat>>1;
- if(testb) //write 1
- {
- DS=0;
- i++;i++;
- DS=1;
- i=8;while(i>0)i--;
- }
- else
- {
- DS=0; //write 0
- i=8;while(i>0)i--;
- DS=1;
- i++;i++;
- }
- }
- }
- void tmpchange(void) //DS18B20 begin change
- {
- dsreset();
- delay(1);
- tmpwritebyte(0xcc); // address all drivers on bus
- tmpwritebyte(0x44); // initiates a single temperature conversion
- }
- uint tmp() //get the temperature
- {
- float tt;
- uchar a,b;
- dsreset();
- delay(1);
- tmpwritebyte(0xcc);
- tmpwritebyte(0xbe);
- a=tmpread();
- b=tmpread();
- temp=b;
- temp<<=8; //two byte compose a int variable
- temp=temp|a;
- tt=temp*0.0625;
- temp=tt*10+0.5;
- return temp;
- }
- void display(uint temp) //顯示程序
- {
-
- uchar A1,A2,A2t,A3;
- A1=temp/100;
- A2t=temp%100;
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
下載:
數碼管顯示溫度并報警 .rar
(63.77 KB, 下載次數: 68)
2017-4-24 21:28 上傳
點擊文件名下載附件
數碼管顯示溫度并報警 下載積分: 黑幣 -5
|