{
redA=!redA,greenA=!greenA,yellowA =!yellowA; redB=!redB,greenB=!greenB,yellowB =!yellowB; DXZ=!DXZ, NBZ=!NBZ; }
}
if(count2==100)
{
count2=0;
second2--; //second2 减1
redA=!redA,greenA=!greenA,yellowA =!yellowA;
//临时通行状态
redB=!redB,greenB=!greenB,yellowB =!yellowB; DXZ=!DXZ, NBZ=!NBZ; }
if(second2==0) { TR1=0,TR0=1,second=C; //关闭定时器1,启动定时器0;返回second值
if(flag2==0) //依据flag2值,判断返回状态
redA=0,greenA=1,yellowA =0,redB=1,greenB=0,yellowB =0,DXZ=0,NBZ=1; else
redA=1,greenA=0,yellowA =0,redB=0,greenB=1,yellowB =0,DXZ=1,NBZ=0; } }
/*************紧急模式 (外部中断0)********* *************/ void INT0_() interrupt 0 // 外部中断0 是0号 {
if(!INT0) {
DelayMs(10);//在此处可以添加去抖动程序,防止按键抖动造成错误 if(!INT0)
while(!INT0);//等待按键释放
{ redA=1,greenA=0,yellowA =0; //紧急状态,红灯全亮 redB=1,greenB=0,yellowB =0; DXZ=0, NBZ=0; Write_Max7219(DIG_0,0); //紧急状态,倒计时清零
Write_Max7219(DIG_1,0); Write_Max7219(DIG_2,0); Write_Max7219(DIG_3,0); Write_Max7219(DIG_4,0);
Write_Max7219(DIG_5,0); Write_Max7219(DIG_6,0); Write_Max7219(DIG_7,0);
16
alarm(); //紧急状态,蜂鸣器报警
} } }
/*****************临时通行 (外部中断1)********** *********/ void INT1_() interrupt 2 // 外部中断1 是2号 {
if(!INT1) {
DelayMs(10); //在此处可以添加去抖动程序,防止按键抖动造成错误 if(!INT1)
while(!INT1); //等待按键释放
{ C=second; //保存second值
second2=15; //临时通行time设值
Init_Timer_interrupt(); TR1=1,TR0=0; //启动定时器1,关闭定时器0 redA=1,greenA=1,yellowA =1; //临时通行,状态设定
redB=1,greenB=1,yellowB =1; DXZ=1, NBZ=1; }
} }
/******************夜间模式******************/ void night_mode() {
Init_Max7219();
//max7219初始化
//夜间模式,状态设定
redA=0,greenA=1,yellowA=!yellowA; redB=1,greenB=0,yellowB=!yellowA; DXZ=0, NBZ=1; buzzer=0;
Init_Timer_interrupt(); TR0=1; while(1)
//开定时器0
//循环扫描,等待中断
{
Write_Max7219(DIG_0,second/10); Write_Max7219(DIG_1,second); Write_Max7219(DIG_2,second/10); Write_Max7219(DIG_3,second); Write_Max7219(DIG_4,second/10); Write_Max7219(DIG_5,second); Write_Max7219(DIG_6,second/10); Write_Max7219(DIG_7,second); yellowA =!yellowA;
17
yellowB =!yellowB;
DelayMs(100); } }
/************普通模式*****************/ void normal_mode()
{
Init_Max7219(); redA=0,greenA=1,yellowA=0; redB=1,greenB=0,yellowB=0; DXZ=0, NBZ=1; buzzer=0;
Init_Timer_interrupt(); TR0=1; while(1) {
//开定时器0
//max7219初始化 //普通模式,状态设定
//循环扫描,等待中断
Write_Max7219(DIG_0,second/10); Write_Max7219(DIG_1,second); Write_Max7219(DIG_2,second/10);
Write_Max7219(DIG_3,second); Write_Max7219(DIG_4,second/10); Write_Max7219(DIG_5,second);
Write_Max7219(DIG_6,second/10); Write_Max7219(DIG_7,second); }
}
/****************报警延时*******************/ void DelayUs2x(unsigned char t) {
while(--t);
}
void DelayMs(unsigned char t) { while(t--)
{
DelayUs2x(245);
DelayUs2x(245);
} }
/*******************报警程序**************************/ void alarm() {
unsigned int i; while(1)
18
{
for(i=0;i<500;i++) {
DelayUs2x(200); DelayUs2x(200);
buzzer=!buzzer; }
for(i=0;i<500;i++) {
DelayMs(1); }
buzzer=!buzzer; } }
Max7219程序:
#include
#define DECODE_MODE 0x09 //max7219参数设置 #define INTENSITY 0x0A #define SCAN_LIMIT 0x0B #define SHUT_DOWN 0x0C #define DISPLAY_TEST 0x0F
sbit CLK = P1^0; sbit LOAD = P1^1;
sbit DIN = P1^2;
void Write_Max7219_byte(unsigned char temp) //向max7219写一字节 {
unsigned char i; for (i=0;i<8;i++) {
CLK=0;
DIN=(bit)(temp&0x80); temp<<=1; CLK=1; } }
void Write_Max7219(unsigned char address,unsigned char dat) //向max7219写入地址,数据
{
19
LOAD=0;
Write_Max7219_byte(address); Write_Max7219_byte(dat); LOAD=1; }
/************MAX7219初始化***************/ void Init_Max7219(void)
{
Write_Max7219(SHUT_DOWN, 0x01); Write_Max7219(DISPLAY_TEST, 0x00); Write_Max7219(DECODE_MODE, 0xff); Write_Max7219(SCAN_LIMIT, 0x07); Write_Max7219(INTENSITY, 0x04); }
键盘扫描程序:
#include
#define P P2
extern unsigned char keyscan() { unsigned char temp,key=0xff; P=0xfe; //将第一行线置为高电平,其余行线全部为高电平 temp=P; //读取P3口当前状态值赋给临时变量temp,用于后面计算
temp=temp&0xf0; //将temp与0xf0进行与运算,然后将结果赋给temp. if(temp!=0xf0) //如果temp不等于0xf0,说明有键按下 {
delay(10); temp=P;
//延时去抖
//重新读一次P3口数据
temp=temp&0xf0; if(temp!=0xf0)
{
temp=P; //重新读取一次P3口数据 switch(temp) //判断那个按键被按下 {
case 0xee:
key=1; break;
case 0xde: key=2; break;
20