中级会员
- 积分
- 299
- 金钱
- 299
- 注册时间
- 2019-7-20
- 在线时间
- 108 小时
|
楼主 |
发表于 2019-7-25 20:16:15
|
显示全部楼层
#include<reg52.h>
#include"delay.h"
#include"lcd602.h"
/************************端口定义**************************/
sbit led1 = P1^3; //车位1指示灯
sbit led2 = P1^4; //车位2指示灯
sbit led3 = P1^5; //车位3指示灯
sbit hongwai1 = P1^0; //红外1
sbit hongwai2 = P1^1; //红外2
sbit hongwai3 = P1^2; //红外3
/*******************变量定义********************/
uchar flag1 = 0; //车位1状态标志
uchar flag2 = 0; //车位2状态标志
uchar flag3 = 0; //车位3状态标志
uchar flag_1 = 0; //车位1计费状态标志
uchar flag_2 = 0; //车位2计费状态标志
uchar flag_3 = 0; //车位3计费状态标志
uchar temp1 = 0; //定时器0定时1S变量
long int sec1 = 0; //车位1时间计时变量
uchar temp2 = 0; //定时器1定时1S变量
long int sec2 = 0; //车位2时间计时变量
uchar temp3 = 0; //定时器2定时1S变量
long int sec3 = 0; //车位3时间计时变量
/*******************定时器T0初始化********************/
void T0_init()
{
TMOD = TMOD | 0x01; //设T0为方式1
TH0 = (65535-50000)/256; //计数50000个
TL0 = (65535-50000)%256;
EA = 1; //开启总中断
ET0 = 1; //允许T0中断
//TR0 = 1; //启动
}
/*******************定时器T1初始化********************/
void T1_init()
{
TMOD = TMOD | 0x10; //设T1为方式1
TH1 = (65535-50000)/256; //计数50000个
TL1 = (65535-50000)%256;
EA = 1; //开启总中断
ET1 = 1; //允许T1中断
//TR1 = 1; //启动
}
///*******************T2-初始化********************/
void init_t2()
{
T2CON = 0x00; //工作模式
RCAP2H = (65535-50000)/256; //自动装载初值
RCAP2L = (65535-50000)%256; //
TH2 = RCAP2H;
TL2 = RCAP2L;
EA = 1; //开总中断
ET2 = 1; //T2中断
//TR2 = 1; //启动
}
/*******************主函数********************/
void main()
{
chushihua(); //LCD1602初始化
T0_init();
T1_init();
init_t2();
while(1)
{
/*检测车位的状态*/
if(hongwai1==1) //车位1没有占用
{
flag1 = 0;
led1 = 1;
write_zifu(1,0,'1');
write_zifu(1,1,':');
write_zifu(2,1,'N');
flag_1 = 0;
sec1 = 0;
}
else //车位1占用
{
flag1 = 1;
led1 = 0;
write_zifu(1,0,'1');
write_zifu(1,1,':');
write_zifu(2,1,'Y');
TR0 = 1; //启动
}
if(hongwai2==1) //车位2没有占用
{
flag2 = 0;
led2 = 1;
write_zifu(1,4,'2');
write_zifu(1,5,':');
write_zifu(2,4,'N');
flag_2 = 0;
sec2 = 0;
}
else //车位2占用
{
flag2 = 1;
led2 = 0;
write_zifu(1,4,'2');
write_zifu(1,5,':');
write_zifu(2,4,'Y');
TR1 = 1; //启动
}
if(hongwai3==1) //车位3没有占用
{
flag3 = 0;
led3 = 1;
write_zifu(1,8,'3');
write_zifu(1,9,':');
write_zifu(2,8,'N');
flag_3 = 0;
sec3 = 0;
}
else //车位3占用
{
flag3 = 1;
led3 = 0;
write_zifu(1,8,'3');
write_zifu(1,9,':');
write_zifu(2,8,'Y');
TR2 = 1; //启动
}
/*计算空车位和占用的车位*/
write_zifu(1,13,'P'); //显示占用的车位
write_num1(2,13,flag3+flag2+flag1); //显示占用的车位
write_zifu(1,15,'N'); //显示占用的车位
write_num1(2,15,3-flag3-flag2-flag1); //显示空车位
/*显示占用车位的费用*/
if(flag_1==0) write_num1(1,2,0); //显示占用的车位1费用
else write_num1(1,2,2); //显示占用的车位1费用
if(flag_2==0) write_num1(1,6,0); //显示占用的车位2费用
else write_num1(1,6,2); //显示占用的车位2费用
if(flag_3==0) write_num1(1,10,0); //显示占用的车位3费用
else write_num1(1,10,2); //显示占用的车位3费用
}
}
/**************************定时器T0中断服务函数************************************/
void T0_time() interrupt 1
{
TH0 = (65535-50000)/256; //计数50000个
TL0 = (65535-50000)%256;
temp1 += 1;
if(temp1>=20)
{
temp1 = 0;
sec1 += 1;
if(sec1>=5)
{
flag_1 = 1;
}
}
}
/**************************定时器T1中断服务函数************************************/
void T1_time() interrupt 3
{
TH1 = (65535-50000)/256; //计数50000个
TL1 = (65535-50000)%256;
temp2 += 1;
if(temp2>=20)
{
temp2 = 0;
sec2 += 1;
if(sec2>=5)
{
flag_2 = 1;
}
}
}
/*******************T2中断函数函数********************/
void T2_time() interrupt 5
{
TF2 = 0;
temp3 += 1;
if(temp3>=20)
{
temp3 = 0;
sec3 += 1;
if(sec3>=5)
{
flag_3 = 1;
}
}
}
|
|