Library for the I2C connected LCD display EA DOGM204.
Example use of the LCD library.
main.cpp
include "mbed.h" #include "lcd_DOGM204_i2c.h" DOGM204I2C lcd(P0_0, P0_1, 0, 100000); int main() { lcd.init(); // Initialize display lcd.cls(); // Clear display lcd.display_set(DOGM204I2C::LCD_DISPLAY_ON); // optional | LCD_CURSOR_ON | LCD_BLINK_ON lcd.set_pos(DOGM204I2C::LCD_LINE1); lcd.write((char *)"*** Hello world *** "); lcd.set_pos(DOGM204I2C::LCD_LINE2); lcd.write((char *)"01234567890123456789"); lcd.set_pos(DOGM204I2C::LCD_LINE3); lcd.write((char *)"ABCDEFGHIJKLMNOPQRST"); lcd.set_pos(DOGM204I2C::LCD_LINE4); lcd.write((char *)"abcdefghijklmnopqrst"); }
Revision 0:ee339d42b34d, committed 2020-01-02
- Comitter:
- bcsd69
- Date:
- Thu Jan 02 16:20:12 2020 +0000
- Commit message:
- Library for the I2C connected EA DOG204 LCD display. Version 1.00.000.
Changed in this revision
lcd_DOGM204_i2c.cpp | Show annotated file Show diff for this revision Revisions of this file |
lcd_DOGM204_i2c.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r ee339d42b34d lcd_DOGM204_i2c.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lcd_DOGM204_i2c.cpp Thu Jan 02 16:20:12 2020 +0000 @@ -0,0 +1,178 @@ +/** EA DOGM204 LCD class + * + * Provides access to the I2C connected Electronic Assembly DOGM204 LCD display + * with SSD1803A controller (www.lcd-module.de) + * + * Version: 1.00.000 + * Date : 02.01.2020 + * Author: Marjan Hanc, www.m-hub.eu + * + * Note: This library does not support SPI and 4/8-Bit I/O modes of the display. + * It is assumed, that the RS address selection pin is statically connected + * either to the GND (SA=0) or VCC (SA=1). + **/ + +#include "lcd_DOGM204_i2c.h" +#include "mbed.h" + +/*\brief Initialises the LCD with respective I2C interface + \param sda I2C SDA pin mapping + \param scl I2C SCL pin mapping + \param SA SA0 (selector) address of the LCD display + \param freq Frequency of the I2C clock +*/ + +DOGM204I2C::DOGM204I2C(PinName sda, PinName scl, char SA, int frequency) : i2c(sda,scl) { + + if (SA > 1) + error("DOGM204I2C: SA is out of range, must be 0..1\n"); + else + _baseAdr = LCD_ADR + (SA << 1); // sets the base address of the LCD + + if (frequency > Frequency_400KHz) + error("DOGM204I2C: I2C frequency out of range, must be less than 400 kHz\n"); + + i2c.frequency(frequency); +} + +/*\brief Sends command with one data byte to the LCD + \param cmd LCD command + \param dta Data byte + \return status Returns true, if the command was successful +*/ + +bool DOGM204I2C::lcd_i2c_write(char cmd, char dta) +{ + char data[] = {cmd, dta}; + + if (i2c.write(_baseAdr, data, 2)) { + + return false; + } + + return true; +} + +/*\brief Reads one byte (command status) of the LCD + \param cmd LCD command + \return data Data byte +*/ + +char DOGM204I2C::lcd_i2c_read(char cmd) +{ + char data[] = {cmd}; + + i2c.write(_baseAdr, data, 1); // note, that the base address occupies bits b1..b7 + i2c.read(_baseAdr + 1, data, 1); // and that the b0 is R=1/W=0 + + return(data[0]); +} + +/*\brief Writes single command to the LCD by checking the display status first + \note The command is not fail safe. If the display doesn't return "not busy" it might hang forever + \param cmd LCD command + \return stat Returns true if successful +*/ + +bool DOGM204I2C::lcd_write_cmd(char data) +{ + //check and wait, if LCD is busy + while(lcd_i2c_read(LCD_STATUS) & LCD_BUSY); + + // write command, control byte C0=0 & D/C = 0 + return lcd_i2c_write(0x80, data); +} + +/*\brief Writes data byte to the LCD by checking the display's busy flag BF first + \note The command is not fail safe. If the display doesn't return "not busy" it might hang forever. + \param data LCD data + \return stat Returns true if successful +*/ +bool DOGM204I2C::lcd_write_data(char data) +{ + //check and wait, if LCD is busy + while(lcd_i2c_read(LCD_STATUS) & LCD_BUSY); + //write data, control byte C0=0 & D/C = 1 + return lcd_i2c_write(0x40, data); +} + +// -- Public functions -- + +/* \brief Sets the LCD display to the provided mode + * \param mode LCD_DISPLAY_ON|OFF LCD_CURSOR_ON|OFF LCD_BLINK_ON|OFF + */ +void DOGM204I2C::display_set(char mode) +{ + if (!lcd_write_cmd(0x08+mode)) + error("DOGM204I2C: Write command in lcd_display_set() failed!\n"); +} + +/* \brief Sets the cursor to the given position counted from the origin + * \param pos position + */ +void DOGM204I2C::set_pos(char pos) +{ + if (!lcd_write_cmd(LCD_HOME+pos)) + error("DOGM204I2C: Write command in lcd_set_pos() failed!\n"); +} + +/* \brief Writes a single character at given position in the given line + * \param pos LCD_LINEx+position + */ +void DOGM204I2C::write_char(char line, char pos, char ch) +{ + char lcdpos = pos + 0x20 * line; + + if (!lcd_write_cmd(lcdpos)) + error("DOGM204I2C: Write command in lcd_write_char() failed!\n"); + else + if (!lcd_write_data(ch)) + error("DOGM204I2C: Write data in lcd_write_char() failed!\n"); +} + +/* \brief Writes null terminated string to the LCD + * \param s Null terminated string + */ +void DOGM204I2C::write(char *s) +{ + while(*s) + { + if (!lcd_write_data(*s++)) { + error("DOGM204I2C: Write data in lcd_write() failed!\n"); + break; + } + } +} + +/* \brief Clears the LCD display and sets the cursor to the top-left position + */ +void DOGM204I2C::cls() +{ + lcd_write_cmd(LCD_CLEAR); // Clear display + lcd_write_cmd(LCD_RTHOME); // Set cursor position to home (top left) +} + +/* \brief Initializes LCD display + * \note This sequence will set 4 lines, 5 dots, and top orientation at maximum + contrast with cursor off + */ +void DOGM204I2C::init() +{ + lcd_write_cmd(0x3A); // 8-Bit data length, extension Bit RE=1; REV=0 + lcd_write_cmd(LCD_4LINE_MODE | LCD_FONT_5DOT ); // 4 lines, 5 dots charset + + lcd_write_cmd(0x80); // Pixel shift 0 + + lcd_write_cmd(LCD_TOPVIEW); // Set LCD orientation + lcd_write_cmd(0x1E); // Bias setting BS1=1 + + lcd_write_cmd(0x39); // 8-Bit data length extension Bit RE=0; IS=1 + lcd_write_cmd(0x1B); // BS0=1 -> Bias=1/6 + + lcd_write_cmd(0x6E); // Divider ON and set value + lcd_write_cmd(0x57); // Booster ON and set contrast (BB1=C5, DB0=C4) + lcd_write_cmd(0x7B); // Set optimum contrast (DB3-DB0=C3-C0) + + lcd_write_cmd(0x38); // 8-Bit data length extension Bit RE=0; IS=0 +} +
diff -r 000000000000 -r ee339d42b34d lcd_DOGM204_i2c.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lcd_DOGM204_i2c.h Thu Jan 02 16:20:12 2020 +0000 @@ -0,0 +1,96 @@ +/** EA DOGM204 LCD header file + * + * Provides access to the I2C connected Electronic Assembly DOGM204 LCD display + * with SSD1803A controller (www.lcd-module.de) + * + * Version: 1.00.000 + * Date : 02.01.2020 + * Author: Marjan Hanc, www.m-hub.eu + * + * Note: This library does not support SPI and 4/8-Bit I/O modes of display. + * It is assumed, that the RS address selection pin is statically connected + * either to the GND (SA0=0) or VCC (SA0=1). + **/ + +#ifndef MBED_LCD_DOGM204 +#define MBED_LCD_DOGM204 + +#include "mbed.h" + +class DOGM204I2C { + + public: + + enum LCD_Commands { + LCD_ADR = 0x78, // SA0=0, 0x7A when SA0=1 + LCD_CLEAR = 0x00, // Clear Display + LCD_RTHOME = 0x02 // Return home + }; + + enum LCD_Status { + LCD_STATUS = 0x00, + LCD_DATA = 0x40, + LCD_BUSY = 0x80 + }; + + enum LCD_Charset { + LCD_ROMA = 0x00, + LCD_ROMB = 0x04, + LCD_ROMC = 0x0C + }; + + enum LCD_Mode { + LCD_CURSOR_LINE = 0x00, + LCD_CURSOR_BLOCK = 0x02, + LCD_TOPVIEW = 0x05, + LCD_BOTVIEW = 0x06, + LCD_2LINE_MODE = 0x08, // Base setting for 1 & 2 line mode + LCD_4LINE_MODE = 0x09, // Base setting for 3 & 4 line mode + LCD_FONT_5DOT = 0x00, + LCD_FONT_6DOT = 0x04 + }; + + enum LCD_Settings { + LCD_DISPLAY_ON = 0x04, + LCD_DISPLAY_OFF = 0x03, + LCD_CURSOR_ON = 0x02, + LCD_CURSOR_OFF = 0x05, + LCD_BLINK_ON = 0x01, + LCD_BLINK_OFF = 0x06 + }; + + enum LCD_Positions { + LCD_HOME = 0x80, + LCD_LINE1 = 0x00, + LCD_LINE2 = 0x20, + LCD_LINE3 = 0x40, + LCD_LINE4 = 0x60 + }; + + enum Frequency { + Frequency_100KHz = 100000, + Frequency_400KHz = 400000 + }; + + DOGM204I2C(PinName sda, PinName scl, char SA, int frequency); + + void display_set(char mode); + void set_pos(char pos); + void write_char(char line, char pos, char ch); + void write(char *s); + void cls(); + void init(); + + private: + char _baseAdr; // LCD base address (0x78 or 0x7A, depends on SA0) + + I2C i2c; // I2C interface + + bool lcd_i2c_write(char cmd, char dta); + char lcd_i2c_read(char cmd); + bool lcd_write_cmd(char data); + bool lcd_write_data(char data); +}; + + +#endif