本帖最后由 lkk135 于 2013-12-8 23:31 编辑
我在做一个基于AT89S52单片机可燃气体检测报警器,电路已经焊接好了,但是程序一直实现不了,能手动报警或取消,但是不会自己检测到气体报警,MQ-2是在工作的,而数码管不管传感器是否检测到气体,它浓度显示也一直是9,就是不知程序哪里错了,求大神指教阿![](static/image/smiley/comcom/9.gif) 课题后天就要交了。
w w w.d o c 8 8.c o m/p-609272479630.html这是课题的网址,请黏贴到地址栏,把空格去掉。
程序:
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
uint num,aa,bb,adval;
uint table[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
sbit wr=P3^0;
sbit rd=P3^1;
sbit csad=P3^2;
sbit led=P2^0;
sbit beep=P2^1;
sbit key0=P3^6;
sbit key1=P3^7;
void init();
void start();
void read();
void delay(uint z)
{
uint t1,y;
for(t1=z;t1>0;t1--)
for(y=110;y>0;y--);
}
void main()
{
init();
while(1)
{
start();
delay(200);
read();
P1=0xff;
adval=P1;
num=adval/12;
if(num>9) num=9;
P0=table[num];
if(num>=2)
TR0=1;
if(num<2)
{
TR0=0;
beep=0;
led=1;
}
if(key0==0)
{
delay(20);
if(key0==0)
{
TR2=1;
while(!key0);
}
}
if(key1==0)
{
delay(20);
if(key1==0)
{
TR2=0;
beep=0;
led=1;
while(!key1);
}
}
}
}
void init()
{
csad=0;
P2=0x01;
TMOD=0x21;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
//TL2=(65536-50000)%256;
//ET2=1;
ET0=1;
EA=1;
}
void start()
{
wr=1;
_nop_();
wr=0;
_nop_();
wr=1;
}
void read()
{
P1=0xff;
rd=1;
_nop_();
rd=0;
_nop_();
adval=P1;
rd=1;
}
void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
aa++;
if(aa==6)
{
aa=0;
led=~led;
beep=~beep;
}
}
void timer2() interrupt 5
{
TF2=0;
TH2=(65536-50000)/256;
TL2=(65536-50000)%256;
bb++;
if(bb==10)
{
bb=0;
led=~led;
beep=~beep;
}
}
初步怀疑是这里出错了:
num=adval/12;
if(num>9) num=9;
P0=table[num];
但是不知道怎么改。
求助啊~~~~~~![](static/image/smiley/comcom/9.gif) ![](static/image/smiley/comcom/9.gif) ![](static/image/smiley/comcom/9.gif)
这是电路图
|