甲;#include<reg51.h>
#define uint unsigned int
#define uchar unsigned char
sbit s1=P1^1;
uchar a=0;
void delay(uint z) //延时函数
{
uint x,y;
for(x=100;x>0;x–)
for(y=z;y>0;y–);
}
void key()
{
if(s10)
{
delay(5);
if(s10)
{
while(!s1);
a++;
if(a>9) a=0;
}
}
}
void init()
{
TMOD=0x20; //设置T1工作方式为方式2
TH1=0xfd; //给定时器高位装初值
TL1=0xfd; //给定时器低位装初值
TR1=1; //开定时器
SM0=0; //设置串口通讯方式为方式1
SM1=1;
EA=1; //开总中断
ES=1; //开串口中断
}
void main()
{
init();
while(1)
{
key();
ES=0;
SBUF=a;
while(!TI);
TI=0;
ES=1;
}
}
乙:
#include<reg51