|
#include
#include
#define INBUF_LEN 5 //数据长度
unsigned char ch;
bit read_flag= 0 ;
unsigned char temp[]={0,1,2,3,0};
unsigned char *str=temp;
void init_serialcom( void )
{
SCON = 0x50 ; //SCON: serail mode 1, 8-bit UART, enable ucvr
TMOD |= 0x20 ; //TMOD: timer 1, mode 2, 8-bit reload
PCON &= 0xef ; //SMOD=0;
TH1 = 0xFD ; //Baud:19200 fosc=11.0592MHz
IE |= 0x90 ; //Enable Serial Interrupt
TR1 = 1 ; // timer 1 run
TI=1;
}
//向串口发送一个字符
void send_char_com( unsigned char ch)
{
//unsigned char point;
// for(point=0;point<20;point++)
//{SBUF=str[point];
SBUF=ch;
while (TI== 0);
TI= 0 ;
}
//向串口发送一个字符串,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 serial () interrupt 4 using 3
{ unsigned char ch;
if (RI)
{
RI = 0 ;
ch=SBUF;
read_flag= 1 ; //就置位取数标志
}
}
main()
{
init_serialcom(); //初始化串口
while ( 1 )
{
if (read_flag) //如果取数标志已置位,就将读到的数从串口发出
{
read_flag= 0 ; //取数标志清0
// send_char_com(ch);
send_string_com(str,5);
}
}
}
//现希望串口调试助手发来数组0 1 2 3 0,单片机接收后返回该数组(或另一个数组),请问程序应如何修改才能正确,现在发送和接受不一致...
|
|