甲机程序: #include <reg52.h>#define uint unsigned int
#define uchar unsigned char
sbit LED1 = P0^0;
sbit LED2 = P0^3;
sbit K1 = P1^0;
void Delay(uint x)
{
uchar i;
while(x--)
{
for(i=0;i<120;i++);
}
}
void putc_to_SerialPort(uchar c)
{
SBUF = c;
while(TI == 0);
TI = 0;
}
void main()
{
uchar Operation_NO = 0;
SCON = 0x40;
TMOD = 0x20;
PCON = 0x00;
TH1 = 0xfd;
TL1 = 0xfd;
TI = 0;
TR1 = 1;
while(1)
{
if(K1 == 0)
{
while(K1==0);
Operation_NO=(Operation_NO+1)%4;
}
switch(Operation_NO)
{
case 0:
LED1=LED2=1; break;
case 1:
putc_to_SerialPort('A');
LED1=~LED1;LED2=1;break;
case 2:
putc_to_SerialPort('B');
LED2=~LED2;LED1=1;break;
case 3:
putc_to_SerialPort('C');
LED1=~LED1;LED2=LED1;break;
}
Delay(10);
}
}
********************************************************************************************************************乙机程序:
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar a,b;
uint num;
//sfr T2MOD = 0xC9; //reg52.h头文件中未定义,故此处单独添加sfr定义
sbit SW1 = P1^5;
sbit SW2 = P1^6;
sbit SW3 = P1^7;
sbit LED= P3^4;
uchar code table[]={0xC0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,};
//-----------------------------------------------------------------
// 主程序
//-----------------------------------------------------------------
void delayms(int z)
{
int x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void display(uchar shi,uchar ge)
{
P2=table[shi];
delayms(5);
P0=table[ge];
delayms(5);
}
uint read(uchar THX,uchar TLX)
{
uchar tl,th1,th2;
uint val;
while(1)
{
th1=THX;
tl=TLX;
th2=THX;
if(th1==th2)
break;
}
val=th1*256+tl;
return val;
}
void main()
{
SCON =0x50;
PCON =0x00;
TMOD =0x25;
TH1 =0xfd;
TL1 =0xfd;
TH0 =0X00;
TL0=0x00;
TR0 =1;
TR1 = 1;
RI = 0;
while(1)
{
if(RI)
{
RI = 0;
switch(SBUF)
{
case 'A': LED=~LED;delayms(100);break;
case 'B': LED=1;delayms(100);break;
case 'C': LED=0;delayms(100);break;
}
}
else
LED=1;
delayms(100);
num=read(TH0,TL0);
if(num>99)
{
num=0;
TH0=0;
TL0=0;
}
a=num/10;
b=num%10;
display(a,b);
}
}
|