Serial communication
When you want to exchange data between the Dwengo board and a computer or another electrical component such as a compass or bluetooth chip, then the easiest way to do this is using the RS232 protocol. This protocol is also used by your computer when you connect a device to the serial port. The Dwengo library provides the necessary functionality to setup a serial connection. Here we provide more information on how you can setup such a connection yourself.
You start by loading the necessary libraries:
#include <p18f4550.h> #include <delays.h> #include <usart.h>
Next, you have to initialize the USART module of the microcontroller. This means you have to chose the communication speed, indicate whether you want to generate an interrupt when data arrives, how many data bits there are, … The complete list of options is described in the data sheet of the microcontroller
. A possible initialization of the USART module is the following:
// open connection OpenUSART (USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_LOW, 77); // cfr table 20-3
The given parameters set the properties of the connection.
- USART_TX_INT_OFF: interrupt when sending data is disabled.
- USART_RX_INT_OFF: interrupt when receiving messages is disabled.
- USART_ASYNCH_MODE: the RS-232 standard works with asynchronous communication, this means the communication is not driven by a clock.
- USART_EIGHT_BIT: we set the number data bits to 8.
- USART_CONT_RX: enable continuous receiving.
- USART_BRGH_LOW: we will work with a low baud rate (speed) of 9600 baud. This is sufficient for these applications. Note that the microcontroller supports speeds up to 115,200 baud if necessary.
- We have to setup as well that we want to work exactly at 9600 baud. Therefore, we add a last argument to the function OpenUSART: the value 77. The calculation of this value is explained in chapter 20 of the PIC18F4550 data sheet
and happens as follows: Fosc/(baud rate*64)-1. In our case this is 48,000,000/(9600*64)-1 which gives the truncated value of 77.
When you no longer want the serial communication, you can terminate the USART module by calling CloseUSART().
Typically the communication happens in the following way:
- Send message to the computer.
- Wait until the computer sends something back.
- Read the message send by the computer.
- Send reply to the computer.
A simple message can be send with the function putrsUSART((const far rom char *) "message"). Next we wait for the reply of the computer. This can be checked with the function DataRdyUSART() that returns the value TRUE when the microcontroller received data from the USART module. Hence, we use a while loop that calls the function DataRdyUSART() as long as it returns FALSE.
// while no new data is available while (!DataRdyUSART());
When the computer sends new data to the Dwengo board, we exit the while loop. In he next piece of code we show how we can read the data character by character. Hereby buffer is an array of characters (char).
// read something from USART position = 0; do { while (!DataRdyUSART()); buffer[position] = getcUSART(); } while(buffer[position++] != '\r');
Alternatively, a fixed amount of bytes can be loaded into a buffer with the function getsUSART. More information about this function can be found in the documentation of the C18 library
.
- Type:

Your shopping cart