甲机:
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar tab[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8, //共阳极LEDO~F的段码//
0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E};
uchar a,b;
sbit P20=P2^0;
void delayms(uint ms)
{
uint i;
while(ms--)
{
for(i=0;i<120;i++);
}
}
void int1() interrupt 2
{
delayms(10);
if(INT1==0)
{
a++;
if(a==9)
{a=0;}
}
}
void disp(void)
{
P2=0x01;
P0=tab[b/10];
delayms(1);
P2=0x02;
P0=tab[b%10];
delayms(1);
}
void send(uchar state)
{
SBUF=state; //将内容串行发送//
while(TI==0); //等待发送完//
TI=0; //发送完将TI复位//
}
void receive() interrupt 4
{
if(RI)
{
RI=0;
b=SBUF;
}
}
void INT_init(void) //INT0和INT1中断初始化//
{
EX1=1; //打开外部中断1//
IT1=1; //下降沿触发中断INT1//
EA=1; //全局中断允许//
PX1=1; //INT1中断优先//
}
void SCON_init(void) //串口初始化//
{
SCON=0x50; //串口工作在方式1//
TMOD=0x20; //T1工作在模式2//
PCON=0x00; //波特率不倍增//
TH1=0xFD; //波特率为9600//
TL1=0xFD;
TI=0; //启动TI//
TR1=1; //允许串行中断//
ES=1;
}
void main(void)
{
SCON_init();
INT_init();
while(1)
{
send(a);
disp();
}
}
乙机:
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
uchar a,b;
uchar discode1[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
void delayms(uint ms)
{
uint i;
while(ms--)
{
for(i=0;i<120;i++);
}
}
void int0() interrupt 0
{
delayms(10);
if(INT0==0)
{
if(a>=15)a=0;
}
}
void LED(void)
{
P0= ~ discode1[b];
}
void send(uchar state)
{
SBUF=state;
while(TI==0);
TI=0;
}
void serival(void)
{
b=SBUF;
while(RI==0);
RI=0;
}
void INT_init(void)
{
EX0=1;
IT0=1;
EA=1;
}
void SCON_init(void)
{
SCON=0x50;
TMOD=0x20;
PCON=0x00;
TH1=0xFD;
TL1=0xFD;
TI=0;
TR1=1;
ES=1;
}
void main(void)
{ int i;
i=a;
SCON_init();
INT_init();
while(1)
{
send(i);
serival();
LED();
if(INT0==0)
{
i++;
if(i>15)i=0;
while(INT0==0);
}
}
}
|