Loading...

serLCD - Sparkfun serLCD Library

This is my attempt at porting LiquidCrystal library for use with serial displays. It implements most all functions from LiquidCrystal and added a few functions of my own. This library subclasses NewSoftSerial.

Since version 1.5 the library no longer subclasses NewSoftSerial as it is Arduino 1.0 compatible. Please download v1.5 from:

http://flipmu.com/files/2011/12/serLCD.zip

Functions

Examples

CODE

serLCD.h

  1. /*
  2.  serLCD.h - Library for controlling a SparkFun serLCD module
  3.             Code written for firmware version 2.5
  4.  
  5.  Created by Cody B. Null, September 9, 2011
  6.  Modified by Jordan Hochenbaum, Dec 5, 2011. (Version 1.5)
  7.  
  8.  Version 1.4 - This version wrote specifically for 16x2
  9.                Display.
  10.                More display support coming in later version.
  11.  
  12.  Version 1.5 - Since mid 2011 the Arduino IDE has implemented the NewSoftSerial
  13.                library updates in the standard SoftwareSerial library and so
  14.                using NewSoftSerial is no longer needed. serLCD has been updated
  15.                to use the Standard SoftwareSerial library. Also, Arduino 1.0
  16.                support has been added. Backwards compatible with previous versions.
  17.  
  18.  Note - This library requires NewSoftSerial library
  19.         The latest version of NewSoftSerial library can
  20.         always be found at http://arduiniana.org. -> NO LONGER NECESSARY. See 1.5 notes above.-Jordan Hochenbaum
  21. */
  22. #ifndef serLCD_h
  23. #define serLCD_h
  24.  
  25. #if ARDUINO >= 100
  26. #include "Arduino.h"       // for delayMicroseconds,digitalPinToBitMask, etc
  27. #else
  28. #include "WProgram.h"      // for delayMicroseconds
  29. #include "pins_arduino.h"  // for digitalPinToBitMask, etc
  30. #endif
  31. #include "SoftwareSerial.h"
  32.  
  33. // Commands
  34. #define LCD_BACKLIGHT           0x80
  35. #define LCD_CLEARDISPLAY        0x01
  36. #define LCD_CURSORSHIFT         0x10
  37. #define LCD_DISPLAYCONTROL      0x08
  38. #define LCD_ENTRYMODESET        0x04
  39. #define LCD_FUNCTIONSET         0x20
  40. #define LCD_SETCGRAMADDR        0x40
  41. #define LCD_SETDDRAMADDR        0x80
  42. #define LCD_SETSPLASHSCREEN     0x0A
  43. #define LCD_SPLASHTOGGLE        0x09
  44. #define LCD_RETURNHOME          0x02
  45.  
  46. // Flags for display entry mode
  47. #define LCD_ENTRYRIGHT          0x00
  48. #define LCD_ENTRYLEFT           0x02
  49.  
  50. // Flags for display on/off control
  51. #define LCD_BLINKON             0x01
  52. #define LCD_CURSORON            0x02
  53. #define LCD_DISPLAYON           0x04
  54.  
  55. // Flags for display size
  56. #define LCD_2LINE               0x02
  57. #define LCD_4LINE               0x04
  58. #define LCD_16CHAR              0x10
  59. #define LCD_20CHAR              0x14
  60.  
  61. //  Flags for setting display size
  62. #define LCD_SET2LINE            0x06
  63. #define LCD_SET4LINE            0x05
  64. #define LCD_SET16CHAR           0x04
  65. #define LCD_SET20CHAR           0x03
  66.  
  67. class serLCD : public SoftwareSerial {
  68. public:
  69.         serLCD (int pin);
  70.  
  71.         void clear();
  72.         void clearLine(int);
  73.         void home();
  74.         void setBrightness(int);
  75.  
  76.         void setSplash();
  77.         void toggleSplash();
  78.  
  79.         void blink();
  80.         void noBlink();
  81.         void cursor();
  82.         void noCursor();
  83.         void display();
  84.         void noDisplay();
  85.  
  86.         void setCursor(int, int);
  87.         void selectLine(int);
  88.  
  89.         void leftToRight();
  90.         void rightToLeft();
  91.         void autoscroll();
  92.         void noAutoscroll();
  93.  
  94.         void createChar(int, uint8_t[]);
  95.         void printCustomChar(int);
  96.  
  97. private:
  98.         void command(uint8_t);
  99.         void specialCommand(uint8_t);
  100.  
  101.         uint8_t _displayfunction;
  102.         uint8_t _displaycontrol;
  103.         uint8_t _displaymode;
  104.         uint8_t _numlines;
  105.         uint8_t _numchars;
  106.         uint8_t _rowoffset;
  107. };
  108.  
  109. #endif

serLCD.cpp

  1. /*
  2.  serLCD.cpp - Library for controlling a SparkFun serLCD
  3.               module.
  4.               Code written for firmware version 2.5
  5.  
  6.  Created by Cody B. Null, September 9, 2011
  7.  Modified by Jordan Hochenbaum, Dec 5, 2011. (Version 1.5)
  8.  
  9.  Version 1.4 - This version wrote specifically for 16x2
  10.                Display.
  11.                More display support coming in later version.
  12.  
  13.  Version 1.5 - Since mid 2011 the Arduino IDE has implemented the NewSoftSerial
  14.                 library updates in the standard SoftwareSerial library and so
  15.                 using NewSoftSerial is no longer needed. serLCD has been updated
  16.                 to use the Standard SoftwareSerial library. Also, Arduino 1.0
  17.                 support has been added. Backwards compatible with previous versions.
  18.  
  19.  
  20.  Note - This library requires NewSoftSerial library
  21.  The latest version of NewSoftSerial library can
  22.  always be found at http://arduiniana.org. -> NO LONGER NECESSARY. See V1.5 notes above
  23. */
  24.  
  25. //#include <../NewSoftSerial/NewSoftSerial.h>
  26.  
  27. #include <SoftwareSerial.h>
  28. #include "serLCD.h"
  29.  
  30. //      PUBLIC FUNCTIONS
  31.  
  32. // Contstructor
  33. // defaults to 16x2 display
  34. serLCD::serLCD(int pin) : SoftwareSerial(pin, pin){
  35.         pinMode(pin, OUTPUT);
  36.         begin(9600);
  37.         _numlines = LCD_2LINE;
  38.         _numchars = LCD_16CHAR;
  39.         _rowoffset = 0;
  40. }
  41.  
  42. /* Initialize.. not used trying to implement all display sizes
  43. void serLCD::init(int pin, int rows, int cols){
  44.         pinMode(pin, OUTPUT);
  45.         delay(4);
  46.         begin(9600);
  47.         if(cols == LCD_20CHAR){
  48.                 _numchars = LCD_20CHAR;
  49.                 specialCommand(LCD_SET20CHAR);
  50.         }else{ // default to 16 char display
  51.                 _numchars = LCD_16CHAR;
  52.                 specialCommand(LCD_SET16CHAR);
  53.         }      
  54.         if(rows == LCD_4LINE){
  55.                 _rowoffset = 1;
  56.                 _numlines = LCD_4LINE;
  57.                 specialCommand(LCD_SET4LINE);
  58.         }else{ // default to 2 line if input was invalid
  59.                 _rowoffset = 0;
  60.                 _numlines = LCD_2LINE;
  61.                 specialCommand(LCD_SET2LINE);
  62.         }
  63.         // clear the display
  64.         clear();
  65.         // set brightness to full
  66.         setBrightness(30);
  67. }
  68. */
  69.  
  70. // Set brightness value range 1-30 1=OFF 30=FULL
  71. void serLCD::setBrightness(int val){
  72.         if(val >= 1 && val <= 30){
  73.                 specialCommand(LCD_BACKLIGHT | (val - 1));
  74.         }
  75. }
  76.  
  77. // Clears screen and returns cursor to home position
  78. void serLCD::clear(){
  79.         command(LCD_CLEARDISPLAY);
  80. }
  81.  
  82. // Clears a single line by writing blank spaces then returning
  83. // cursor to beginning of line
  84. void serLCD::clearLine(int num){
  85.         if(num > 0 && num <= _numlines){
  86.                 setCursor(num, 1);
  87.                 print("                ");
  88.                 setCursor(num, 1);
  89.         }
  90. }
  91.  
  92. // Moves cursor to the beginning of selected line
  93. void serLCD::selectLine(int num){
  94.         if(num > 0 && num <= _numlines){
  95.                 setCursor(num, 1);
  96.         }
  97. }
  98.  
  99. // returns cursor to home position
  100. void serLCD::home(){
  101.         command(LCD_RETURNHOME);
  102. }
  103.  
  104. // Saves first 2 lines of txt to splash screen memory
  105. void serLCD::setSplash(){
  106.         specialCommand(LCD_SETSPLASHSCREEN);
  107. }
  108.  
  109. // Toggles splashscreen on and off
  110. void serLCD::toggleSplash(){
  111.         specialCommand(LCD_SPLASHTOGGLE);
  112. }
  113.  
  114. //  This is for text that flows Left to Right
  115. void serLCD::leftToRight(){
  116.         _displaymode |= LCD_ENTRYLEFT;
  117.         command(LCD_ENTRYMODESET | _displaymode);
  118. }
  119.  
  120. // This is for text that flows Right to Left
  121. void serLCD::rightToLeft() {
  122.         _displaymode &= ~LCD_ENTRYLEFT;
  123.         command(LCD_ENTRYMODESET | _displaymode);
  124. }
  125.  
  126. // Blinking cursor on/off
  127. void serLCD::blink(){
  128.         _displaycontrol |= LCD_BLINKON;
  129.         command(LCD_DISPLAYCONTROL | _displaycontrol);
  130. }
  131. void serLCD::noBlink(){
  132.         _displaycontrol &= ~LCD_BLINKON;
  133.         command(LCD_DISPLAYCONTROL | _displaycontrol);
  134. }
  135.  
  136. // Underline cursor on/off
  137. void serLCD::cursor(){
  138.         _displaycontrol |= LCD_CURSORON;
  139.         command(LCD_DISPLAYCONTROL | _displaycontrol);
  140. }
  141. void serLCD::noCursor(){
  142.         _displaycontrol &= ~LCD_CURSORON;
  143.         command(LCD_DISPLAYCONTROL | _displaycontrol);
  144. }
  145.  
  146. // Display on/off
  147. void serLCD::display(){
  148.         _displaycontrol |= LCD_DISPLAYON;
  149.         command(LCD_DISPLAYCONTROL | _displaycontrol);
  150. }
  151. void serLCD::noDisplay(){
  152.         _displaycontrol &= ~LCD_DISPLAYON;
  153.         command(LCD_DISPLAYCONTROL | _displaycontrol);
  154. }
  155.  
  156. // Set cursor to specific row and col values start at 1 not 0
  157. void serLCD::setCursor(int row, int col){
  158.         int row_offsets[2][4] = {
  159.                 { 0x00, 0x40, 0x10, 0x50 },
  160.                 { 0x00, 0x40, 0x14, 0x54 }
  161.         };
  162.         if((row > 0 && row < 3) && (col > 0 && col < 17)){
  163.            command(LCD_SETDDRAMADDR | ((col - 1) + row_offsets[_rowoffset][(row - 1)]));
  164.         }
  165. }
  166.  
  167. // Creates custom characters 8 char limit
  168. // Input values start with 1
  169. void serLCD::createChar(int location, uint8_t charmap[]){
  170.         location -= 1;
  171.         location &= 0x07;
  172.   for (int i=0; i<8; i++){
  173.     command(LCD_SETCGRAMADDR | (location << 3) | i);
  174.     write(charmap[i]);
  175.   }
  176. }
  177.  
  178. // Prints custom character
  179. // Input values start with 1
  180. void serLCD::printCustomChar(int num){
  181.         write((num - 1));
  182. }
  183.  
  184. // PRIVATE FUNCTIONS
  185.  
  186. // Functions for sending the special command values
  187. void serLCD::command(uint8_t value){
  188.         write(0xFE);
  189.         write(value);
  190.         delay(5);
  191. }
  192. void serLCD::specialCommand(uint8_t value){
  193.         write(0x7C);
  194.         write(value);
  195.         delay(5);
  196. }