玩轉(zhuǎn)單片機(jī)之五--串口多數(shù)據(jù)收發(fā)
#include
#define uchar unsigned char //byte
#define uint unsigned int //word
sbit led1=P0^0;
sbit led2=P0^1;
#define INBUF_LEN 8 //數(shù)據(jù)長度
unsigned char inbuf[INBUF_LEN]; //存儲數(shù)據(jù)數(shù)組
unsigned char count3; //計數(shù)器
//向串口發(fā)送一個字符
void send_char_com( unsigned char ch)
{
SBUF=ch;
while (TI== 0 );
TI= 0 ;
}
//向串口發(fā)送一個字符串,strlen為該字符串長度
void send_string_com( unsigned char *str, unsigned int strlen)
{
unsigned int k= 0 ;
do
{
send_char_com(*(str + k));
k++;
} while (k < strlen);
}
void main(void)
{ //串口初始化
TMOD=0x20;
PCON=0x00;
SCON=0x50;
TL1=0xfd;
TH1=0xfd;
TR1=1;
//計數(shù)
count3= 0 ;
while(1)//動態(tài)現(xiàn)實是接收多個數(shù)據(jù)
{
if (RI)
{
//接收數(shù)據(jù)
unsigned char ch;
RI = 0 ;
ch=SBUF;
inbuf[count3]=ch; //存儲數(shù)據(jù)
count3++; //計數(shù)器增加
if ( (count3==INBUF_LEN ))
{ count3= 0 ; //計數(shù)器清零
send_string_com(&inbuf,INBUF_LEN);//向串口發(fā)送一個字符串
}
}
//串口數(shù)據(jù)的檢驗,可以刪除
if(inbuf[3]==0x11 )
{led1=0;}
}
}
評論