/* * Arduino library to drive an LCD display that uses an SPI controller compatible with the HD44780 * Based on the structure of the standard LiquidCrystal.cpp routines for the Hitachi HD44780 chip * The commands seem to be a subset of the HD44780, so this is usable as a replacement * for the LiquidCrystal library, but compatibility hasn't been extensively tested. * * I haven't yet discovered exactly which controller chip they contain * They have a 6 pin, 1mm spaced, PCB flex cable, connected as follows: * 1 MOSI * 2 SCLK * 3 SS * 4 GND * 5 +5V * 6 ? (grounded) * There is no command/data (RS) line, it appears that it assumes the first byte of a * sequence, with SS held low, is a command, the rest data. So I send a display-on command * then data to write to the screen, or a cgram-address-set command then data to write to cgram * (the original firmware used a cursor position command, then data, to write to the screen) * * Thanks to the authors of the standard LiquidCrystal.cpp HD44780 library. * Author Tony Wills * Licensed CC-BY-SA * * 20181217 Initial release version 1.00 * see http://www.arduino.scorchingbay.nz (or an archive.org copy) for more information */ #include "LiquidCrystal_SPI.h" #include #include #include #if defined(ARDUINO) && ARDUINO >= 100 #include "Arduino.h" #else #include "WProgram.h" #endif LiquidCrystal_SPI::LiquidCrystal_SPI(uint8_t sclk, uint8_t mosi, uint8_t miso, uint8_t ss) { init(sclk, mosi, miso, ss); } LiquidCrystal_SPI::LiquidCrystal_SPI(uint8_t sclk, uint8_t mosi, uint8_t ss) { init(sclk, mosi, 255, ss); } void LiquidCrystal_SPI::init(uint8_t sclk, uint8_t mosi, uint8_t miso, uint8_t ss) { _sclk_pin = sclk; _mosi_pin = mosi; _miso_pin = miso; _ss_pin = ss; digitalWrite(_sclk_pin, HIGH); pinMode(_sclk_pin, OUTPUT); digitalWrite(_mosi_pin, HIGH); pinMode(_mosi_pin, OUTPUT); digitalWrite(_ss_pin, HIGH); pinMode(_ss_pin, OUTPUT); // not supporting miso pin yet, but for future options if (_miso_pin != 255) { pinMode(_miso_pin, INPUT); } _displayfunction = LCD_8BITMODE | LCD_1LINE | LCD_5x8DOTS; //begin(16, 1); } void LiquidCrystal_SPI::begin(uint8_t cols, uint8_t lines, uint8_t dotsize) { if (lines > 1) { _displayfunction |= LCD_2LINE; } _numlines = lines; _currline = 0; // for some 1 line displays you can select a 10 pixel high font if ((dotsize != 0) && (lines == 1)) { _displayfunction |= LCD_5x10DOTS; } command(LCD_FUNCTIONSET | _displayfunction); command(LCD_DISPLAYCONTROL | LCD_CURSOROFF | LCD_BLINKOFF); command(0xf8); //reset perhaps? // turn the display on with no cursor or blinking default _displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF; display(); // clear it off clear(); // Initialize to default text direction (for romance languages) _displaymode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT; // set the entry mode command(LCD_ENTRYMODESET | _displaymode); } /********** high level commands, for the user! */ void LiquidCrystal_SPI::clear() { command(LCD_CLEARDISPLAY); // clear display, set cursor position to zero delay(20); // this command takes a long time! } void LiquidCrystal_SPI::home() { command(LCD_RETURNHOME); // set cursor position to zero delayMicroseconds(2000); // this command takes a long time! } void LiquidCrystal_SPI::setCursor(uint8_t col, uint8_t row) { int row_offsets[] = { 0x00, 0x40, 0x14, 0x54 }; if ( row > _numlines ) { row = _numlines-1; // we count rows starting w/0 } command(LCD_SETDDRAMADDR | (col + row_offsets[row])); } // Turn the display on/off (quickly) void LiquidCrystal_SPI::noDisplay() { _displaycontrol &= ~LCD_DISPLAYON; command(LCD_DISPLAYCONTROL | _displaycontrol); } void LiquidCrystal_SPI::display() { _displaycontrol |= LCD_DISPLAYON; command(LCD_DISPLAYCONTROL | _displaycontrol); } // Turns the underline cursor on/off void LiquidCrystal_SPI::noCursor() { _displaycontrol &= ~LCD_CURSORON; command(LCD_DISPLAYCONTROL | _displaycontrol); } void LiquidCrystal_SPI::cursor() { _displaycontrol |= LCD_CURSORON; command(LCD_DISPLAYCONTROL | _displaycontrol); } // Turn on and off the blinking cursor void LiquidCrystal_SPI::noBlink() { _displaycontrol &= ~LCD_BLINKON; command(LCD_DISPLAYCONTROL | _displaycontrol); } void LiquidCrystal_SPI::blink() { _displaycontrol |= LCD_BLINKON; command(LCD_DISPLAYCONTROL | _displaycontrol); } // These commands scroll the display without changing the RAM void LiquidCrystal_SPI::scrollDisplayLeft(void) { command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVELEFT); } void LiquidCrystal_SPI::scrollDisplayRight(void) { command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT); } // This is for text that flows Left to Right void LiquidCrystal_SPI::leftToRight(void) { _displaymode |= LCD_ENTRYLEFT; command(LCD_ENTRYMODESET | _displaymode); } // This is for text that flows Right to Left void LiquidCrystal_SPI::rightToLeft(void) { _displaymode &= ~LCD_ENTRYLEFT; command(LCD_ENTRYMODESET | _displaymode); } // This will 'right justify' text from the cursor void LiquidCrystal_SPI::autoscroll(void) { _displaymode |= LCD_ENTRYSHIFTINCREMENT; command(LCD_ENTRYMODESET | _displaymode); } // This will 'left justify' text from the cursor void LiquidCrystal_SPI::noAutoscroll(void) { _displaymode &= ~LCD_ENTRYSHIFTINCREMENT; command(LCD_ENTRYMODESET | _displaymode); } // Allows us to fill the first 8 CGRAM locations // with custom characters void LiquidCrystal_SPI::createChar(uint8_t location, uint8_t charmap[]) { location &= 0x03; // we only have 4 locations 0-3 sequenceBegin(); send(LCD_SETCGRAMADDR | (location << 3)); for (uint8_t i=0; i<8; i++) { send(charmap[i]); } sequenceEnd(); setCursor(0,0); // return to writing data ram } /*********** mid level commands, for sending data/cmds */ // used for singe byte commands inline void LiquidCrystal_SPI::command(uint8_t value) { sequenceBegin(); send(value); sequenceEnd(); } // used by print library to send characters to display inline size_t LiquidCrystal_SPI::write(uint8_t value) { sequenceBegin(); send(LCD_DISPLAYCONTROL|LCD_DISPLAYON); send(value); sequenceEnd(); return 1; // assume sucess } /************ low level data pushing commands **********/ // send either command or data aas part of a sequence of one or more bytes void LiquidCrystal_SPI::send(uint8_t value) { write8bits(value); delayMicroseconds(800); } // select LCD to begin writing command and possibly data void LiquidCrystal_SPI::sequenceBegin(void) { digitalWrite(_ss_pin, LOW); } // deselect LCD, writing done void LiquidCrystal_SPI::sequenceEnd(void) { digitalWrite(_ss_pin, HIGH); delayMicroseconds(800); } // bit bang the data out, LSB first void LiquidCrystal_SPI::write8bits(uint8_t value) { for (int i = 0; i < 8; i++) { digitalWrite(_mosi_pin, !!((value >> i) & 0x01)); digitalWrite(_sclk_pin, LOW); delayMicroseconds(28); digitalWrite(_sclk_pin, HIGH); delayMicroseconds(28); } digitalWrite(_mosi_pin,HIGH); }