当前位置:网站首页>Proteus -- Serial Communication parity flag mode

Proteus -- Serial Communication parity flag mode

2022-07-06 06:53:00 Wonderful drifting of coal

1. Circuit diagram

2. Code

(1) Machine a sends 
#include<reg51.h>// Include access sfr Library function rug51、h
sbitP27=P2^7,// Define the driving end of parity marker lamp 
bit F=0;// Define the reply flag of machine B - He Qing 0
unsigned char i;// Define the serial number of sending data i( Global variables )
unsigned char code c[10]=(// Define the sending data array , And the assignment 
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0xf8,0xf8,0x80,0x90);//0~9 Gongyang share code 
void main(){
    // Main function of machine a 
unsigned long t;// Define delay parameters t
SCON=0x90;// Set serial mode 2. Allow to receive 
PCON=0;// Set up SNOD=0
IE=0x90;// Serial port is open 
for(t=0;t<10;t++);// Time delay 0.5ms、 To facilitate serial initialization of machine B 
while(1)(
for(i=0;i<10;i++)// Cycle to send 10 Data 
ACC=c[i];// Get and send data 
TBB=P;// The parity sign is sent TB6
P27=!P;// The parity sign is sent P2.7 Show 
P1=ACC;// Send data serially P1 Mouth display 
SBUF=ACC;// Send a frame of data in series 
while(F==0);// Wait for the reply of machine B 
F=0;// Reply 、 Reply sign of cleaning machine 
P1=0xff;//P1 Mouth stop display 
P27=1;// The parity sign light is off 
for(t=0;t<11000;t++);}// Stop display 0.5s after , Return loop , Continue to send the next 
for(t=0;t<11000;t++);}}//10 Data has been sent , Delay again 0.5s、 Start from scratch void 
    
 void insa() interrupt 4{
    // A serial interrupt function 
if(TI==1)TI=0;// If serial transmission is interrupted , Clear the serial transmission interrupt flag if(TI==1)TI=0;// Otherwise, the serial reception is interrupted , Clear the serial receive interrupt flag 
     else{
    RI=0;// If the reply signal is FFH, error , Execute the following statement :
	if(SBUF==0xff){
    // Get and send data 
	ACC=c[i];// The parity bit is sent to TB8 
    TB8=P;//P2.7 Display parity bit  
  	 P27=!P;
// Resend data 
	SBUF=ACC;// Resend data to P1 Mouth display 
P1=ACC;// If reply 0, No mistakes , Set B machine reply sign 
else if(SBUF==0) F=1;}}
(2) Machine B receives 
    #include<reg51.h>
    sbit P10=P1^0;// Define the drive end of the calibration marker lamp 
sbit P11=P1^1;// Define the driving end of parity marker lamp 
// Define the serial signal receiving flag , He Qing 0 
    bit f=0;
// Define the parity correct flag , He Qing 0 
     bit y=0;
void main(){
    // Machine B main function 
unsigned long t;// Define delay parameters t
SCON=0x90;// Set serial mode 2, Allow to receive 
PCON=0;// Set up SMOD=0
IE=0x90;// Serial port is open 
while(1){
    // Infinite loop 
while(f==0);// Wait for serial signal reception 
f=0; Clear the serial signal receiving flag 
if(y==1){
    y=0;// If parity is correct , Clear the parity check correct flag 
for(t=0;t<11000;t++);// Keep showing 0.5s
P2=0xff;// Off display 
P11=1;P10=1;// The parity sign light is off , The calibration mark light is off 
SBUF=0;// Send a signal that replies correctly 
else SBUF=0xff;}}// Otherwise, the verification error , Send a reply error signal 
void ins() interrupt 4{
    // B machine serial interrupt function 
if(TI==1)TI=0;// If serial transmission is interrupted , Clear the serial transmission interrupt flag 
else(RI=0;// Otherwise, the serial reception is interrupted , Clear the serial receive interrupt flag 
	ACC=SBUF;// The nail reader sends a signal , And in PSM Generate parity messages for receiving data in 
	P2=ACC;// Receive data and send P2 Mouth display 
	P11=!RB8;// The first 9 Bit data is sent to P1.1 Show 
	P10=!P;// Parity check is sent to P1.0 Show 
     if(P==RB8)y=1;// If parity is correct , Set the parity correct flag 
     else y=0;// Otherwise, the verification error , Clear the parity check correct flag 
	f=1;}}// Set the serial signal receiving flag 
原网站

版权声明
本文为[Wonderful drifting of coal]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202131954025798.html