还可这样IOto232
#include <reg52.h>
sbit BT_SND =P3^1;
sbit BT_REC =P3^0;
/**********************************************
IO 口模拟232通讯程序
使用两种方式的C程序 占用定时器0
**********************************************/
#define F_TM F0
#define TIMER0_ENABLE TL0=TH0; TR0=1;
#define TIMER0_DISABLE TR0=0;
#define uchar unsigned char
#define uint unsigned int
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;
uchar bufSND[8],bufRCV[8],putRCV,getRCV,curSND,lenSND,staRCV;
uchar putKEY,getKEY,nFUN,nSTN,Cxy,Cxy0,nTIM;
void IntTimer0() interrupt 1
{
F_TM=1;
}
//发送一个字符
void PSendChar(unsigned char inch)
{
unsigned char ii;
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);
TIMER0_DISABLE; //停止timer
}
//接收一个字符
unsigned char PGetChar()
{
unsigned char rch,ii;
TIMER0_ENABLE;
F_TM=0;
ii=0;
rch=0;
while(!F_TM); //等过起始位
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; //停止timer
return rch;
}
//检查是不是有起始位
bit StartBitOn()
{
return (BT_REC==0);
}
void main()
{
unsigned char gch;
TMOD=0x22; //定时器1为工作模式2(8位自动重装),0为模式2(8位自动重装)
PCON=00;
TR0=0; //在发送或接收才开始使用
TF0=0;
TH0=(256-104); //9600bps 就是 1000000/9600=104.167微秒 执行的timer是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();
bufRCV[putRCV++]=PGetChar();
putRCV &= 0X07;
if(bSD){if(curSND<lenSND) SBUF=bufSND[curSND++];else {bSD=OFF;}}
PSendChar(gch);
}
}
} |