|
顯示上限溫度:
0.png (73.61 KB, 下載次數(shù): 57)
下載附件
2017-5-13 03:36 上傳
0.png (79.34 KB, 下載次數(shù): 58)
下載附件
2017-5-13 03:36 上傳
顯示實時溫度:
0.png (73.36 KB, 下載次數(shù): 49)
下載附件
2017-5-13 03:36 上傳
顯示下限溫度:
0.png (79.32 KB, 下載次數(shù): 43)
下載附件
2017-5-13 03:37 上傳
原理圖:
psb.png (57.96 KB, 下載次數(shù): 58)
下載附件
2017-5-13 03:35 上傳
0.png (67.04 KB, 下載次數(shù): 31)
下載附件
2017-5-13 03:34 上傳
單片機源程序如下:
- #include "reg51.h"
- #define uchar unsigned char
- #define uint unsigned int
- sbit DQ=P1^4; //溫度數(shù)據(jù)口
- sbit wx1=P2^4; //位選1
- sbit wx2=P2^5; //位選2
- sbit wx3=P2^6; //位選3
- sbit wx4=P2^7; //位選4
- sbit sd=P1^2; //范圍與顯示開關(guān) =1 范圍
- sbit sj=P2^1; //上下界切換 =1 上限
- sbit ss=P2^2; //加1
- sbit xj=P2^3; //減1
- sbit bjs=P1^0; //上限報警
- sbit bjx=P1^1; //下限報警
- unsigned int temp, temp1, temp2, xs;
- unsigned int sx = 30;
- unsigned int xx = 20;
- uchar code table[] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, //共陽極數(shù)碼管
- 0x92, 0x82, 0xf8, 0x80, 0x90, 0x88, 0x83, 0xc6};
- /*******延時程序*******/
- void delay1( unsigned int m )
- { //1ms
- unsigned int i, j;
- for(i = m; i > 0; i--)
- for(j = 110; j > 0; j--);
- }
- void delay( unsigned int m )
- { //1us
- while(m--);
- }
- void Init_DS18B20()
- {
- unsigned char x = 0;
- DQ = 1; //DQ復(fù)位DS18B20通信端口
- delay(8); //稍作延時
- DQ = 0; //單片機將DQ拉低
- delay(80); //精確延時 大于480us
- DQ = 1; //拉高總線
- delay(4);
- x = DQ; //稍作延時后,如果x=0則初始化成功,x=1則初始化失敗
- delay(20);
- }
- /*********DS18B20讀一個字節(jié)*********/
- uchar ReadOneChar()
- {
- unsigned char i = 0;
- unsigned char dat = 0;
- for(i = 8; i > 0; i--)
- {
- DQ = 0; //高電平拉成低電平時讀周期開始
- dat >>= 1;
- DQ = 1; //給脈沖信號
- if(DQ)
- dat |= 0x80;
- delay(4);
- }
- return dat;
- }
- /*********DS18B20寫一個字節(jié)*********/
- void WriteOneChar( unsigned char dat )
- {
- unsigned char i = 0;
- for(i = 8; i > 0; i--)
- {
- DQ = 0; //從高電平拉低至低電平,寫周期開始
- DQ = dat & 0x01; //數(shù)據(jù)的最低位先寫入
- delay(5); //60us到120us延時
- DQ = 1;
- dat >>= 1; //從最低位到最高位傳入
- }
- }
- /*********讀取DS18B20當(dāng)前溫度*********/
- void ReadTemperature()
- {
- unsigned char a = 0;
- unsigned b = 0;
- unsigned t = 0;
- Init_DS18B20();
- WriteOneChar( 0xCC ); //跳過讀序號列號的操作
- WriteOneChar( 0x44 ); //啟動溫度轉(zhuǎn)換
- delay(5); //
- Init_DS18B20();
- WriteOneChar( 0xCC ); //跳過讀序號列號的操作
- WriteOneChar( 0xBE ); //讀取溫度寄存器等(共可讀9個寄存器)前兩個就是溫度
- delay(5);
- a = ReadOneChar(); //讀取溫度值低位
- b = ReadOneChar(); //讀取溫度值高位
- temp1 = b << 4; //高8位中后三位數(shù)的值
- temp1 += ((a & 0xf0) >> 4); //低8位中的高4位值加上搞8位中后三位數(shù)的值 temp1室溫整數(shù)值
- temp2 = a & 0xf0; //小數(shù)的值
- temp = ((b*256 + a) >> 4); //當(dāng)前采集溫度值除16得實際溫度值
- xs = temp2*0.0625*10; //小數(shù)位,若為0.5則算5來顯示
- }
- void wenduxianshi()
- {
- wx1 = 1;
- P0 = table[temp/10]; //顯示十位
- delay1(1);
- wx1 = 0;
- wx2 = 1;
- P0 = table[temp%10] + 0x80; //顯示個位 加上0x80就顯示小數(shù)點了
- delay1(1);
- wx2 = 0;
- wx3 = 1;
- P0 = table[xs%10]; //顯示小位
- delay1(1);
- wx3 = 0;
- wx4 = 1;
- P0 = table[12]; //顯示C字符
- delay1(1);
- wx4 = 0;
- }
- void sxxianshi()
- {
- wx1 = 1;
- P0 = table[sx/10]; //顯示十位
- delay1(1);
- wx1 = 0;
- wx2 = 1;
- P0 = table[sx%10]+0x80; //顯示個位
- delay1(1);
- wx2 = 0;
- wx3 = 1;
- P0 = table[12]; //顯示小位
- delay1(1);
- wx3 = 0;
- wx4 = 1;
- P0 = table[5]; //顯示C字符
- delay1(1);
- wx4 = 0;
- }
- void xxxianshi()
- {
- wx1 = 1;
- P0 = table[xx/10]; //顯示十位
- delay1(1);
- wx1 = 0;
- wx2 = 1;
- P0 = table[xx%10]+0x80; //顯示個位
- delay1(1);
- wx2 = 0;
- wx3 = 1;
- P0 = table[12]; //顯示小位
- delay1(1);
- wx3 = 0;
- wx4 = 1;
- P0 = table[12]; //顯示C字符
- delay1(1);
- wx4 = 0;
- }
- void dsx()
- {
- if(ss == 0){ delay1(5); if(ss == 1) sx = sx + 1; }
- if(xj == 0){ delay1(5); if(xj == 1) sx = sx - 1; }
- }
- void dxx()
- {
- if(ss == 0){ delay1(5); if(ss == 1) xx = xx + 1; }
- if(xj == 0){ delay1(5); if(xj == 1) xx = xx - 1; }
- }
- void alarm()
- {
- delay1(5);
- if( temp > sx )
- bjs = 1;
- if( temp < xx )
- bjx = 1;
- }
- void normal()
- {
- if( temp > xx )
- bjx = 0;
- if( temp < sx )
- bjs = 0;
- }
- void main()
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復(fù)制代碼
所有資料51hei提供下載:
溫度測量與控制系統(tǒng)設(shè)計.rar
(2.27 MB, 下載次數(shù): 55)
2017-5-12 20:17 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|
|