#include<reg52.h>#defineOPEN_DOOR DOOR = 1#defineCLOSE_DOOR DOOR = 0#defineNO_FREE_PARK {RED_LED=1;GREEN_LED=0;}#defineHAVE_FREE_PARK {RED_LED=1;GREEN_LED=0;}#defineSEG P0 sbitbaiw=P2^0;sbitshiw=P2^1; sbitgew=P2^2;sbitEXT_LINE = P3^0; //sbi
tiNT_LINE = P3^1; // sbitSW1 = P0^3; sbitDOOR = P0^2; //1 打开,0 关闭sbitRED_LED = P0^0;//空闲指示灯 1 亮表示无空闲车位sbitGREEN_LED = P0^0;//空闲指示灯 1 亮表示有空闲车位 unsignedchar total_num=255; //最大车位unsignedchar remain_num=255; //剩余车位unsignedchar dai,shi,ge;unsignedchar check_car(void);void delay_ms(unsigned int ms);voiddisplay(unsigned char va);unsignedchar seg7code[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; voidmain(void){ EX0=1;; EX1=1; EA=1;
while(1) { if(1==check_car()) //如果检测到car 出入 { OPEN_DOOR; //启动定时器的程序 TMOD=0X01; TH0=0XFF; TL0=0; EA=1; ET0=1; TR0=1; //end of"启动定时器的程序" } else { ;// do nothing } if(0==remain_num) { NO_FREE_PARK; } else { HAVE_FREE_PARK; } if(0==SW1) { display(remain_num); } else { display(total_num); } }}unsignedchar check_car(void){ unsigned char ext_1=0; unsigned char int_1=0; unsigned char ext_2=0; unsigned char int_2=0; unsigned char return_flag=0; EXT_LINE = 1; INT_LINE = 1; //设置为读 ext_1 = EXT_LINE; int_1 = INT_LINE;//读取状态 if((0==ext_1)&&(0==int_1))//没有车 { ; } else if((1==ext_1)&&(1==int_1))//车在门口,还没有出去 { ; } else if((1==ext_1)&&(0==int_1)) { delay_ms(1000);//延时 ext_2 = EXT_LINE; int_2 = INT_LINE;//读取状态 if((0==ext_1)&&(1==int_1)) //有车进入 { remain_num--; return_flag = 1; } else //异常 { ;//do nothing } } else // if((0==ext_1)&&(1==int_1)) { delay_ms(1000);//延时 ext_2 = EXT_LINE; int_2 = INT_LINE;//读取状态 if((1==ext_1)&&(0==int_1)) //有车出去 { remain_num++; return_flag= 1; } else //异常 { ; } } return return_flag;}void delay_ms(unsigned int ms) // Fosc=11.0592MHz{ unsigned char i; for(;ms>0;ms--) { for(i=0;i<124;i++) { ; } }} //中断 用于关门voidtime_0(void) interrupt 1{ unsigned char ext_1=0; unsigned char int_1=0; EXT_LINE = 1; INT_LINE = 1; //设置为读 ext_1 = EXT_LINE; int_1 = INT_LINE;//读取状态 if((0==ext_1)&&(0==int_1))//没有车 关门 停止计时 { CLOSE_DOOR; //关闭定时器的程序 ET0=0; TR0=0; //end of "关闭定时器的程序" } else //有车 继续定时检查 { TH0=0XFF; TL0=0; }} //增加键中断voidkey_0(void) interrupt 0{ total_num++; if(total_num<remain_num) { remain_num = total_num; }} //减少键中断voidkey_1(void) interrupt 2{ total_num--; if(total_num<remain_num) { remain_num = total_num; }} //显示程序voiddisplay(unsigned char va) { bai=va/100; //处理数据 shi=va%100/10; ge=va%10; gew=1; //数码管显示车位 shiw=1; baiw=1; SEG=seg7code[bai]; baiw=0; Delay_ms(2); baiw=1; SEG=seg7code[shi]; shiw=0; Delay_ms(2); shiw=1; SEG=seg7code[ge]&0x7f; gew=0; Delay_ms(2); gew=1;}
一周热门 更多>