?? main.c
字號:
#include<reg51.h>
#include <intrins.h>
#define uchar unsigned char
/*端口定義*/
#define LED P0 /*實際溫度值輸出端口定義*/
#define NOP _nop_()
sbit tem_in=P3^0; /*溫度讀取端口*/
//sbit con_out=P1^7; /*報警啟動端口*/
//sbit DP=P2^7;
#define L 15 /*溫度報警下限*/
#define H 40
uchar temp_h,temp_l; /*溫度值變量*/
uchar flag1; /*正負標志位*/
uchar code ledcode[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};/*數(shù)碼顯示數(shù)據(jù):0,1,2,3,4,5,6,7,8,9*/
uchar code ledbit[]={0xfe,0xfd,0xfb};//共陰led
uchar dispbuf[2]={0,0};
uchar B20num[]={1,2,3,4,5,6,7,8};//假B20共有8片
/*====================================
函數(shù)功能:延時
入口參數(shù):
說 明 :送1遲202us
=====================================*/
void delay(unsigned int count)
{
unsigned int i;
while(count)
{
i=200;
while(i>0)i--;
count--;
}
}
/*====================================
函數(shù)功能:延時 us
入口參數(shù):
說 明 :
=====================================*/
void Delay_us(uchar n)
{
uchar i;
i=0;
while(i<n)
{i++;}
return;
}
/*====================================
函數(shù)功能:DS18B20重設(shè)
入口參數(shù):無
說 明 :
=====================================*/
void dsreset(void)
{
unsigned int i;
tem_in=0;
i=103;
while(i>0)i--;
tem_in=1;
i=4;
while(i>0)i--;
}
/*====================================
函數(shù)功能:直接讀一字節(jié)程序
入口參數(shù):無
說 明 :返回結(jié)果
=====================================*/
uchar ReadByte(void){
uchar i,k;
i=8;
k=0;
while(i--)
{
tem_in=1;
Delay_us(1);
tem_in=0;
k=k>>1;
tem_in=1;
NOP;
if(tem_in)k |= 0x80;
Delay_us(4);
}
return(k);
}
/*====================================
函數(shù)功能:向B20寫一字節(jié)
入口參數(shù):待寫數(shù)據(jù)
說 明 :
=====================================*/
void tmpwrite(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)
{ tem_in=0;i++;i++;
tem_in=1;
i=8;while(i>0)i--;
}
else
{
tem_in=0;
i=8;while(i>0)i--;
tem_in=1;i++;i++;
}
}
}
//////////////////////////////////////////////////////////////
void tmpchange(void)
{
dsreset(); /*復(fù)位*/
delay(1);
tmpwrite(0xcc); //跳過序列號命令
tmpwrite(0x44); //轉(zhuǎn)換命令
}
/////////////////////////////////////////////////////////
/*====================================
函數(shù)功能:將溫度值讀出來并轉(zhuǎn)化為顯示數(shù)組
入口參數(shù):
說 明 :調(diào)用 ReadByte();
=====================================*/
void tmp(void)
{
float dis;
// uchar tempbuf;
dsreset();
delay(1);
tmpwrite(0xcc);
tmpwrite(0xbe);
temp_l=ReadByte(); //低位在前
temp_h=ReadByte(); //高位在后
flag1=temp_h&0xf8;
if(flag1)
{
temp_h=~temp_h;
if(temp_l==0)temp_h++; //若低8位全為0且溫度為負,取補時就要向高位進1
temp_l=~temp_l+1;
}
dis=(temp_h*256+temp_l)/16;
// dis=25.34; //調(diào)試用
if(dis<10)
{
dispbuf[0]=0;
dispbuf[1]=(uchar)dis;
}
else
{
dispbuf[0]=(uchar)dis/10;
dispbuf[1]=(uchar)dis%10;
}
// dispbuf[2]=(uchar)(dis*10)%10;
// dispbuf[3]=(uchar)dis*100%10;
}
/*====================================
函數(shù)功能:將顯示數(shù)組里的數(shù)顯示
入口參數(shù):無
說 明 :本例中只顯示一片DS18B20中的溫度
=====================================*/
void dis(void)
{
uchar i;
for(i=0;i<2;i++) /*輸送顯示數(shù)據(jù)*/
{
LED=0xff; //去段碼
P2 = ledbit[i]; //LED位選能
delay(3);
// if((i==0)&&(dispbuf[0]==0))
// LED=0x00; /*去掉最前面的0,更符合閱讀習(xí)慣*/
/*保證有效數(shù)可靠顯示,使其符合習(xí)慣*/
//
LED=ledcode[dispbuf[i]]; //送段碼
}
delay(5);
}
/*====================================
函數(shù)功能:主函數(shù) 將溫度從DS18B20讀出來并且顯示
入口參數(shù):
說 明 :
=====================================*/
main()
{
LED=0xff;
P2=0x00;
do{
tmpchange(); /*啟動溫度轉(zhuǎn)換*/
delay(10); /*等待轉(zhuǎn)換結(jié)束,可不用,會對顯示產(chǎn)生影響*/
tmp(); /*讀取溫度轉(zhuǎn)換結(jié)果*/
dis(); /*溫度顯示和報警*/
}while(1);
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -