您的位置 首页 数字

51单片机摸拟串口(232)

includereg51h>sbitBT_SND=P1^0;sbitBT_REC=P1^1;IO口模拟232通讯程序使用两种方式的C程序占用定时器0defineuchar

#include

sbit BT_SND =P1^0;

sbit BT_REC =P1^1;

//IO 口模仿232通讯程序

//运用两种方法的C程序 占用定时器0

#define uchar unsigned char

#define uint unsigned int

#define MODE_QUICK

#define F_TM F0

#define TIMER0_ENABLE TL0=TH0; TR0=1;

#define TIMER0_DISABLE TR0=0;

sbit ACC0= ACC^0;

sbit ACC1= ACC^1;

sbit ACC2= ACC^2;

sbit ACC3= ACC^3;

sbit ACC4= ACC^4;

sbit ACC5= ACC^5;

sbit ACC6= ACC^6;

sbit ACC7= ACC^7;

unsignedchari;

uchar gch[8];

uchar idata *p;

voiddelay(uint x);

voidIntTimer0() interrupt 1

{

F_TM=1;

}

//发送一个字符

voidPSendChar(unsignedcharinch)

{

#ifdef MODE_QUICK

ACC=inch;

F_TM=0;

BT_SND=0;//start bit

TIMER0_ENABLE;//发动

while(!F_TM);

BT_SND=ACC0;//先送出低位

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;//中止timer

#else

unsignedcharii;

ii=0;

F_TM=0;

BT_SND=0;//start bit

TIMER0_ENABLE;//发动

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;//中止timer

}

//接纳一个字符

unsignedcharPGetChar()

{

#ifdef MODE_QUICK

TIMER0_ENABLE;

F_TM=0;

while(!F_TM);//等过开始位

ACC0=BT_REC;

TL0=TH0;

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/zhishi/shuzi/255845.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部