19. 2 kbaud buffer stuffing and not break
a sweat. The receive buffer value can
extend from one to 256 bytes and must
always lie on a power of 2 boundary

( 1, 2, 4, 8, 16, 32, 64,128,256) to form a circular buffer.
Remember, our receive buffer is 64 bytes deep. The
EUSART_RX_BUFFER_MASK is set as one less than
the receive buffer depth. So, that defines our
EUSART_RX_BUFFER_MASK as binary 0b00111111. Let’s
pretend that our receive buffer head pointer value is zero
and the first character comes in. Following the fetch of the
incoming byte (usart_data = RCREG;), the temporary head
of the receive buffer (tmphead) is calculated. In our pretend
case, the receive buffer head pointer value is incremented
to one (0b00000001) and bit-wise ANDed to the
EUSART_RX_BUFFER_MASK value (0b00111111 AND
0b00000001). The bit-wise AND operation results in
0b00000001 and the incoming byte is stored at location

1 of the receive buffer. Now, let’s pretend the receive buffer
head pointer value is 63 (0b00111111) and a character
comes in. The receive buffer head pointer gets incremented
to 64 (0b01000000) and is bit-wise ANDed with the
EUSART_RX_BUFFER_MASK (0b01000000 AND
0b00111111). The bit-wise AND operation results in zero
and the incoming character 64 is stored at receive buffer
location zero. Pretty slick, huh? You should be able to work
your way through the transmit buffer function code as it
operates in a similar manner. I’ve included the entire set of
PIC18LF2620 interrupt-driven EUSART functions in the
download package which is available from the SERVO
website ( www.servomagazine.com).

Once all of the PIC18LF2620 initialization tasks have
completed, the C statement we have all been waiting for is
up for execution:

SCREENSHOT 1. The receive buffer is 64 bytes deep beginning
at offset 0xF3E and ending at offset 0xF7D.
The transmit buffer begins at offset 0xF36 and extends
eight bytes to offset 0xF3D. This shot is part of the MPLAB IDE
File Registers view. I obtained the buffer extents
from the MPLAB IDE Watch window.

//get the ETRX2’s
//attention
printf(“AT+N\r\n”); //what PAN has the
//ETRX2 joined

do{
temp16++;
}while(1);

}

Since there are no active PANs in my shop at the
moment, the “+N=NoPAN” response returned from the
ETRX2 in Screenshot 2 is appropriate. An “OK” is also
returned to inform us that the command was sent correctly
and processed as expected. Before we move on, let’s issue
one more command. This time, we’ll read the ETRX2’s
inputs with all of the flying in the wind. Then, we’ll ground
the ETRX2’s bit zero input and read the inputs again. Here’s
the code we’ll use for both input pin reads:

The PICC- 18 PRO C printf statement sends the ASCII
characters “AT” followed by a carriage return (\r) and line
feed (\n) from the PIC18LF2620’s EUSART to the ETRX2’s
UART. We are looking for a response of some kind from
the ETRX2. So, I simply kill time forever with the loop that
follows the printf statement.

Stopping the program execution while in the forever
do-while( 1) loop revealed the receive buffer’s contents you
see in Screenshot 1. The PIC18LF2620’s receive buffer
actually begins one byte prior to the “A” that I have
highlighted in the screenshot. Recall that the first character
received gets pushed into location one of the receive buffer,
not location zero. It seems that the ETRX2 has echoed our
“AT” command transmission down to the carriage return
(0x0D) and line feed (0x0A). The echoed command is
followed by the ETRX2’s reply of “OK.” We’re in the money!

Just for grins, let’s issue a command that queries
the ETRX2 about its PAN (Personal Area Network)
relationship:

do{
temp16++;
}while(1);

}

The results of the first read performed with the ETRX2’s
input pins floating is shown in Screenshot 3. The response
from the ETRX2 is S11:00F3. The value of the ETRX2’s
inputs are located within the bowels of S Register S11. I
grounded the zero input bit of the ETRX2 and ran the S11
query again. This time, we see a slightly different picture in
Screenshot 4. The ETRX2’s response with the zero input pin
grounded is S11:00F2.

What Goes In, Must Come Out

Now that we can get meaningful data from the ETRX2
into the PIC18LF2620’s receive buffer, how do we retrieve it
for use in our application? No worries. There is a function
in the PIC18LF2620’s ETRX2 firmware driver called recvchar.
The recvchar function pulls characters from the tail of the