51 IO port simulation serial communication C source program

Publisher:朝霞暮雨Latest update time:2021-05-13 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Introduction: This article mainly introduces the C source program of 51 IO port simulation serial communication.


#include

sbitBT_SND=P1^0;

sbitBT_REC=P1^1;

/**********************************************


IO port simulation 232 communication program


C program using two methods to occupy timer 0


**********************************************/


#defineMODE_QUICK


#defineF_TMF0


#defineTIMER0_ENABLETL0=TH0;TR0=1;

#defineTIMER0_DISABLETR0=0;


sbitACC0=ACC^0;

sbitACC1=ACC^1;

sbitACC2=ACC^2;

sbitACC3=ACC^3;

sbitACC4=ACC^4;

sbitACC5=ACC^5;

sbitACC6=ACC^6;

sbitACC7=ACC^7;


voidIntTimer0() interrupt1

{

F_TM=1;

}

//Send a character

voidPSendChar(unsignedcharinch)

{

#ifdefMODE_QUICK

ACC=inch;


F_TM=0;

BT_SND=0;//start bit

TIMER0_ENABLE; //Start

while(!F_TM);


BT_SND=ACC0; //Send low bit first

F_TM=0;

while(!F_TM);


BT_SND=ACC1;

F_TM=0;

while(!F_TM);


BT_SND=ACC2;

F_TM=0;

while(!F_TM);


BT_SND=ACC3;

F_TM=0;

while(!F_TM);


BT_SND=ACC4;

F_TM=0;

while(!F_TM);


BT_SND=ACC5;

F_TM=0;

while(!F_TM);


BT_SND=ACC6;

F_TM=0;

while(!F_TM);


BT_SND=ACC7;

F_TM=0;

while(!F_TM);


BT_SND=1;

F_TM=0;

while(!F_TM);



TIMER0_DISABLE; //Stop timer

#else

unsignedcharii;


ii=0;


F_TM=0;

BT_SND=0;//start bit

TIMER0_ENABLE; //Start

while(!F_TM);


while(ii<8)

{

if(inch&1)

{

BT_SND=1;

}

else

{

BT_SND=0;

}

F_TM=0;

while(!F_TM);

ii++;

inch>>=1;

}

BT_SND=1;

F_TM=0;

while(!F_TM);


#endif

TIMER0_DISABLE; //Stop timer

}

//Receive a character

unsignedcharPGetChar()

{

#ifdefMODE_QUICK


TIMER0_ENABLE;

F_TM=0;

while(!F_TM);//wait until the start position

ACC0=BT_REC;


TL0=TH0;


F_TM=0;

while(!F_TM);

ACC1=BT_REC;


F_TM=0;

while(!F_TM);

ACC2=BT_REC;


F_TM=0;

while(!F_TM);

ACC3=BT_REC;


F_TM=0;

while(!F_TM);

ACC4=BT_REC;


F_TM=0;

while(!F_TM);

ACC5=BT_REC;


F_TM=0;

while(!F_TM);

ACC6=BT_REC;


F_TM=0;

while(!F_TM);

ACC7=BT_REC;


F_TM=0;


while(!F_TM)

{

if(BT_REC)

{

break;

}

}

TIMER0_DISABLE; //Stop timer

returnACC;

#else

unsignedcharrch,ii;

TIMER0_ENABLE;

F_TM=0;

ii=0;

rch=0;

while(!F_TM);//wait until the start position


while(ii<8)

{

rch>>=1;

if(BT_REC)

{

rch|=0x80;

}

ii++;

F_TM=0;

while(!F_TM);


}

F_TM=0;

while(!F_TM)

{

if(BT_REC)

{

break;

}


}

TIMER0_DISABLE; //Stop timer

returnrch;


#endif


}

// Check if there is a start bit

bitStartBitOn()

{

return(BT_REC==0);


}

voidmain()

{

unsignedchargch;


TMOD = 0x22; /*Timer 1 is in working mode 2 (8-bit auto-reload), 0 is in mode 2 (8-bit

Automatic reload)*/

PCON=00;


TR0=0; //Start using when sending or receiving

TF0=0;

TH0=(256-96); //9600bps is 1000000/9600=104.167 microseconds

timer is

//

104.167*11.0592/12=96

TL0=TH0;

ET0=1;

EA=1;


PSendChar(0x55);

PSendChar(0xaa);

PSendChar(0x00);

PSendChar(0xff);


while(1)

{

if(StartBitOn())

{

gch=PGetChar();

PSendChar(gch);

}

}


}


Reference address:51 IO port simulation serial communication C source program

Previous article:Realization of the communication test system between computer serial port and single chip microcomputer
Next article:Four methods of extending interrupts of 51 MCU

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号