我闲着没事又编了个交通灯的程序。
#include<reg52.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
sbit we = P2^7;
sbit du = P2^6;
sbit S3 = P3^1;
uint counter;
void delay(uint z)
{
uint x,y;
for(x = z;x > 0; x--)
for(y = 120;y > 0; y--);
}
uchar code leddata[]={
0x3F, //"0"
0x06, //"1"
0x5B, //"2"
0x4F, //"3"
0x66, //"4"
0x6D, //"5"
0x7D, //"6"
0x07, //"7"
0x7F, //"8"
0x6F, //"9"
0x77, //"A"
0x7C, //"B"
0x39, //"C"
0x5E, //"D"
0x79, //"E"
0x71, //"F"
0x76, //"H"
0x38, //"L"
0x37, //"n"
0x3E, //"u"
0x73, //"P"
0x5C, //"o"
0x40, //"-"
0x00, //熄灭
0x00 //自定义
};
void main()
{
uint a = 0;
TMOD = 0x10;
TH1 = 0x4b;
TL1 = 0xfe;
while(1)
{
if(S3 != 0)
{
delay(5);
if(S3 != 0)
{
TR1 = 1;
}
}
if(TF1 == 1)
{
TF1 = 0;
TH1 = 0x4b;
TL1 = 0xfe;
counter++;
}
du = 1;
P0 = leddata[a];
du = 0;
we = 1;
P0 = 0xf0;
we = 0;
delay(1);
if(counter == 20)
{
a++;
counter = 0;
}
if(a == 16)
a = 0;
if(S3 == 0)
{
delay(5);
if(S3 == 0)
TR1 = 0;
}
}
}