Communication with the computer: a configurable news ticker

In this tutorial we extend the news ticker in such a way that the text can by configured with a computer. We explain step by step how to communicate with the computer using the serial connection of the Dwengo board. This can be applied to computers with a serial connector as well as computers that have only USB ports.



Requirements

  1. One Dwengo board
  2. One Dwengo programmer
  3. Enclosed USB cable
  4. Computer with a serial port or USB port
  5. One serial cable or a USB-to-serial cable

Communicating with a computer

The easiest way to let the Dwengo board communicate with a computer is through the built-in serial driver chip, the MAX232, which converts signals from an RS232 port to signals at a TTL (transitor transitor logic) level so the microcontroller can use them. The microcontroller of the Dwengo board can easily communicate with the computer through the serial port by using the MAX232 in the correct way.

Start by loading the necessary libraries. Since we start from the tutorial about the news ticker we use the same libraries. We append an extra library to this list: dwengoUsart.h, needed for serial communication.

  1. #include <dwengoConfig.h>
  2. #include <dwengoBoard.h>
  3. #include <dwengoUsart.h>

We define a macro that contains the maximum length of the text we want to send.

  1. #define PREDEFINED_LENGTH 64 // Maximum number of characters in string

In this tutorial we need two extra functions that we define under the main function. These functions have to be declared at the top of the C file in a so-called prototype.

  1. unsigned char newsticker(ram char *msg, unsigned char position);
  2. void str2ram(static char *dest, static char rom *src);

The first prototype is for the newsticker function that was also used in the tutorial about the news ticker. The second function makes it possible to copy a string from the program memory to the data memory. Otherwise the initial message we show on the LCD will not be accepted by the newsticker function which only accepts strings from the data memory.

In the main loop we first define the variables we'll use, setup the initial message and initialize the LCD.

  1. void main(void) {
  2. unsigned char position = 0;
  3. char buffer[PREDEFINED_LENGTH+1];
  4.  
  5. str2ram(buffer, "Initial message ..."); // initial message
  6.  
  7. initBoard();
  8. backlightOn();

Next we begin with a loop that will run indefinitely. In this loop we execute the following systematic steps:

  1. Open and configure the serial connection.
  2. Send a message to the computer.
  3. Wait until the computer sends something back. Meanwhile, show the message on the LCD of the Dwengo board.
  4. Load the message sent by the computer.
  5. Acknowledge the message from the computer.
  6. Close the serial connection.

After starting the infinite loop we open and configure the serial connection. For this we use the initUsart() function. This function will initialise the connection to a speed of 9600 baud. If you want more technical details we provide some tips about serial communication.

  1.  
  2. while(TRUE) { // do this forever
  3. // open connection
  4. initUsart();

After configurating the connection, we send a message to the computer.

  1. // send something
  2. putrsUSART((const far rom char *)"\nGive message\n\r");

Next we wait for an answer from the computer. This is checked by the DataRdyUSART() function which returns the value TRUE when the microcontroller has received data through the USART module. Therefor we make a while loop that keeps running as long as the function DataRdyUSART() returns FALSE. In the while loop we call the function newsticker that shows the message in buffer on the LCD.

  1. // while no new data is available
  2. while(!DataRdyUSART()) {
  3. // display message with news ticker effect
  4. setCursorLCD(0, 0);
  5. position = newsticker(buffer, position);
  6. delay_ms(400);
  7. }

When the computer sends new data to the Dwengo board, we leave this while loop. In the next piece of code we read this data character by character.

  1. // read something from USART
  2. position = 0;
  3. do {
  4. while (!DataRdyUSART());
  5. buffer[position] = getcUSART();
  6. }
  7. while (buffer[position++] != '\r' && position < PREDEFINED_LENGTH);

Inside this do ... while loop we read each new character using getcUSART(). This character is stored in buffer. The variable position determines where the current character is stored in the buffer. We keep reading until the last character is equal to '\r' (the code for a RETURN) or until position has reached the maximum length of the buffer.
Within this do ... while loop is also a while loop nested that waits until there is new data present. If we would not do this, then getcUSART would read the same character again.

  1. // null character to close the string
  2. buffer[position-1] = NULL;
  3.  
  4. // reset position
  5. position = 0;

After the do ... while loop we terminate the string with a NULL character. We also put position back to 0, so the next time the message is displayed from the first character to the screen.

To round of, we let the user know that we have processed the message.

  1. // send something back
  2. putrsUSART((const far rom char *)"\nMessage accepted\n\r");

In step 6 we close the connection.

  1.  
  2. // close connection
  3. CloseUSART();
  4. }
  5. }

We also provide the function that is needed for the news ticker.

  1. unsigned char newsticker(ram char *msg, unsigned char position) {
  2. char i,j;
  3. j=position;
  4. for (i=0; i<16; i++) {
  5. if (msg[j]==NULL)
  6. j=0;
  7. appendCharToLCD(msg[j]);
  8. j++;
  9. }
  10. if (msg[position+1]==NULL)
  11. return 0;
  12. else
  13. return position+1;
  14. }

To conlcude we provide the function that copies a NULL-terminated string from program memory to the data memory.

  1. void str2ram(static char *dest, static char rom *src) {
  2. while((*dest++ = *src++) != NULL);
  3. }

Opening and closing the serial connection

In this program we have repeatedly opened and closed the serial connection with the computer (step 1 and step 6). In most cases it is more efficient to leave the connection open starting from the beginning and only to close it at the very end.

The connection uses the pins RC2-7. If you want to use these pins for something else, then you will have to close the serial connection. In such an application, the approach we used (each time opening and closing the connection) is necessary.

Connecting the Dwengo board to the computer

Connecting the Dwengo board to the computer is done with a serial cable. The cable is connected to the serial port of the Dwengo board and the serial port of the computer. If the computer has no serial port, you have to use a USB-to-serial cable. Probably you will have to install a driver for this, which is enclosed with the cable.

Connecting the Dwengo port

Testing with the computer

After you have programmed and connected the Dwengo board, you can start testing. For this you can write your own computer program that writes messages to the serial port. However, it is easier to use an existing program such as the free TeraTerm Pro External link. Download and install this program and start it. We are going to setup a connection with the Dwengo board. When you use a USB-to-serial calbe, in Windows check Control Panel > Performance and Maintenance > Administrative Tools > Computer Management to find out to which COM port this cable is connected.
In our example this is COM3.

Determine COM port


Next start TeraTerm Pro and select a connection type serial and the correct COM port. Next click on OK.
New connection
Navigate in TeraTerm Pro to Setup > Serial port..., now configure the properties of the connection to the settings as displayed below and click on OK.
Connection settings
Also turn on Local echo in the menu Setup > Terminal in order to see what you type yourself in the terminal of TeraTerm Pro. Press on OK again.
Terminal settings
Now you have a connection to the Dwengo board. If everything goes right, a message appears on your monitor when the Dwengo board is turned on or reset.

Give message


Now you can type your own message and send it to the Dwengo board. Type your message and press RETURN when you are finished. This message will then appear on the LCD of the Dwengo board. If you want you can send another new message.

Message accepted

More fun to play with

With the knowledge of this tutorial you can do a lot of cool stuff. For example you can use the Dwengo board to drive a servo motor controlled by the computer. If you mount a web cam to this motor you can be monitored continuously. Also building simple home automation in which your computer and the Dwengo board are used to switch on or off several lights are just one of the many possibilities.

Source codeSource code files are only available to registered users. If you have an account on the Dwengo site, please log in to download source files, else you can create your free account by registering.