|
/****
函數(shù)名main.c*****/
#include <reg52.h>
#include "main.h"
#include "display.h"
#include "18b20.h"
#include "alarm.h"
//extern unsigned char idata flag; //定義外部變量
void main()
{
initalarm();
while(1)
{
SetHighTem();
alarm();
}
}
/*****函數(shù)名;display.c
數(shù)碼管顯示采集的溫度***/
#include "display.h"
#include "main.h"
unchar ; code table[]={ //共陰極數(shù)碼管顯示
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71,0x40,0x76,0x38};
unchar ; code table1[]={ //第三個數(shù)碼管有小數(shù)點(diǎn),所以不一樣
0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,
0xff,0xef,0xf7,0xfc,0xb9,0xde,0xf9,0xf1};
/***********
*數(shù)碼管顯示函數(shù)
*參數(shù):dat
*返回?zé)o
*************/
void diaplay(uint first,uint second,uint third,uint forth)
//顯示負(fù)溫度的函數(shù),第一個數(shù)碼管顯示‘-’
{
wela=0xfe;
dula=table[first];
dealy_ms(5);
dula=0x00;
wela=0xfd;
dula=table[second];
dealy_ms(5);
dula=0x00;
wela=0xfb;
dula=table1[third];
dealy_ms(5);
dula=0x00;
wela=0xf7;
dula=table1[forth];
dealy_ms(5);
dula=0x00;
}
/*****延時函數(shù)
*參數(shù):timer
*返回?zé)o
**************/
void delay_ms(uint timer)
{
uint i,j;
for(i=0;i<timer;i++)
for(j=0;j<110;j++)
}
#include "main.h"
unsigned char idata flag;
unchar show[4]={1,2,3,4};
void delay_us(uchar a)
{
while(--a);
}
//****初始化子程序
void init1820()
{
DQ=1;_nop_();
DQ=0; //拉低數(shù)據(jù)線
delay_us(125); //延時510us
delay_us(125);
DQ=1; //提升數(shù)據(jù)線
delay_us(15); //延時35us
while(DQ) //等待slave器件的ACK信號
{
_nop_();
}
delay_us(60); //延時125us
DQ=1; //提升 數(shù)據(jù)線,準(zhǔn)備數(shù)據(jù)傳輸
}
//******write*****
void write1820(uchar a)
{
uchar i;
for(i=0;i<8;i++)
{
if(a & 0x01) //低位在前
{
DQ=0; //結(jié)束recovery time
_nop_();_nop_();_nop_();
DQ=1; //發(fā)送數(shù)據(jù)
}
else
DQ=0; //結(jié)束recovery time
_nop_();_nop_();_nop_();
DQ=0;}
delay_us(30);
DQ=1;
_nop_(); //開始recovery time
a>>=1;
}
}
//**********read
unsigned char read1820(void)
{
unsigned char i;
unsigned char tmp=0;
DQ=1;_nop_(); //準(zhǔn)備讀
for(i=0;1<8;i++)
{
tmp>>=1; //低位先發(fā)
DQ=0; //讀初始化
_nop_();
DQ=1;
delay_us(2);
_nop_();
if(DQ)
tmp |=0x80;
delay_us(30);
DQ=1;_nop_();
}
return tmp;
}
uchar gettemp()
{
unsigned int tp;
init1820();
write1820(0xcc);
delay_ms(2);
write1820(0x44);
_nop_();
DQ=1;
delay_ms(250);delay_ms(250);delay_ms(250);
init1820();
write1820(0xcc);
write1820(0xbe);
show[0]=read1820();
show[1]=read1820();
init1820();
tp=show[1]*256+show[0];
flag=show[1]>>7;
if(flag==0){tp=tp>>4;}
if(flag==1){tp=tp>>4;tp=256-tp;}
//show[0]=(unsigned char)(tp/10);
//show[1]=(unsigned char)(tp% 10);
return tp;
}
#include "main.h"
#include "alarm.h"
#include "display.h"
#include "18b20.h"
extern unsigned char idata flag; //定義外部變量,溫度正負(fù)標(biāo)志
uint HNum=50;LNum=10;
uint Tem;
/**********
*顯示溫度函數(shù)
*參數(shù)無
*返回?zé)o
***********/
void ShowTem()
{
Tem=gettemp(); //讀取采集的溫度
if(flag==1)
display(16,(Tem% 100)/10,Tem% 10,(Tem* 10)/10);
if(flag==0)
display(Tem/100,(Tem% 100)/10,Tem% 10,(Tem* 10)/10);
}
/*************
*設(shè)置低溫溫度
*參數(shù)無
*返回?zé)o
************/
void SetLowTem()
{
while(1)
{
display(18,(LNum% 100)/10,LNum% 10,(LNum* 10)/10);
if(k2==0)
delay_ms(50); //按鍵消抖
if(k2==0)
{
while(!k2); //等待按鍵釋放
LNum++;
}
if(k3==0)
delay_ms(50);
if(k3==0)
{
while(!k3);
LNum--;
}
if(k4==0)
delay_ms(50);
if(k4==0)
{
while(!k4);
break;
}
}
}
/*******
*設(shè)置高溫溫度
*參數(shù)無
*返回?zé)o
************/
void SetHighTem()
{
HNum=50;
if(k1==0)
delay_ms(10); //按鍵消抖
if(k1==0)
{
while(!k1); //等待按鍵釋放
while(1)
{
display(17,(HNum% 100)/10,HNum% 10,(HNum* 10)/10);
if(k1==0)
delay_ms(50);
if(k1==0)
{
while(!k1);
break;
}
if(k2==0)
delay_ms(50);
if(k2==0)
{
while(!k2);
HNum++;
}
if(k3==0)
delay_ms(50);
if(k3==0)
{
while(!k3);
HNum--;
}
if(k4==0)
delay_ms(50);
if(k4==0)
{
while(!k4);
break;
}
}
}
ShowTem();
}
/***************
*LED蜂鳴器初始化
*參數(shù)無
*返回值無
*****************/
void initalarm()
{
led1=0;
led2=0;
led3=0;
buzzer=1;
}
/***************
*LED蜂鳴器控制
*參數(shù)無
*返回值無
*****************/
void alarm()
{
Tem=gettemp();
if( Tem > HNum)
{
led1=1;
led2=0;
led3=0;
buzzer=0;
}
else if( Tem< LNum)
{
led1=1;
led2=0;
led3=0;
buzzer=0;
}
else
{
led1=1;
led2=0;
led3=0;
buzzer=1;
}
}
|
-
44.PNG
(85.75 KB, 下載次數(shù): 26)
下載附件
2020-6-1 09:04 上傳
-
捕獲.PNG
(17.63 KB, 下載次數(shù): 31)
下載附件
2020-6-1 09:04 上傳
|