#include<reg52.h>
#include<7279.h>
#define uchar unsigned char
#define uint unsigned int
//---------------------------------------------
sbit DQ=P1^5;
//DS18b20数据端
bit tx_flag;
unsigned char c;
//温度低 四位 小数位
unsigned int tmpr = 0;
//---------------------------------------------------
uchar data_buff[10]={0,1,2,3,4,5,6,7,8,9};
uchar rx_buff[5];
uchar data_send[8]={0,1,2,3,4,5,6,7};//数据发送缓冲区
uchar data_receive[5]={4,3,2,1,0};
//数据接收缓冲区
//-----------------------------------------------------
/***************************************************************/
unsigned char seg[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
unsigned char flag,tem0,tem1,count,num,temp0,temp1;
int f;
//频率计数
void int_scom(void) //初始化:
//串口初始化
{
TMOD = 0X25;
//定时器1工作方式1
SCON = 0X50;
//8位UART,波特率为T1溢出率
TH1 = 0XFA;
//波特率设定
TL1 = 0XFA;
TR1 = 1;
//开启定时器1
ES = 1;
//串口中断打开
ET1 = 0;
EA = 1;
//总中断打开
}
//------------------------------------------------------
void T2_init(void)
//定时器初始化
{
RCAP2H=(65536-62500)/256;
//重装载计数器赋初值
RCAP2L=(65536-62500)%256;
TH2=65536/256;
//T2赋初值
TH2=65536%256;
TMOD=0x25;
//工作方式设定
TL0=0x00;
TH0=0x00;
ET2=1;
//开定时器2中断
EA=1;
//开总中断
TR2=1;
//开启定时器,并设置为自动重装载模式
ET0=1;
TR0=1;
}
//-------------------------------------------------------
void delay(unsigned int i)
//延时程序
{
while(i--);
}
/*18b20初始化函数*/
void Init_DS18B20(void)
{
unsigned char x=0;
DQ = 1;
//DQ复位
delay(8);
//稍做延时
DQ = 0;
//单片机将DQ拉低
delay(80); //精确延时 大于 480us
DQ = 1;
//拉高总线
delay(10);
x=DQ;
//稍做延时后 如果x=0则初始化成功 x=1则初始化失败
delay(5);
}
//--------------------------------------------------------
//
//
//
//--------------------------------------------------------
/*DS18B20读一个字节函数*/
unsigned char ReadOneChar(void)
{
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(5);
}
return(dat);
}
/*DS18B20写一个字节函数*/
void WriteOneChar(unsigned char dat)
{
unsigned char i=0;
for (i=8; i>0; i--)
{
DQ = 0;
DQ = dat&0x01;
delay(5);
DQ = 1;
dat>>=1;
}
delay(5);
}
/*DS18B20读取温度函数*/
unsigned char ReadTemperature(void)
{
unsigned char a=0;
unsigned char b=0;
unsigned char t=0;
Init_DS18B20();
WriteOneChar(0xCC); // 跳过读序号列号的操作
WriteOneChar(0x44); // 启动温度转换
delay(200);
Init_DS18B20();
WriteOneChar(0xCC); //跳过读序号列号的操作
WriteOneChar(0xBE); //读取温度寄存器等(共可读9个寄存器) 前两个就是温度
a=ReadOneChar();
//a为温度的低8位
b=ReadOneChar();
//b为温度的高4位兼4位符号位
c=a;
c=c&0x0f;
//小数位的数值
b<<=4;
b+=(a&0xf0)>>4;
t=b;
return(t);
}
//--------------------------------------------------------
//
//
//
//
//-频率换算成湿度----------------------------------------
void tran(void)
{
f=tem1;
f=(f<<8)|tem0;
f=f*16;
//这里f的值是最终读到的频率,不同频率对于不同相对湿度。
if((5423<=f)&&(f<=6652))
//相对湿度在有效范围内(0%--100%)
{if((6534<f)&&(f<= 6652))
{temp0=0;temp1=(6652-f)*10/118;}
if((6418<f)&&(f<=6534))
{temp0=1;temp1=(6534-f)*10/116;}
if((6303<f)&&(f<=6418))
{temp0=2;temp1=(6418-f)*10/115;}
if((6188<f)&&(f<=6303))
{temp0=3;temp1=(6303-f)*10/115;}
if((6071<f)&&(f<=6188))
{temp0=4;temp1=(6188-f)*10/117;}
if((5952<f)&&(f<=6071))
{temp0=5;temp1=(6071-f)*10/119;}
if((5829<f)&&(f<=5952))
{temp0=6;temp1=(5952-f)*10/123;}
if((5701<f)&&(f<=5829))
{temp0=7;temp1=(5829-f)*10/128;}
if((5566<f)&&(f<=5701))
{temp0=8; temp1=(5701-f)*10/135;}
if((5423<f)&&(f<=5566))
{temp0=9; temp1=(5566-f)*10/143;}
}
else
{
temp0 = 0; temp1 = 0;
}
write7279(DECODE0+5,temp1);
write7279(DECODE0+6,temp0);
write7279(UNDECODE+4,0x37);
}
//-------------------------------------------------------
void send_comm_1(uchar tmp)//串口发送函数:
{
TI = 0;
SBUF = tmp;
while(!TI);
TI = 0;
}
void send_comm (uchar *tmp)//发送函数:
{
uchar i;
ET0 = 0;
ET2 = 0;
for(i = 0; i < 8; i++)
{
TI = 0;
SBUF = *(tmp++);
while(!TI);
TI = 0;
//
write7279(0xc8+i,*tmp);
//0X90为最右边数码管
}
ET0 = 1;
ET2 = 1;
}
void Receive(void) interrupt 4 using 2//接收中断:
{
uchar i = 0;
if(RI == 1)
{
rx_buff[i++] = SBUF;
if(i > 5)
{
i = 0;
}
RI = 0;
}
}
//-------------------------------------------------------------
//
//
//
//
//----------------------------------------------------------------
void Timer0() interrupt 1 //T0中断函数
{
EA =0;
TR2=0;
TR0=0;
TH2=65536/256;
TH1=65536%256;
TL0=0x00;
TH0=0x00;
TR2=1;
TR0=1;
EA=1;
}
//---------------------------------------------------------------
void Timer2() interrupt 5
//调用定时器2,自动重装载模式
{
TF2=0;//定时器2的中断标志要软件清0
EA =0;
TR0=0;
tem0=TL0;
tem1=TH0;
TL0=0x00;
TH0=0x00;
num++;
//计数标志自加1
if(num==16)
//判断是否到5s
{
flag=1;
count++;
if(count==10) count=0;
num=0;
}
TR0=1;
EA=1;
}
//--------------------------------------------------------------
void chuli_data(void)
{
data_send[0]= tmpr%10;
data_send[1]= tmpr%100/10;
data_send[2]= tmpr%1000/100;
data_send[3]= 0;
data_send[4]= 0x37;
data_send[5]= temp1;
data_send[6]= temp0;
data_send[7]= 1;
}
//--------------------------------------------------------------
void main(void)
{
int_scom();
int_7279();
T2_init();
while(1)
{
tmpr = ReadTemperature();
tmpr = tmpr*10+c*6/10;
if(flag==1)
{
flag = 0;
tran();
Disp(tmpr);
chuli_data();
send_comm(data_send);
}
// Disp(tmpr);
if(!key)
{
switch(Key_Scan())
{
case 0:
{
break;
}
case 1:
{
send_comm(data_receive);
break;
}
case 2:break;
case 3:break;
case 4:break;
case 5:break;
case 6:break;
case 7:break;
case 8:break;
}
}
}
}