12-串行接口的进阶应用
串行接口的进阶应用
由上可知:
应用高四位来判断控制灯光,所以应用高四位对比,将第四位置0进行对比用switch语句,
进行判断,将为A时,保留低四位,取反将高四位置1
将为B时,保留高四位,取反将低四位置1,向左移动4位
#include <REGX52.H>
sfr AUXR = 0x8E;
unsigned char command = 0x00;
void _74HC138(unsigned char n) {
switch(n) {
case 4:
P2 = (P2 & 0x1f) | 0x80;
break;
case 5:
P2 = (P2 & 0x1f) | 0xa0;
break;
case 6:
P2 = (P2 & 0x1f) | 0xc0;
break;
case 7:
P2 = (P2 & 0x1f) | 0xe0;
break;
}
}
void System_Init(void) {
_74HC138(5);
P0 = 0x00;
_74HC138(4);
P0 = 0xFF;
}
void UART_Init(void) {
TMOD = 0X20;
SCON = 0X50;
TL1= 0XFD;
TH1= 0XFD;
AUXR = 0x00;
TR1 = 1;
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 work() {
_74HC138(4);
if(command != 0) {
switch(command & 0xf0)
{// 1111 0000
case 0xA0:
P0 = 0XFF; //
P0 = (P0 | 0x0F) & (~command | 0xF0);
command = 0x00;
break;
case 0xB0:
P0 = 0XFF;
P0 = (P0 | 0XF0) & (~command << 4 | 0x0F);
command = 0x00;
break;
case 0xC0:
sendString("The System is running...");
command = 0x00;
break;
}
}
}
void main(void) {
System_Init();
UART_Init();
sendString("Wecome to xxx System...\t\n");
while(1) {
work();
}
}
void UART1_Routine(void) interrupt 4{
if(RI == 1) {
command = SBUF;
RI = 0;
}
}