Serial LCD allows you use a single serial port to control a 16×2 LCD display. The default baud rate is 9600, but of course you can modify the baud rate to: 2400,4800,9600,14400,19200. In the following tutorial I will demonstrate how to modify this baud rate.
For detailed information please refer to this LinkSprite wiki page.
First we must add the baud rate definitions to the top of the sample code:
#define Baud_2400 0x0b #define Baud_4800 0x0c #define Baud_9600 0x0d #define Baud_14400 0x0e #define Baud_19200 0x0f
Then add the function to set the baud rate:
void SetLcdBaud(int Baud) { unsigned int serialbaud; LCD.write(0x7C); LCD.write(Baud); switch(Baud) { case 0x0b : serialbaud = 2400; break; case 0x0c : serialbaud = 4800; break; case 0x0d : serialbaud = 9600; break; case 0x0e : serialbaud = 14400; break; case 0x0f : serialbaud = 19200; break; }
Notice: Before the baud rate can be modified it must first communicate at the default baud rate which is 9600.
Example:
#include <SoftwareSerial.h> #define Baud_2400 0x0b #define Baud_4800 0x0c #define Baud_9600 0x0d #define Baud_14400 0x0e #define Baud_19200 0x0f #define txPin 2 SoftwareSerial LCD = SoftwareSerial(0, txPin); // since the LCD does not send data back to the Arduino, we should only define the txPin const int LCDdelay=10; // conservative, 2 actually works void SetLcdBaud(int Baud) { unsigned int serialbaud; LCD.write(0x7C); LCD.write(Baud); switch(Baud) { case 0x0b : serialbaud = 2400; break; case 0x0c : serialbaud = 4800; break; case 0x0d : serialbaud = 9600; break; case 0x0e : serialbaud = 14400; break; case 0x0f : serialbaud = 19200; break; } LCD.begin(serialbaud); delay(100); } // wbp: goto with row & column void lcdPosition(int row, int col) { LCD.write(0xFE); //command flag LCD.write((col + row*64 + 128)); //position delay(LCDdelay); } void clearLCD(){ LCD.write(0xFE); //command flag LCD.write(0x01); //clear command. delay(LCDdelay); } void backlightOn() { //turns on the backlight LCD.write(0x7C); //command flag for backlight stuff LCD.write(157); //light level. delay(LCDdelay); } void backlightOff(){ //turns off the backlight LCD.write(0x7C); //command flag for backlight stuff LCD.write(128); //light level for off. delay(LCDdelay); } void serCommand(){ //a general function to call the command flag for issuing all other commands LCD.write(0xFE); } void setup() { pinMode(txPin, OUTPUT); LCD.begin(9600); SetLcdBaud(Baud_19200); delay(200); backlightOff() ; delay(1000); backlightOn() ; clearLCD(); lcdPosition(0,0); LCD.print("test baud rate 19200"); } void loop() { }
Leave a Reply
You must be logged in to post a comment.