#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char //定义数据类型
#define uint unsigned int
bit flag; //标志位
/**************1602显示字符***************/
uchar code table[]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};
//1602显示数组
uchar code tab[]="** GAS ALARM **"; //1602显示字符
uchar code table1[]=" NOW:";
uchar code table2[]=" %";
/**************1602控制端口管脚定义***************/
sbit lcdrs=P3^0;
sbit lcdrw=P3^1;
sbit lcden=P3^2;
sbit start=P3^4;
/**************AD0809控制端口定义***************/
sbit eoc=P3^3;
sbit oe=P3^5;
sbit CLK=P3^6;
/**************其他变量***************/
uchar shi, ge, shi1, bai1, num;
uint temp;
uchar count; //蜂鸣器常量
/**************系统延时函数***************/
void delay(uint z)
{
uint x, y;
for(x=z; x>0; x--)
for(y=110; y>0; y--);
}
/**************蜂鸣器延时(500HZ)***************/
void dely500(void)
{
unsigned char i;
for(i=250; i>0; i--)
{
_nop_();
}
}
/**************1602命令函数***************/
void write _com(uchar com)
{
lcdrs=0;
P1=com;
delay(1);
lcden=1;
delay(1);
lcden=0;
}
/**************1602数据函数***************/
void write _data(uchar date)
{
lcdrs=1;
P1=date;
delay(1);
lcden=1;
delay(1);
lcden=0;
}
/**************系统初始化***************/
void init()
{
oe=0;
lcden=0;
lcdrw=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80);
temp=0;
TMOD=0x01;
TH0=(65536-500)/256;
TL0=(65536-500)%256;
EA=1;
ET0=1;
TR0=1;
}
/**************1602显示函数***************/
void voltage(uchar ge,uchar shi,uchar bai1)
{
shi=temp/1000;
ge=temp%1000/100;
shi1=temp%100/10;
bai1=temp%10;
write_com(0x80);
for(num=0;
num<16;num++)
{
write_data(tab[num]);
}
write_com(0xc0);
for(num=0;num<6;num++)
{
write_data(table1[num]);
}
write_com(0xc7);
write_data(table[shi]);
delay(1);
write_com(0xc8);
write_data(table[ge]);
delay(1);
write_com(0xc9);
write_data('.');
delay(1);
write_com(0xca);
write_data(table[shi1]);
delay(1);
write_com(0xcb);
write_data(table[bai1]);
delay(1);
write_com(0xcc);
for(num=0;num<2;num++)
{
write_data(table2[num]);
}
/***********AD转换控制程序**********/
start=0;
start=1;
start=0;
while(!eoc);
oe=1;
temp=P2;
oe=0;
temp=(temp*2*1*98.039216/100); //AD采样换算
/*****蜂鸣器报警程序*******/
/**可燃气体爆炸浓度:5%--16%,本设计设置为5%--15%**/
if(temp>=200)
{
for(count=200;count>0;count--)
{
P0_7=~P0_7;
dely500();
}
for(count=200;count>0;count--)
{
P0_7=~P0_7;
dely500();
dely500();
}
}
}
/**************中断***************/
void timer0() interrupt 1
{
TH0=(65536-500)/256;
TL0=(65536-500)%256;
CLK=~CLK; //CLK按位取反,代替方波脉冲
}
void main()
{
init();
while(1)
{
voltage(ge,shi,bai1);
}
}