/**********************单片机与PC机通讯例程************************
* STC89C52RC+
* 编写:颜
* 晶振:11.0592MHZ
******************************************************************/
#include
#define jingzhen 11059200UL /*使用22.1184M晶体*/
#define botelv 9600UL /*波特率定义为9600*/
volatile unsigned char sending;
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
uchar code LED_L[] = {0xfe, 0xfd, 0xfb, 0xf7, 0xef, 0xdf, 0xbf, 0x7f};
uchar code LED_R[] = {0x7f, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd, 0xfe};
uchar code LED_1[] = {0xe7, 0xe7, 0xdb, 0xdb, 0xbd, 0xbd, 0x7e, 0x7e};
uchar code LED_2[] = {0x7e, 0x7e, 0xbd, 0xbd, 0xdb, 0xdb, 0xe7, 0xe7};
#define u16 unsigned int
#define u8 unsigned char
//流水灯
u16 LED_Delay = 0;
u8 KeyFlag = 0;
u8 LEDState = 0;
u8 Data = 0;
u8 Flag = 0;
void delay(unsigned char i)
{
unsigned char j,k;
for(j=i;j>0;j--)
for(k=90;k>0;k--);
}
void init(void) //串口初始化
{
EA=0; //暂时关闭中断
TMOD&=0x01; //定时器0工作在16位模式
TMOD|=0x20; //定时器1工作在模式2,自动重装模式
SCON=0x50; //串口工作在模式1
TH1=256-jingzhen/(botelv*12*16); //计算定时器重装值
TL1=256-jingzhen/(botelv*12*16);
PCON|=0x80; //串口波特率加倍
ES=1; //串行中断允许
TR1=1; //启动定时器1
REN=1; //允许接收
//定时器0
TH0 = (65535 - 5000) >> 8; //写入定时器0初值
TL0 = (65535 - 5000);
ET0 = 1; //打开定时器0中断
TR0 = 1; //启动定时器0
EA=1; //允许中断
}
void main()
{
init();
while(1)
{
if(Flag != 0)
{
Flag = 0;
KeyFlag = 0;
switch(Data) //补充用户所需代码
{
case 0x00:P0 = 0xfe;break; //第一个按键
case 0x01:P0 = 0xfd;break;
case 0x02:P0 = 0xfb;break;
case 0x03:P0 = 0xf7;break;
case 0x04:P0 = 0xef;break;
case 0x05:P0 = 0xdf;break;
case 0x06:P0 = 0xbf;break;
case 0x07:P0 = 0x7f;break;
case 0x08:KeyFlag = 1;LEDState = 0;break;
case 0x09:KeyFlag = 2;LEDState = 0;break;
case 0x0A:KeyFlag = 3;break;
case 0x0B:KeyFlag = 4;break;
case 0x0C:break;
case 0x0D:break;
case 0x0E:break;
case 0x0F:break;
}
}
if(KeyFlag != 0)
{
switch(LEDState)
{
case 0:
case 2:
case 4:
case 6:
case 8:
case 10:
case 12:
case 14:
LED_Delay = 0;
switch(KeyFlag)
{
case 1:P0 = LED_L[LEDState/2];break; //流水灯左模式
case 2:P0 = LED_R[LEDState/2];break; //流水灯右模式
case 3:P0 = LED_1[LEDState/2];break;
case 4:P0 = LED_2[LEDState/2];break;
}
LEDState++;
break;
default:
if(LED_Delay >= 50)
{
LED_Delay = 0;
LEDState++;
if(LEDState == 16)
{
LEDState = 0;
}
}
break;
}
}
}
}
void uart(void) interrupt 4 //串口发送中断
{
if(RI) //收到数据
{
RI=0; //清中断请求
……………………
…………限于本文篇幅 余下代码请从51黑下载附件…………