按题目要求,继电器闭合后,应该在5s之后断开,但是我的代码闭合之后就一直闭合,不断开了,这是为什么?
主函数main.c
#include <STC15F2K60S2.H>
#include "hardware.h"
#include "iic.h"
#include "ds1302.h"
#include "intrins.h"
unsigned char Write_ds1302_addr[]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c};
unsigned char Read_ds1302_addr[]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};
unsigned char Timer[]={0x00,0x59,0x06,0x09,0x05,0x04,0x24};
sbit Tx=P1^0;
sbit Rx=P1^1;
unsigned int distance=0;
unsigned char timerh=0;
//------------------------------------------------------------------------
void write_time()
{
char i;
Write_Ds1302_Byte(0x8e,0x00);
for(i=0;i<7;i++)
{
Write_Ds1302_Byte(Write_ds1302_addr[i],Timer[i]);
}
Write_Ds1302_Byte(0x8e,0x80);
}
//------------------------------------------------------------------------
void Read_time()
{
char i;
for(i=0;i<7;i++)
{
Timer[i]=Read_Ds1302_Byte(Read_ds1302_addr[i]);
}
}
//------------------------------------------------------------------------
void SMG_display()
{
SMG_buf[0]=Timer[2]/16;
SMG_buf[1]=Timer[2]%16;
SMG_buf[2]=17;
SMG_buf[3]=Timer[1]/16;
SMG_buf[4]=Timer[1]%16;
SMG_buf[5]=17;
SMG_buf[6]=Timer[0]/16;
SMG_buf[7]=Timer[0]%16;
}
//------------------------------------------------------------------------
void Delay14us() //@12.000MHz
{
unsigned char i;
_nop_();
_nop_();
i = 39;
while (--i);
}
//------------------------------------------------------------------------
void sendwave()
{
unsigned char i=0;
for(i=0;i<8;i++)
{
Tx=1;
Delay14us();
Tx=0;
Delay14us();
}
}
//------------------------------------------------------------------------
unsigned int csb_read()
{
unsigned int time=0;
CMOD=0x00;
CH=CL=0;
sendwave();
CR=1;
while((Rx==1)&&(CF==0));
CR=0;
if(CF==1)
{
CF=0;
return 999;
}
else
{
time=(CH<<8)|CL;
return (time*0.017)+3;
}
}
//------------------------------------------------------------------------
void judge_door()
{
/*if(Timer[2]>=0x07 && Timer[2]<0x22)
{*/
if(distance<30)
{
bz_open=1;
}
else if(distance>=30)
{
bz_open=0;
}
//}
}
//------------------------------------------------------------------------
void open_door()
{
if(bz_open==1)
{
ws_buf=0x10;
flag=1;
//timers=Timer[2]/16*10+Timer[2]%16;
if(bz_5s==1)
{
bz_5s=0;
ws_buf=0x00;
bz_open=0;
timer5s=0;
}
}
if(bz_open==0)
{
ws_buf=0x00;
bz_5s=0;
timer5s=0;
}
}
//------------------------------------------------------------------------
void main()
{
Timer1Init();
write_time();
while(1)
{
SMG_display();
if(timer200ms>=200)
{
timer200ms=0;
Read_time();
distance=csb_read();
}
judge_door();
open_door();
}
}
hardware.c
#include<STC15F2K60S2.H>
#include"hardware.h"
/************* 本地常量声明 **************/
unsigned char code t_display[]={ //标准字库
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71,
//black - H J K L N o P U t G Q r M y
0x00,0x40,0x76,0x1E,0x70,0x38,0x37,0x5C,0x73,0x3E,0x78,0x3d,0x67,0x50,0x37,0x6e,
0xBF,0x86,0xDB,0xCF,0xE6,0xED,0xFD,0x87,0xFF,0xEF,0x46}; //0. 1. 2. 3. 4. 5. 6. 7. 8. 9. -1
unsigned char code T_COM[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}; //位码
unsigned char led_buf=0xff;
unsigned char ws_buf=0x00;
unsigned char SMG_buf[]={16,16,16,16,16,16,16,16};
unsigned int timer10ms=0;
unsigned int timer200ms=0;
unsigned int timer5s=0;
unsigned char key_stat=0;
unsigned char key_value=0;
unsigned char key_values[4][4]=
{
{12,13,14,15},
{8,9,10,11},
{4,5,6,7},
{0,1,2,3},
};
bit bz_5s=0;
bit bz_open=0;//开关门标志
//----------------------------------------------------------------------
void Timer1Init() //1毫秒@12.000MHz
{
AUXR |= 0x40; //定时器时钟1T模式
TMOD &= 0x0F; //设置定时器模式
TL1 = 0x20; //设置定时初值
TH1 = 0xD1; //设置定时初值
TF1 = 0; //清除TF1标志
TR1 = 1; //定时器1开始计时
EA=1;
ET1=1;
}
//---------------------------------------------------------------------
void SelectHC573(unsigned char channel)
{
switch(channel)
{
case 4:
P2=(P2 & 0x1f)|0x80;
break;
case 5:
P2=(P2 & 0x1f)|0xa0;
break;
case 6:
P2=(P2 & 0x1f)|0xc0;
break;
case 7:
P2=(P2 & 0x1f)|0xe0;
break;
case 0:
P2=(P2 & 0x1f)|0x00;
break;
}
}
//---------------------------------------------------------------------
void led_output()
{
P0=led_buf;
SelectHC573(4);
SelectHC573(0);
}
//---------------------------------------------------------------------
void ws_output()
{
P0=ws_buf;
SelectHC573(5);
SelectHC573(0);
}
//---------------------------------------------------------------------
void SMG_output()
{
static unsigned char SMG_com=0;
P0=0x00;
SelectHC573(6);
SelectHC573(0);
P0=~t_display[SMG_buf[SMG_com]];
SelectHC573(7);
SelectHC573(0);
P0=T_COM[SMG_com];
SelectHC573(6);
SelectHC573(0);
if(++SMG_com>=8)
{
SMG_com=0;
}
}
//---------------------------------------------------------------------
void ServiceTimer()interrupt 3
{
SMG_output();
led_output();
ws_output();
timer10ms++;
timer200ms++;
if(bz_open==1)
{
timer5s++;
if(timer5s>=5000)
{
bz_5s=1;
timer5s=0;
}
}
}
//----------------------------------------------------------------------
void Scankey()
{
unsigned char R,C;
P3=0x0f;
P4=0x00;
if(~P3&0x0f)
{
if(key_stat<255)
key_stat++;
if(P33==0)
R=0;
if(P32==0)
R=1;
if(P31==0)
R=2;
if(P30==0)
R=3;
P3=0xf0;
P4=0xff;
if(P44==0)
C=0;
if(P42==0)
C=1;
if(P35==0)
C=2;
if(P34==0)
C=3;
key_value=key_values[R][C];
}
else
{
key_stat=0;
key_value=0;
}
}