📄 max232.c
字号:
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
#define TR 0
uchar idata buf[10]={0x20,1,2,3,4,5,6,7,8,9};
uchar pf;
void init(void)
{TMOD=0x20; //mode 1,counter 1
TH1=0xFD; //set baud rate
TL1=0xFD;
PCON=0x00;
TR1=1; //start T/C1
SCON=0x50; //mode1
}
void send(uchar idata *d)
{uint i;
uchar j,v;
P1=0Xfb;
do{RI=0;
SBUF=0xaa; //send handshaking message
while (TI==0); //sending
TI=0; //software clear
while(RI==0); //waiting for ACK
RI=0;
v=SBUF; //software clear
}while (v!=0xbb);//B is not ready
do{
pf=0; //clear the sum
for(i=0;i<10;i++){
j=*(d+i);
SBUF=j ; //send the data
pf+=*(d+i); //sum
while(TI==0);TI=0;
}
SBUF=pf; //send sum
while(TI==0);TI=0;
while(RI==0);RI=0; //waiting for B to ack
}while (SBUF!=0); //if there is wrong, send again
}
void receive(uchar idata *d)
{uchar i;
do{
while(RI==0);RI=0;
} while((SBUF^0xaa)!=0); //check if A is sending
SBUF=0xbb; //send ACK
while(TI==0);TI=0;
while(1){
pf=0;
for(i=0;i<16;i++){
while(RI==0);RI=0;
d[i]=SBUF; //receive data
pf+=d[i]; //sum
}
while (RI==0);RI=0; //receive sum
if((SBUF^pf)==0){ //check sum
SBUF=0x00;break;} //if the two are equal, then send 00
else{
SBUF=0xff; //send FF,receive again
while(TI==0);TI=0;
}
}
}
void main(void){
init();
if(TR==0){
send(buf);
}
//else{
//receive(buf);
//}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -