需要从电脑串口接收字符串,并与1234比较,如果相同让led灯亮!!!!!!
#include "stc15.h"
#include <stdio.h>
#include <string.h>
#include <intrins.h>
#include <stdlib.h>
sbit led1= P3^2; //指示灯1
void UartInit(void) //115200bps@11.0592MHz
{
SCON = 0x50; //8位数据,可变波特率
AUXR |= 0x40; //定时器1时钟为Fosc,即1T
AUXR &= 0xFE; //串口1选择定时器1为波特率发生器
TMOD &= 0x0F; //设定定时器1为16位自动重装方式
TL1 = 0xE8; //设定定时初值
TH1 = 0xFF; //设定定时初值
ET1 = 0; //禁止定时器1中断
TR1 = 1; //启动定时器1
ES=1; //串行口中断允许
REN=1; //接收允许
EA=1; //总中断允许
led1=1;
}
void Delay5000ms() //@11.0592MHz
{
unsigned char i, j, k;
_nop_();
_nop_();
i = 211;
j = 30;
k = 11;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
#define uchar unsigned char
#define uint unsigned int
char mima[5] = {'1','2','3','4','\0'};
uchar table[5];
void SendByte(unsigned char dat)
{
SBUF = dat;
while(!TI);
TI = 0;
}
void send(uchar *dis)
{
while (*dis!='\0')
{
SBUF=*dis;
dis++;
while(!TI);
TI=0; //软件请发送中断
}
}
void main()
{
UartInit(); //初始化
while(table[5]!='\0')
{
send(table);
}
{int a=strcmp(table,mima);
if(a==0)
{led1=0;
Delay5000ms();
}
else
{led1=1;}
}
}
void Uart() interrupt 4 using 1
{
static int i=0;
if(i<4)
{
ES=0;
while(!RI); //查询接收标志
RI=0;
table[i]=SBUF; //创建一个4元素的数组
i++;
ES=1;
}
}