by Fred Eady
Part 2: Coding
The Buffered
Communications
Subsystem
Lately, making things robotic move under
the control of a PIC microcontroller has
been my business. As we continue the
Dynamixel AX- 12+ coding task we
embarked upon last month, we will also
explore what it takes to coax a Microchip
PIC18F2620 to drive the Dynamixel RX- 28
and RX- 64 robot actuators, which operate
on a multi-drop RS-485 link.
Coding the Core Routines
Tcircular transmit and receive buffers called
EUSART_TxBuf and EUSART_RxBuf, respectively. The
he code that follows allocates a pair of 128-byte
buffers are termed circular because we have defined a pair
of head and tail pointers for each of them. The head pointer
is always chasing the tail pointer. Data is entered at the
buffer’s head and leaves the buffer via its tail. A buffer is
considered empty when the head and tail values are equal.
The buffer masks keep the pointers within the bounds of
the designated buffer sizes. Here are the definitions that
are at the heart of the transmission and reception of data
via the PIC18F2620’s EUSART:
//***********************************************
//* EUSART BUFFER DEFINITIONS
//***********************************************
#define EUSART_RX_BUFFER_SIZE 128
#define EUSART_RX_BUFFER_MASK \
( EUSART_RX_BUFFER_SIZE - 1 )
#define EUSART_TX_BUFFER_SIZE 128
#define EUSART_TX_BUFFER_MASK \
( EUSART_TX_BUFFER_SIZE - 1 )
char EUSART_RxBuf[EUSART_RX_BUFFER_SIZE];
char EUSART_TxBuf[EUSART_TX_BUFFER_SIZE];
char EUSART_TxHead;
char EUSART_TxTail;
32 SERVO 05.2009
char EUSART_RxHead;
char EUSART_RxTail;
The buffers are supported by a firmware interrupt
mechanism that consists of a set of send/receive functions
that are triggered by the PIC18F2620’s EUSART hardware
interrupt bits. The send/receive firmware functions are
responsible for sensing the status of the hardware interrupt
bits and moving data between the circular buffers and the
EUSART hardware buffers accordingly.
To remove a character from the circular receive buffer,
we call the recvchar function. Conversely, to place a character
into the transmit buffer, we call the sendchar function. Each
time that the sendchar function is called, a byte is buffered
and transmitted as soon as possible. In reality, as soon as
possible means immediately as the EUSART effectively
double buffers bytes to be transmitted. Incoming characters
from the EUSART are automatically stuffed into the circular
receive buffer by the firmware. To avoid overrunning the
receive buffer (head pointer = tail pointer with valid unread
data remaining in the buffer), we must periodically
query the receive buffer’s head versus tail status. That is
accomplished with the CharInQueue function, which when
called returns a Boolean TRUE if the head pointer is not
equal to the tail pointer.
The RS-232 link used by the Dynamixel AX- 12+ is
half-duplex in nature and this is reflected by the
CD4069/74HC125 circuitry you see in Schematic 1.