下位机程序
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
基于VB的单片机温度数据采集与传输系统设计 下位机程序
2011-01-06 17:46 158人阅读 评论(0) 收藏 举报
/****************************************************/
#include
#include "intrins.h"
#include "1602.h"
#include "DS18B20.h"
#include "AT.h"
#define uint unsigned int
#define uchar unsigned char
sbit speaker=P2^3;
sbit P1_0=P1^0; /*P1_0~P1_3键盘*/
sbit P1_1=P1^1;
sbit P1_2=P1^2;
sbit P1_3=P1^3;
uchar i,w_num=0;
uchar n=0;
uchar a=0x87;
uchar ii=0;
uint temp,save_low,save_high;
uchar warn_mark=1,key_warn=0;//1默认启动
char zhifu[17]="T: Link:N*",low[8]="L_temp*",high[9]="H_temp*"; //*结束标志
uchar code ditab[16]={0x00,0x01,0x01,0x02,0x03,0x03,0x04,0x04,0x05,0x06,0x06,0x07,0x08,0x08,0x09,0x09}; //温度小数位表值
uchar data temp_data[2]={0x00,0x00}; //温度数据存储
uchar display[5]={0x30,0x30,0x30,0x30,0x30},save[2]={0xD0,0x07},save_h[2]={0x90,0xfc}; //温度LCD编码
uchar sendtemp[6]={0x41,0x00,0x00,0x00,0x2e,0x00};
uchar lowtemp[5]={0x30,0x32,0x35,0x2e,0x30} ,hightemp[5]={0x30,0x32,0x37,0x2e,0x30}; //正常温度范围
/************延时函数*************/
/*********************初始化函数*********************/
void Init(void)
{
Write_Instruction(0x38);
Delay(20);
Write_Instruction(0x0c);
Delay(20);
Write_Instruction(0x06);
Delay(20);
Cls();
TMOD=0X20;
TH1=0XF3;TL1=0XF3;
SCON=0X50;
TR1=1;
//EA=1;
//ES=1;
speaker=1;//报警声默认关闭
P1=0x0f;
AT24C64_R(save,0x00,2);
save_low=save[1];
save_low=save_low<<8;
save_low=save_low|save[0];
AT24C64_R(lowtemp,0x03,5);
AT24C64_R(hightemp,0x09,5);
AT24C64_R(save_h,0x10,2);
save_high=save_h[1];
save_high=save_high<<8;
save_high=save_high|save_h[0];
}
/****************读出温度函数************************/
read_temp()
{
ow_reset(); //总线复位
Delay(200);
write_byte(0xcc); //发命令
write_byte(0x44); //发转换命令
ow_reset();
Delay(1);
write_byte(0xcc); //发命令
write_byte(0xbe);
temp_data[0]=read_byte(); //读温度值的低字节
temp_data[1]=read_byte(); //读温度值的高字节
temp=temp_data[1];
temp<<=8;
temp=temp|temp_data[0]; // 两字节合成一个整型变量。
return temp; //返回温度值
}
/****************温度值编码**************************/
void work_temp(uint tem)
{
n=0;
if((tem<6348&&save_low<6348&&tem
{
save_low=tem;
save[0]=save_low;//&0x00ff;
save[1]=(save_low>>8);//&0x00ff;
AT24C64_W(save,0x00,2);
}
if((tem<6348&&save_high<6348&&tem>save_high)||(tem<6348&&save_high>6348)||(tem>6348&&save_high>6348&&tem>save_high))
{
save_high=tem;
save_h[0]=save_high;//&0x00ff;
save_h[1]=(save_high>>8);//&0x00ff;
AT24C64_W(sa
ve_h,0x10,2);
}
if(tem>6348) // 温度值正负判断
{tem=65536-tem;n=1;} // 负温度求补码,标志位置1
display[4]=tem&0x0f; // 取小数部分的值
display[0]=ditab[display[4]]; // 存入小数部分显示值
display[4]=tem>>4; // 取中间八位,即整数部分的值
display[3]=display[4]/100; // 取百位数据暂存
display[1]=display[4]%100; // 取后两位数据暂存
display[2]=display[1]/10; // 取十位数据暂存
display[1]=display[1]%10;
/******************符号位显示判断**************************/
display[3]=display[3]+0x30;
display[2]=display[2]+0x30;
display[1]=display[1]+0x30;
display[0]=display[0]+0x30;
if(display[3]==0x30)
{
display[3]=0x20; //最高位为0时不显示
if(display[2]==0x30)
{
display[2]=0x20; //次高位为0时不显示
}
}
if(n)
{display[3]=0x2d;} //负温度时最高位显示"-"
}
/****************显示温度函数************************/
void showtemp()
{
work_temp(read_temp()); //处理温度数据
Write_Instruction(0x82);
Write_Data(display[3]);
Write_Data(display[2]);
Write_Data(display[1]);
Write_Data(0X2E);
Write_Data(display[0]);
Write_Data(0xdf);
Write_Data(0x43);
}
/****************显示报警温度范围********************/
void show_high_low()
{
Write_Instruction(0xc7);
Write_Data(lowtemp[0]);
Write_Data(lowtemp[1]);
Write_Data(lowtemp[2]);
Write_Data(0X2E);
Write_Data(lowtemp[4]);
Write_Instruction(0x87);
Write_Data(hightemp[0]);
Write_Data(hightemp[1]);
Write_Data(hightemp[2]);
Write_Data(0X2E);
Write_Data(hightemp[4]);
if(warn_mark==0)
Write_String(0xcd," ON*");
else
Write_String(0xcd,"OFF*");
}
/*************温度是否超出范围判断及显示*************/
void show_warn_extent()
{
uint display4,display3,high4,low4,low3,high3;
if(display[3]==0x20)
display[3]=0x30;
if(display[2]==0x20)
display[2]=0x30;
high3=hightemp[1]*100+hightemp[2]*10+hightemp[4];
high4=hightemp[0]*1000+high3;
low3=lowtemp[1]*100+lowtemp[2]*10+lowtemp[4];
low4=lowtemp[0]*1000+low3;
display3=display[2]*100+display[1]*10+display[0];
display4=display[3]*1000+display3;
if(hightemp[0]!=0x2d&&lowtemp[0]!=0x2d&&display[3]!=0x2d
&&high4>=display4&&display4>=low4)
Write_String(0xCC,"WELL*");
else if(hightemp[0]!=0x2d&&lowtemp[0]==0x2d&&display[3]!=0x2d
&&high4>=display4)
Write_String(0xCC,"WELL*");
else if(hightemp[0]!=0x2d&&lowtemp[0]==0x2d&&display[3]==0x2d
&&low3>=display3)
Write_String(0xCC,"WELL*");
else if(hightemp[0]==0x2d&&lowtemp[0]==0x2d&&display[3]==0x2d
&&high3<=display3&&display3<=low3)
Write_String(0xCC,"WELL*");
else
{
Write_String(0xCC,"WARN*");
if(warn_mark==1)
speaker=0;
else
speaker=1;
}
}
void show_limits()
{
uchar j;
Write_Inst
ruction(0xc0);
for(j=0;j<5;j++)
Write_Data(lowtemp[j]);
Write_Data(0x7e);
for(j=0;j<5;j++)
Write_Data(hightemp[j]);
}
void send_temp()
{
i=0;
sendtemp[1]=display[3];
sendtemp[2]=display[2];
sendtemp[3]=display[1];
sendtemp[5]=display[0];
while(i<6)
{
SBUF=sendtemp[i];
while(!TI);
TI=0;
i++;
}
}
/****************报警温度调节************************/
void change_warn_temp()
{
uint high_temp,low_temp;
Delay(200);
while(!P1_0);
Cls();
Write_String(0x80,high);
Write_String(0xc0,low);
show_high_low();
while(1)
{
Write_Instruction(a+ii);
Write_Instruction(0x0f);
if(P1_1==0)
{ Delay(200);
while(!P1_1);
if(a==0x87)
{
ii++;
if(ii==3)
ii++;
if(ii==5)
{
ii=0;
a=0xc7;continue;
}
}
if(a==0xc7)
{
ii++;
if(ii==3)
ii++;
if(ii==5)
ii=7;
if(ii==8)
{
ii=0;a=0x87;continue;
}
}
}
if(P1_2==0)
{ Delay(200);
while(!P1_2);
if(a==0x87)
{
hightemp[ii]++;
if(hightemp[0]==0x32)
hightemp[0]=0x2d;
if(hightemp[0]==0x2e)
hightemp[0]=0x30;
if(hightemp[0]==0x2d&&hightemp[1]==0x36)
hightemp[1]=0x30;
if(hightemp[0]==0x31&&hightemp[1]==0x33)
hightemp[1]=0x30;
if(hightemp[0]==0x2d&&hightemp[1]==0x35&&hightemp[2]==0x36)
hightemp[2]=0x30;
if(hightemp[ii]==0x3a)
hightemp[ii]=0x30;
}
if(a==0xc7)
{
if(ii==7&&warn_mark==1)
{warn_mark=0;key_warn=1; }
else if(ii==7&&warn_mark==0)
{ warn_mark=1;key_warn=0;}
else
{
warn_mark=1;
lowtemp[ii]++;
if(lowtemp[0]==0x32)
lowtemp[0]=0x2d;
if(lowtemp[0]==0x2e)
lowtemp[0]=0x30;
if(lowtemp[0]==0x2d&&lowtemp[1]==0x36)
lowtemp[1]=0x30;
if(lowtemp[0]==0x31&&lowtemp[1]==0x33)
lowtemp[1]=0x30;
if(lowtemp[0]==0x2d&&lowtemp[1]==0x35&&lowtemp[2]==0x36)
lowtemp[2]=0x30;
if(lowtemp[ii]==0x3a)
lowtemp[ii]=0x30;
if(hightemp[0]==0x2d)
{
lowtemp[0]=0x2d;
if(lowtemp[1]
lowtemp[1]=hightemp[1];
}
if(lowtemp[2]
lowtemp[2]=hightemp[2];
}
if(lowtemp[4]
lowtemp[4]=hightemp[4];
}
}
if(hightemp[0]!=0x2d)
{ if(lowtemp[0]>=0x30)
{
if(lowtemp[0]>hightemp[0])
{
lowtemp[0]=0x2d;
}
if(lowtemp[1]>hightemp[1]&&lowtemp[0]==hightemp[0])
{
lowtemp[1]=0x30;
}
if(lowtemp[2]>hightemp[2]&&lowtemp[1]==hightemp[1]&&lowtemp[0]==hightemp[0])
{
lowtemp[2]=0x30;
}
if(lowtemp[4]>hightemp[4]&&lowtemp[2]==hightemp[2]&&lowtemp[1]==hightemp[1]&&lowtemp[0]==hightem
p[0])
{
lowtemp[4]=0x30;
}
}
}
}
}
show_high_low();
}
if(P1_0==0)
{ Delay(200);
while(!P1_0);
if(hightemp[0]>=0x30)
{
high_temp=hightemp[0]*1000+hightemp[1]*100+hightemp[2]*10+hightemp[4];
low_temp=lowtemp[0]*1000+lowtemp[1]*100+lowtemp[2]*10+lowtemp[4];
if(low_temp>high_temp)
{
lowtemp[0]=hightemp[0];
lowtemp[1]=hightemp[1];
lowtemp[2]=hightemp[2];
lowtemp[4]=hightemp[4];
}
}
else
{
lowtemp[0]=hightemp[0];
high_temp=hightemp[1]*100+hightemp[2]*10+hightemp[4];
low_temp=lowtemp[1]*100+lowtemp[2]*10+lowtemp[4];
if(high_temp>low_temp)
{
lowtemp[1]=hightemp[1];
lowtemp[2]=hightemp[2];
lowtemp[4]=hightemp[4];
}
}
Cls();
AT24C64_W(lowtemp,0x03,5);
AT24C64_W(hightemp,0x09,5);
if(warn_mark==1)
{
SBUF=0X41;
while(!TI);
TI=0;
SBUF=0X4F;
while(!TI);
TI=0;
}
else
{
SBUF=0X41;
while(!TI);
TI=0;
SBUF=0X4E;
while(!TI);
TI=0;
}
ii=0;a=0x87;
Write_Instruction(0x0c);
Write_String(0x80, zhifu);
show_limits();
break;
}
}
}
void warn_start()
{ Delay(200);
while(!P1_1);
warn_mark=1;
SBUF=0X41;
while(!TI);
TI=0;
SBUF=0X4f;
while(!TI);
TI=0;
}
void warm_stop()
{ Delay(200);
while(!P1_1);
warn_mark=0;
SBUF=0X41;
while(!TI);
TI=0;
SBUF=0X4E;
while(!TI);
TI=0;
}
void data_handle_or_send()
{
uchar receive;
RI=0;
Write_Instruction(0x8f);
Write_Data(0x59);
receive=SBUF;
if(receive==65)
{
send_temp();
}
else if(receive==79)
{
warn_mark=1;
}
else if(receive==78)
{
warn_mark=0;
}
else
{
Write_Instruction(0x8f);
Write_Data(0x4e);
}
}
void show_save_high_low()
{
Write_String(0x80,"The Low :*");
work_temp(save_low); //处理温度数据
//Write_Instruction(0x80);
Write_Data(display[3]);
Write_Data(display[2]);
Write_Data(display[1]);
Write_Data(0X2E);
Write_Data(display[0]);
Write_Data(0xdf);
Write_Data(0x43);
Write_String(0xc0,"The High:*");
work_temp(save_high); //处理温度数据
Write_Data(display[3]);
Write_Data(display[2]);
Write_Data(display[1]);
Write_Data(0X2E);
Write_Data(display[0]);
Write_Data(0xdf);
Write_Data(0x43);
}
void re_save_high_low()
{
save[0]=0xD0;
save[1]=0x07;
AT24C64_W(save,0x00,2);
save_low=save[1];
save_low=save_low<<8;
save_low=save_low|save[0];
save_h[0]=0x90;
save_h[1]=0xfc;
AT24C64_W(save_h,0x10,2);
save_high=save_h[1];
save_high=save_high<<8;
save_high=save_high|save_h[0];
}
/***********************主函数***********************/
void main(void)
{
Init();//
初始化
Write_String(0x80, "Please wait.....*");
ow_reset(); //开机先转换一次
write_byte(0xcc); //Skip ROM 跳过读序列号
write_byte(0x44); //发转换命令
Delay(50000);
Delay(50000);
Write_String(0x80, zhifu);
show_limits();
while(1)
{
showtemp();
show_warn_extent();
if(P1_0==0)
change_warn_temp();
if(P1_1==0)
{
if(warn_mark==1)
warm_stop();
else
warn_start();
}
if(RI)
{
data_handle_or_send();
}
if(P1_2==0)
{
Delay(300);while(!P1_2);
show_save_high_low();
while(P1_2);
Delay(300);while(!P1_2);
Cls();
Write_String(0x80, zhifu);
show_limits();
SBUF=0X41;
while(!TI);
TI=0;
SBUF=0X4F;
while(!TI);
TI=0;
}
if(P1_3==0)
{
Delay(300);while(!P1_3);
re_save_high_low();
}
}
}
//void int4()interrupt 4
//{
//data_handle_or_send();
//}