(4) MCS-51串行通讯:

  MCS-51的串行口使用起来非常简单,因为MCS-51单片机的串行口没有与MODEM控制相关的信号。这使得51的通讯口非常易于使用。使用查询方式时,仅需初始化有关的寄存器即可。演示程序如下:

#include <stdio.h>
#include <reg51.h>

void putch(unsigned char);
unsigned char getch(void);

main()
{
unsigned char ch;

SCON = 0x50;
TMOD |= 0x20;
TH1 = 0xfd;
TL1 = 0xfd;
TR1 = 1;
TI = 1;
RI = 0;

while(1) {
ch = getch(); putch(ch);

}
}

void putch(unsigned char ch) {

SBUF = ch;
TI = 0;
while(!TI);
}

unsigned char getch(void) {

while(!RI);
RI = 0;
return(SBUF);

}

  使用中断驱动的程序比较复杂,下面为完整的MCS-51串行通讯底层驱动程序,由头文件serint.hJ及serint.c组成。   

serint.h

unsigned char RR_iHead; /* receiver head index */
unsigned char RR_iTail; /* receiver tail index */
unsigned char RR_cLev; /* receiver buffer count */
unsigned char RR_cMax; /* receiver buffer count */

unsigned char TR_iHead; /* transmitter head index */
unsigned char TR_iTail; /* transmitter tail index */
unsigned char TR_cLev; /* transmitter buffer count */
unsigned char TR_cMax; /* transmitter buffer count */

unsigned char UnGotCh; /* saved char for ungetch() */

unsigned char SerFlags; /* serial flag */

bit FlagTransIdle; /* set when transmitter is finished */
bit FlagStripOutLF; /* don't send linefeeds */
bit FlagCvtInCR; /* convert incoming CR to LF */

unsigned char TestBits;

#define INRINGSIZE 128 /* must be <= 254 to avoid wraps */
#define OUTRINGSIZE 250 /* ditto */

#define T1RELOAD 253

#define CR 13
#define LF 10
#define ESC 27

#define EOF -1

unsigned char xdata RRing[INRINGSIZE]; /* receiver ring buffer */
unsigned char xdata TRing[OUTRINGSIZE]; /* receiver ring buffer */

int putstr (const char *);
int putch(int);
int chkch();
int getch();
void SerWaitOutDone();
int SerFlushIn();
int putc(int TransChar);

serint.c

/* CONSOLE.C -- serial I/O code */

/*---------------------------------------------------------------------------*/   
/* Initialize serial port hardware and variables */

#include <reg51.h>
#include "serint.h"

void SerInitialize() {

SerFlags = 0;

FlagTransIdle = 1;
FlagCvtInCR = 1; /* want to turn CRs into LFs */
RR_iHead = RR_iTail = RR_cLev = RR_cMax = 0;
TR_iHead = TR_iTail = TR_cLev = TR_cMax = 0;
UnGotCh = -1;

/*--- set up Timer 1 to produce serial rate */

TCON &= 0x3F; /* clear run & interrupt flags */
TMOD &= 0x0F; /* flush existing Timer 1 setup */
TMOD |= 0x20; /* flush existing Timer 1 setup */

SCON = 0x50; /* flush existing Timer 1 setup */
PCON |= 0x00;
TH1 = TL1 = T1RELOAD & 0x00FF; /* flush existing Timer 1 setup */
TR1 = 1; /* start the timer */
ES = 1; /* enable serial interrupts */
}

/*---------------------------------------------------------------------------*/   
/* Serial console interrupt handler */
/* If transmitter output is disabled, we fake trans interrupts until empty */

void SerInt() interrupt 4
{

if(RI) { /* receiver interrupt active? */
if(RR_cLev<INRINGSIZE) { /* room for newest char? */
RRing[RR_iHead] = SBUF; /* pick up the character and stick in ring */
RR_iHead ; /* tick the index */
RR_cLev ; /* tick size counter */
if(RR_iHead==INRINGSIZE) RR_iHead = 0; /* hit end of array yet? */
}
RI = 0; /* indicate we have it */
}

if(TI) { /* transmitter interrupt active? */
if(TR_cLev) { /* anything to send? */
SBUF = TRing[TR_iTail]; /* fetch next char and send it */
TR_cLev--; /* tick size counter */
TR_iTail ; /* tick the index */
if(TR_iTail==OUTRINGSIZE) TR_iTail = 0; /* hit end of array yet? */
} else FlagTransIdle = 1;/* no, flag inactive */

TI = 0; /* indicate done with it */
}

}

/*---------------------------------------------------------------------------*/  
/* Send character to console */
/* Can strip LFs, in which case you get CR instead of LF/CR */


int putch(int TransChar)
{
putc(TransChar); /* if not LF, handle normally */
if(TransChar=='\n') putc('\r'); /* if LF, send a CR */
}

int putc(int TransChar)
{

while(TR_cLev>=OUTRINGSIZE); /* wait for space in ring */
ES = 0;
TRing[TR_iHead] = TransChar; /* point to char slot */
TR_iHead ; /* tick counter & index */
TR_cLev ;
if(TR_iHead==OUTRINGSIZE) TR_iHead = 0;
if(FlagTransIdle) {
FlagTransIdle = 0; /* kickstart transmitter if idle */
TI = 1;
}

ES = 1;
return(TransChar);
}

/*---------------------------------------------------------------------------*/
/* Decide if there are any pending chars */
/* Returns nonzero if there's a char */

int chkch() {

return(RR_cLev); /* tack on current level */
}

/*---------------------------------------------------------------------------*/  
/* Wait for the serial transmitter to go idle */
/* If the transmitter is disabled, that's considered to be the same thing */

void SerWaitOutDone() {

while (TR_cLev); /* wait for ring empty */
while(!FlagTransIdle); /* wait for last char */
}

/*---------------------------------------------------------------------------*/   
/* Flush the input buffer */
/* Returns number of chars flushed */


int SerFlushIn() {

ES = 0; /* turn off serial interrupts */
RR_iTail = 0; /* reset ring variables */
RR_iHead = 0;
RR_cLev = 0;
ES = 1; /* turn on serial interrupts */

}

/*---------------------------------------------------------------------------*/   
/* Get character from console */
/* CRs turn into LFs unless we're not doing that... */


int getch() {
int RetVal;

ES = 0; /* avoid interruptions */

if(RR_cLev) { /* anything pending? */
RetVal = RRing[RR_iTail];
if(RetVal=='\r') RetVal = '\n'; /* use LF instead of CR */
RR_iTail ; /* tick size index & counter */
RR_cLev--;
if(RR_iTail==INRINGSIZE) RR_iTail = 0; /* hit end of array yet? */
} else RetVal = -1;

ES = 1;
return(RetVal);
}

/*---------------------------------------------------------------------------*/   
/* Send string to console */
/* putstr(char *pString); */
/* The ?putstr entry point has *pString in DPTR */

int putstr (char *pstring)
{
while(*pstring) { /* fetch character if zero done */
putch(*pstring);
pstring ; /* continue... */
}
}

  使用查询的程序可以做到多高的波特率取决于主程序的工作量,使用中断方式的通讯驱动程序在使用11.0592MHz晶振时可以达到57.6Kbps的速率。

(5) 关于RS485   

  以上几个方面详细介绍了PC及MCS-51的RS-232C的串行通讯程序的设计方法,RS-485与RS-232C相类似,其区别在于使用双端平衡驱动及半双工模式,这些措施使RS-485传输距离更远,同时,RS-485还可以组网。在同一个RS-485网络中,可以多达32个模块,某些器件可以多达256个甚至更多。相应的,RS-485具有接收/发送控制端,RS-485的接收控制端可以在需要接收的时候打开或者一直打开以便无条件的接收线路上的数据。RS-485的发送控制端仅在需要发送时打开,平时应关端发送器,因为在同一RS-485网络中在同一时刻仅允许一个发送器工作。在数据发送完成后关闭发送器。这可以通过以下两种方法实现。

  一、在数据完全移出后,对于PC机为发送移位寄存器空,对于MCS-51为TI置位。这些调件既可使用查询的方法得到,也可以在中断程序中实现。

  二、将RS-485的接收器始终打开,这样一来,所有在RS-485上的数据均被接收回来,包括自己发送出去的数据。因此,当自己发送的数据完全被自己接收回来时即可关闭发送器。原则上说,这一方法无论是查询或中断方式都适用,但实际上,由于RS-485的数据通常打包后发送,因此,使用查询的方法并不理想。这一方法非常适合中断方式,尤其是以数据包传送的RS-485通讯。