|
#include"reg51.h"
//unsigned char code duan[]={0xb7,0x05,0x73,0x67,0xC5,0xE6,0xF6,0x07,0xF7,0xE7};//共阴段码
unsigned char code duan[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//共阴段码
sbit DUAL = P3^0;
sbit LED3_yellow = P1^2;
sbit LED2_green = P1^1;
sbit LED1_red = P1^0;
sbit LED6_yellow = P1^5;
sbit LED4_red = P1^3;
sbit LED5_green = P1^4;
sbit WEI1 = P2^0;
sbit WEI2 = P2^1;
sbit WEI3 = P2^2;
sbit WEI4 = P2^3;
unsigned char count = 0;
unsigned char i = 0;
unsigned char key_num = 0;
unsigned char mode = 0;
void delay(unsigned char ms)
{
while(ms--);
}
void display(unsigned char grva,unsigned char kwgrva)
{
DUAL = 1;
P0=0x00;
P0 = duan[grva/10];
DUAL = 0;
WEI1=0;
WEI2=1;
WEI3=1;
WEI4=1;
delay(10);
DUAL = 1;
P0=0x00;
P0 = duan[grva%10];
DUAL = 0;
WEI1=1;
WEI2=0;
WEI3=1;
WEI4=1;
delay(10);
DUAL = 1;
P0=0x00;
P0 = duan[kwgrva/10];
DUAL = 0;
WEI1=1;
WEI2=1;
WEI3=0;
WEI4=1;
delay(10);
DUAL = 1;
P0=0x00;
P0 = duan[kwgrva%10];
DUAL = 0;
WEI1=1;
WEI2=1;
WEI3=1;
WEI4=0;
delay(10);
if(kwgrva == 0 || grva == 0)
{
mode++;
count = 0;
if(mode==4)
mode=0;
}
}
void partten_1(void) //南北通行
{
TR0 = 0;
LED1_red = 0;
LED2_green = 1;
LED3_yellow = 0;
LED4_red = 1;
LED5_green= 0;
LED6_yellow = 0;
display(0,0);
}
void partten_2(void) //东西通行
{
TR0 = 0;
LED1_red = 1;
LED2_green = 0;
LED3_yellow = 0;
LED4_red = 0;
LED5_green= 1;
LED6_yellow = 0;
display(0,0);
}
void partten_3(void)
{
TR0 = 1;
if(mode == 0)
{
LED1_red = 0;
LED2_green = 1;
LED3_yellow = 0;
LED4_red = 1;
LED5_green= 0;
LED6_yellow = 0;
display(25-count,30-count);
}
else if(mode == 1)
{
LED1_red = 0;
LED2_green = 0;
LED3_yellow = 0;
LED4_red = 1;
LED5_green= 0;
LED6_yellow = 0;
display(5-count,5-count);
}
else if(mode == 2)
{
LED1_red = 1;
LED2_green = 0;
LED3_yellow = 0;
LED4_red = 0;
LED5_green= 1;
LED6_yellow = 0;
display(30-count,25-count);
}
else if(mode == 3)
{
LED1_red = 0;
LED2_green = 0;
LED3_yellow = 0;
LED4_red = 1;
LED5_green= 0;
LED6_yellow = 0;
display(5-count,5-count);
}
}
void main()
{
TMOD |= 0x01;
TH0 = (65536-10000)/256; //10MS
TL0 = (65536-10000)%256;
ET0 = 1;
EX0 = 1;
IT0 = 1;
EA = 1;
TR0 = 1;
while(1)
{
if(key_num == 0)partten_3();
if(key_num == 1)partten_1();
if (key_num == 2)partten_2();
}
}
void INT_0() interrupt 0
{
key_num++;
if(key_num == 3)
{
key_num = 0;
count = 0;
mode = 3;
}
}
void TIMER0() interrupt 1
{
TH0 = (65536-10000)/256; //10MS
TL0 = (65536-10000)%256;
i++;
if(i == 100) //1ms
{
i = 0;
count++;
}
}
|
-
仿真图
|