WPILib & LCD Display

We’re trying to use just WPILib to display a character on an LCD screen through a TX port on a Vex processor.

We aren’t having too much luck with making it happen with just these functions that WPILib gives us:

unsigned char ReadSerialPortOne(void);
unsigned char ReadSerialPortTwo(void);
void WriteSerialPortOne(unsigned char byte);
void WriteSerialPortTwo(unsigned char byte);
void OpenSerialPortOne(unsigned baudRate);
void OpenSerialPortTwo(unsigned baudRate);

These are the definitions for the BAUD rates:

#define BAUD_4800 0x0081
#define BAUD_9600 0x0040
#define BAUD_14400 0x01AD
#define BAUD_19200 0x0181
#define BAUD_28800 0x0156
#define BAUD_38400 0x0140
#define BAUD_57600 0x012A
#define BAUD_115200 0x0115

We tried out Kevin Watson’s serial port code, and it works, and we’ve found out a BAUD of 9600 should be used for an LCD display. Also, his code and the WPILib manual say Port 2 should be used for the LCD display. The only problem is his code has so much more than we need for just this goal.

It seems like there should be a way to use the functions in WPILib to output a character (and later a string of characters) to an LCD display serially. Is it possible?

WriteSerialPortOne and WriteSerialPortTwo should write a single character to the first and second serial ports respectively. To write a null-terminated string to the first serial port, you could use a function such as


void puts(char *str)
{
   while(*str)
   {
      WriteSerialPortOne(*str++);
   }
}

Of course, make sure you’ve opened the serial port before trying to read or write with it, and that you pass one of the baudrate constants to the open function; it sounds like you would want something like OpenSerialPortTwo(BAUD_9600);

It seems like you probably could have figured this yourself, though, so let me know if I’ve misinterpreted your question.

–Ryan

Here’s the code we had first tried using to output “h” onto the serial lcd display, but we had no luck with it.

#include "API.h"
#include "BuiltIns.h"

void main(void)
{
OpenSerialPortTwo(BAUD_9600);

while(1){
WriteSerialPortTwo('h');
}

}

Using your suggestion, we’ve modified the code to the following, but we’re still getting nothing. Here is our use of your code (we changed “puts” to “putsd” because of an error message):

#include "API.h"
#include "BuiltIns.h"

void putsd(char *str)
{
   while(*str)
   {
      WriteSerialPortTwo(*str++);
   }
}

void main(void)
{
OpenSerialPortTwo(BAUD_9600);
putsd('h');
}

We know the display is wired properly because when we use Kevin Watson’s serial ports code, it displays correctly. The only problem is his code seems to conflict with WPILib, and we’d like to use the display as well as WPILib.

I don’t know much about what you are doing, but I do know you have a error in this code, putsd is expecting a string, not a character, try putsd(“h”); (double quote vs single quote) or putsd(“h\0”);

\0 is implied by “”, so you don’t need to put it. “test\0” will double null terminate the string, which isn’t necessary.

Are you initializing the LCD driver chip properly? The ones I have used needed to be prodded a bit before they would display characters.

We also tried quotes but we didn’t try the \0 at all. We’ll give that a go tomorrow.

I’m not quite sure. The only initialization being done is “OpenSerialPortTwo(BAUD_9600);” so it may be that the LCD chip itself is not being initialized. If you’re right, how do you think we could properly initialize that LCD chip?
This is the LCD screen we’re using, just a standard HD44780 controller.

CHeck the Arduino source code, they have a library for character LCD’s

I’m working with aseligman on this issue. We eventually got the WPILib to output display to the SparkFun SerLCD display. We did use OpenSerialPortTwo(BAUD_9600); and then WriteSerialPortTwo(‘w’);

For some reason, it wasn’t working with an earlier project. But we noticed that we couldn’t get output to go to the terminal either. So, we looked up an old project that did output to the terminal and when we added the above lines of code, we were able to write to the LCD display.

On a side note, we just tried to create a new project and the new project will not printf to the terminal. So, there is still something wrong- maybe with our MPLAB configuration? Project configuration? Not sure. Regardless, our old project is working fine, so we’ll try to isolate the problem. Thanks for all the ideas.

Ok, another interesting discovery today. After much trial and error, we figured out why outputting to the serial port only worked sometimes. Actually, we never figured out why, we merely figured out how to get output consistently. For some reason, we had to have some kind of delay at the beginning of our program. This is really strange because we had the output looping and it did not work.

Anyway, by adding a Wait(1000) statement at the beginning of the code, we were able to get printf to the terminal and serial output to the SparkFun SerLCD display consistently. After we discovered this little trick, we then stumbled across a chiefdelphi thread that came to the same conclusion:

Interestingly, the thread also said that they couldn’t get printf to work with WPILib when they had a function other than main(). We may experiment with that a bit more another day. However, I’m hoping that this info is helpful to anyone else having problems using printf or serial out with WPILib. Because if you can get it to work, than you can have a nice inexpensive serial LCD display for your vex (http://www.sparkfun.com/commerce/product_info.php?products_id=9393).