帮我看看龚建伟老师写的那个单片机串口通信的程序:单片机C51串口中断接收和发送测试例程(含通信协议的实现)。
有没有什么问题啊?我看了半天,不明白checksum ^= ch;到底是怎么意思?这个程序编译是成功的,但是把烧写到单片机里,用串口调试助手就没反应?\帮我看看啊?急!!!
#include <reg51.h> #include <string.h>
#define INBUF_LEN 4 file://数据长度
unsigned char inbuf1[INBUF_LEN]; unsigned char checksum,count3; bit read_flag=0;
void init_serialcomm(void) { SCON = 0x50; file://SCON: serail mode 1, 8-bit UART, enable ucvr TMOD |= 0x20; file://TMOD: timer 1, mode 2, 8-bit reload PCON |= 0x80; file://SMOD=1; TH1 = 0xF4; file://Baud:4800 fosc=11.0592MHz IE |= 0x90; file://Enable Serial Interrupt TR1 = 1; // timer 1 run // TI=1; }
file://向串口发送一个字符 void send_char_com(unsigned char ch) { SBUF=ch; while(TI==0); TI=0; }
file://向串口发送一个字符串,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); }
file://串口接收中断函数 void serial () interrupt 4 using 3 { if(RI) { unsigned char ch; RI = 0; ch=SBUF; if(ch>127) { count3=0; inbuf1[count3]=ch; checksum= ch-128; } else { count3++; inbuf1[count3]=ch; checksum ^= ch; if( (count3==(INBUF_LEN-1)) && (!checksum) ) { read_flag=1; file://如果串口接收的数据达到INBUF_LEN个,且校验没错, file://就置位取数标志 } } } }
main() { init_serialcomm(); file://初始化串口 while(1) { if(read_flag) file://如果取数标志已置位,就将读到的数从串口发出 { read_flag=0; file://取数标志清0 send_string_com(inbuf1,INBUF_LEN); } }
}