1.上电后向上位机发送:Welcome to my system!
#include "reg52.h"
sfr AUXR=0x8e;
unsigned char urdat;
void Init_Hc138(int n) //译码器选择
{
switch (n)
{
case 0:
P2=(P2 & 0x1f) | 0x00;
break; //什么都不选
case 4:
P2=(P2 & 0x1f) | 0x80;
break; //让Y4输出低电平
case 5:
P2=(P2 & 0x1f) | 0xa0;
break; //让Y5输出低电平
case 6:
P2=(P2 & 0x1f) | 0xc0;
break; //让Y6输出低电平
case 7:
P2=(P2 & 0x1f) | 0xe0;
break; //让Y7输出低电平
}
}
void Output_P0(unsigned char channel,unsigned char dat)
{
Init_Hc138(channel);
P0=dat; //P0口输出数据
}
void delay(int n)
{
while(n--)
{}
}
//========================定时器0
void init_uart()
{
TMOD=0x20;
TH1=0xfd;
TL1=0xfd;
TR1=1;
SCON=0x50;
AUXR=0x00;
ES=1;
EA=1;
}
void sendbyte(unsigned char dat)
{
SBUF=dat;
while(TI==0);
TI=0;
}
void sendstring(unsigned char *str)
{
while(*str!='\0')
sendbyte(*str++);
}
void service_uart() interrupt 4
{
if(RI==1)
{
RI=0;
urdat=SBUF;
}
}
//========================
void working()
{
if (urdat!=0x00)
{
switch(urdat & 0xf0)
{
case 0xa0:
Output_P0(4,~(0x0f&urdat));
urdat=0x00;
break;
case 0xb0:
Output_P0(4,~((0x0f&urdat)<<4));
urdat=0x00;
break;
case 0xc0:
sendstring("The system is running!\r\n");
urdat=0x00;
Output_P0(4,0xff);
break;
default:
sendstring("ERROR!\r\n");
urdat=0x00;
Output_P0(4,0xff);
}
}
}
void Initsystem() //关闭无关设备
{
Output_P0(4,0xff);
Output_P0(5,0x00);
Output_P0(0,0x00);
}
void main()
{
Initsystem();
init_uart();
sendstring("Welcome to my system!\r\n");
while(1)
{
working();
}
}