想请教个串口调试问题,用上位机控制单片机五个IO口的状态,同时通过单品机读取另外五个状态
#include
#include
sfr AUXR = 0x8e;
sfr AUXR1 =0xA2;
sfr BRT = 0x9c;
sbit MCU_Run_Led = P1^3;
/* key1=P3^4;key2=P3^5;key3=P3^7;key4=P1^0;
key5=P1^1;key6=P1^2;key7=P1^4;key8=P1^5;
key9=P1^6;key10=P1^7;
*/
sbit DO_1=P3^4;
sbit DO_2=P3^5;
sbit DO_3=P3^7;
sbit DO_4=P1^0;
sbit DO_5=P1^1;
sbit DI_6=P1^2;
sbit DI_7=P1^4;
sbit DI_8=P1^5;
sbit DI_9=P1^6;
sbit DI_10=P1^7;
unsigned char process=6;
unsigned char com_buf[4];
unsigned char sta_buf[4];
unsigned char com_index=0;
int count=0;
#define timeh 76 // 10ms-220:0;50ms-76:0
#define timel 0
#define RELOAD_COUNT 0xfb //9600bps 8 n 1
void serial_port_initial()
{
SCON = 0x50; !设定串口工作方式
BRT = RELOAD_COUNT;
AUXR = 0x11;
TI=0;
RI=0;
ES=1;
EA=1;
}
void send_UART(unsigned char value)
{
ES=0;
TI=0;
SBUF=value;
ES=1;
while(TI==0);
}
void UART_Interrup_Receive() interrupt 4
{
unsigned char ch;
if(RI==1)
{
ch=SBUF;
if(ch==0x1A)
{
com_index=0;
process=0;
}
if(ch==0xf0)
{
com_index=0;
process=1;
}
if(ch==0x1D&&com_index==3&&com_buf[0]==0x1A)
{
process=2;
}
if(com_index>3) com_index=0;
if(process==0)
{
com_buf[com_index]=ch;
com_index++;
}
RI=0;
}
else
{
TI=0;
}
}
void t0_init(void)
{
AUXR=0x00;
TMOD=0x01; !16位计数器
TH0=219;
TL0=255;
EA=1;
ET0=1;
TR0=1;
}
void t0_int (void) interrupt 1
{
TR0=0;
TH0=219;
TL0=255;
count++;
if(count==50)
{
if(MCU_Run_Led==0)
{
MCU_Run_Led=1;
//send_UART(0x01);
}
else
{
MCU_Run_Led=0;
// send_UART(0x02);
}
count=0;
}
TR0=1;
}
void delay(int count)
{
int i,j;
for(i=0;i
for(j=0;j<1000;j++);
}
/**** timer0 application sub ****/
void main()
{
unsigned char flag=0;
int i=0;
DO_1=1;DO_2=1;DO_3=1;DO_4=1;DO_5=1;
DO_6=1;DO_7=1;DO_8=1;DO_9=1;DO_10=1;
MCU_Run_Led=0;
process=0;
t0_init();
serial_port_initial();
while(1)
{
if(process==2)
{
if(com_buf[1]>=0x01&&com_buf[1]<=0x0a)
{
EA=0;
flag=com_buf[2];
// if(flag==1) flag=1;
// else if(flag==0) flag=0;
switch(com_buf[1])
{
case 0x01:
DO_1=flag;
break;
case 0x02:
DO_2=flag;
break;
case 0x03:
DO_3=flag;
break;
case 0x04:
DO_4=flag;
break;
case 0x05:
DO_5=flag;
break;
/* case 6:
DO_6=flag;
break;
case 7:
DO_7=flag;
break;
case 8:
DO_8=flag;
break;*/
default:
break;
}
EA=1;
send_UART(0x1A);
}
else
{
EA=1;
send_UART(0x1b);
}
process=0;
}
elseif(process==1)
{
}
}
}