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"); }
lcd_DOGM204_i2c.cpp@0:ee339d42b34d, 2020-01-02 (annotated)
- Committer:
- bcsd69
- Date:
- Thu Jan 02 16:20:12 2020 +0000
- Revision:
- 0:ee339d42b34d
Library for the I2C connected EA DOG204 LCD display. Version 1.00.000.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bcsd69 | 0:ee339d42b34d | 1 | /** EA DOGM204 LCD class |
bcsd69 | 0:ee339d42b34d | 2 | * |
bcsd69 | 0:ee339d42b34d | 3 | * Provides access to the I2C connected Electronic Assembly DOGM204 LCD display |
bcsd69 | 0:ee339d42b34d | 4 | * with SSD1803A controller (www.lcd-module.de) |
bcsd69 | 0:ee339d42b34d | 5 | * |
bcsd69 | 0:ee339d42b34d | 6 | * Version: 1.00.000 |
bcsd69 | 0:ee339d42b34d | 7 | * Date : 02.01.2020 |
bcsd69 | 0:ee339d42b34d | 8 | * Author: Marjan Hanc, www.m-hub.eu |
bcsd69 | 0:ee339d42b34d | 9 | * |
bcsd69 | 0:ee339d42b34d | 10 | * Note: This library does not support SPI and 4/8-Bit I/O modes of the display. |
bcsd69 | 0:ee339d42b34d | 11 | * It is assumed, that the RS address selection pin is statically connected |
bcsd69 | 0:ee339d42b34d | 12 | * either to the GND (SA=0) or VCC (SA=1). |
bcsd69 | 0:ee339d42b34d | 13 | **/ |
bcsd69 | 0:ee339d42b34d | 14 | |
bcsd69 | 0:ee339d42b34d | 15 | #include "lcd_DOGM204_i2c.h" |
bcsd69 | 0:ee339d42b34d | 16 | #include "mbed.h" |
bcsd69 | 0:ee339d42b34d | 17 | |
bcsd69 | 0:ee339d42b34d | 18 | /*\brief Initialises the LCD with respective I2C interface |
bcsd69 | 0:ee339d42b34d | 19 | \param sda I2C SDA pin mapping |
bcsd69 | 0:ee339d42b34d | 20 | \param scl I2C SCL pin mapping |
bcsd69 | 0:ee339d42b34d | 21 | \param SA SA0 (selector) address of the LCD display |
bcsd69 | 0:ee339d42b34d | 22 | \param freq Frequency of the I2C clock |
bcsd69 | 0:ee339d42b34d | 23 | */ |
bcsd69 | 0:ee339d42b34d | 24 | |
bcsd69 | 0:ee339d42b34d | 25 | DOGM204I2C::DOGM204I2C(PinName sda, PinName scl, char SA, int frequency) : i2c(sda,scl) { |
bcsd69 | 0:ee339d42b34d | 26 | |
bcsd69 | 0:ee339d42b34d | 27 | if (SA > 1) |
bcsd69 | 0:ee339d42b34d | 28 | error("DOGM204I2C: SA is out of range, must be 0..1\n"); |
bcsd69 | 0:ee339d42b34d | 29 | else |
bcsd69 | 0:ee339d42b34d | 30 | _baseAdr = LCD_ADR + (SA << 1); // sets the base address of the LCD |
bcsd69 | 0:ee339d42b34d | 31 | |
bcsd69 | 0:ee339d42b34d | 32 | if (frequency > Frequency_400KHz) |
bcsd69 | 0:ee339d42b34d | 33 | error("DOGM204I2C: I2C frequency out of range, must be less than 400 kHz\n"); |
bcsd69 | 0:ee339d42b34d | 34 | |
bcsd69 | 0:ee339d42b34d | 35 | i2c.frequency(frequency); |
bcsd69 | 0:ee339d42b34d | 36 | } |
bcsd69 | 0:ee339d42b34d | 37 | |
bcsd69 | 0:ee339d42b34d | 38 | /*\brief Sends command with one data byte to the LCD |
bcsd69 | 0:ee339d42b34d | 39 | \param cmd LCD command |
bcsd69 | 0:ee339d42b34d | 40 | \param dta Data byte |
bcsd69 | 0:ee339d42b34d | 41 | \return status Returns true, if the command was successful |
bcsd69 | 0:ee339d42b34d | 42 | */ |
bcsd69 | 0:ee339d42b34d | 43 | |
bcsd69 | 0:ee339d42b34d | 44 | bool DOGM204I2C::lcd_i2c_write(char cmd, char dta) |
bcsd69 | 0:ee339d42b34d | 45 | { |
bcsd69 | 0:ee339d42b34d | 46 | char data[] = {cmd, dta}; |
bcsd69 | 0:ee339d42b34d | 47 | |
bcsd69 | 0:ee339d42b34d | 48 | if (i2c.write(_baseAdr, data, 2)) { |
bcsd69 | 0:ee339d42b34d | 49 | |
bcsd69 | 0:ee339d42b34d | 50 | return false; |
bcsd69 | 0:ee339d42b34d | 51 | } |
bcsd69 | 0:ee339d42b34d | 52 | |
bcsd69 | 0:ee339d42b34d | 53 | return true; |
bcsd69 | 0:ee339d42b34d | 54 | } |
bcsd69 | 0:ee339d42b34d | 55 | |
bcsd69 | 0:ee339d42b34d | 56 | /*\brief Reads one byte (command status) of the LCD |
bcsd69 | 0:ee339d42b34d | 57 | \param cmd LCD command |
bcsd69 | 0:ee339d42b34d | 58 | \return data Data byte |
bcsd69 | 0:ee339d42b34d | 59 | */ |
bcsd69 | 0:ee339d42b34d | 60 | |
bcsd69 | 0:ee339d42b34d | 61 | char DOGM204I2C::lcd_i2c_read(char cmd) |
bcsd69 | 0:ee339d42b34d | 62 | { |
bcsd69 | 0:ee339d42b34d | 63 | char data[] = {cmd}; |
bcsd69 | 0:ee339d42b34d | 64 | |
bcsd69 | 0:ee339d42b34d | 65 | i2c.write(_baseAdr, data, 1); // note, that the base address occupies bits b1..b7 |
bcsd69 | 0:ee339d42b34d | 66 | i2c.read(_baseAdr + 1, data, 1); // and that the b0 is R=1/W=0 |
bcsd69 | 0:ee339d42b34d | 67 | |
bcsd69 | 0:ee339d42b34d | 68 | return(data[0]); |
bcsd69 | 0:ee339d42b34d | 69 | } |
bcsd69 | 0:ee339d42b34d | 70 | |
bcsd69 | 0:ee339d42b34d | 71 | /*\brief Writes single command to the LCD by checking the display status first |
bcsd69 | 0:ee339d42b34d | 72 | \note The command is not fail safe. If the display doesn't return "not busy" it might hang forever |
bcsd69 | 0:ee339d42b34d | 73 | \param cmd LCD command |
bcsd69 | 0:ee339d42b34d | 74 | \return stat Returns true if successful |
bcsd69 | 0:ee339d42b34d | 75 | */ |
bcsd69 | 0:ee339d42b34d | 76 | |
bcsd69 | 0:ee339d42b34d | 77 | bool DOGM204I2C::lcd_write_cmd(char data) |
bcsd69 | 0:ee339d42b34d | 78 | { |
bcsd69 | 0:ee339d42b34d | 79 | //check and wait, if LCD is busy |
bcsd69 | 0:ee339d42b34d | 80 | while(lcd_i2c_read(LCD_STATUS) & LCD_BUSY); |
bcsd69 | 0:ee339d42b34d | 81 | |
bcsd69 | 0:ee339d42b34d | 82 | // write command, control byte C0=0 & D/C = 0 |
bcsd69 | 0:ee339d42b34d | 83 | return lcd_i2c_write(0x80, data); |
bcsd69 | 0:ee339d42b34d | 84 | } |
bcsd69 | 0:ee339d42b34d | 85 | |
bcsd69 | 0:ee339d42b34d | 86 | /*\brief Writes data byte to the LCD by checking the display's busy flag BF first |
bcsd69 | 0:ee339d42b34d | 87 | \note The command is not fail safe. If the display doesn't return "not busy" it might hang forever. |
bcsd69 | 0:ee339d42b34d | 88 | \param data LCD data |
bcsd69 | 0:ee339d42b34d | 89 | \return stat Returns true if successful |
bcsd69 | 0:ee339d42b34d | 90 | */ |
bcsd69 | 0:ee339d42b34d | 91 | bool DOGM204I2C::lcd_write_data(char data) |
bcsd69 | 0:ee339d42b34d | 92 | { |
bcsd69 | 0:ee339d42b34d | 93 | //check and wait, if LCD is busy |
bcsd69 | 0:ee339d42b34d | 94 | while(lcd_i2c_read(LCD_STATUS) & LCD_BUSY); |
bcsd69 | 0:ee339d42b34d | 95 | //write data, control byte C0=0 & D/C = 1 |
bcsd69 | 0:ee339d42b34d | 96 | return lcd_i2c_write(0x40, data); |
bcsd69 | 0:ee339d42b34d | 97 | } |
bcsd69 | 0:ee339d42b34d | 98 | |
bcsd69 | 0:ee339d42b34d | 99 | // -- Public functions -- |
bcsd69 | 0:ee339d42b34d | 100 | |
bcsd69 | 0:ee339d42b34d | 101 | /* \brief Sets the LCD display to the provided mode |
bcsd69 | 0:ee339d42b34d | 102 | * \param mode LCD_DISPLAY_ON|OFF LCD_CURSOR_ON|OFF LCD_BLINK_ON|OFF |
bcsd69 | 0:ee339d42b34d | 103 | */ |
bcsd69 | 0:ee339d42b34d | 104 | void DOGM204I2C::display_set(char mode) |
bcsd69 | 0:ee339d42b34d | 105 | { |
bcsd69 | 0:ee339d42b34d | 106 | if (!lcd_write_cmd(0x08+mode)) |
bcsd69 | 0:ee339d42b34d | 107 | error("DOGM204I2C: Write command in lcd_display_set() failed!\n"); |
bcsd69 | 0:ee339d42b34d | 108 | } |
bcsd69 | 0:ee339d42b34d | 109 | |
bcsd69 | 0:ee339d42b34d | 110 | /* \brief Sets the cursor to the given position counted from the origin |
bcsd69 | 0:ee339d42b34d | 111 | * \param pos position |
bcsd69 | 0:ee339d42b34d | 112 | */ |
bcsd69 | 0:ee339d42b34d | 113 | void DOGM204I2C::set_pos(char pos) |
bcsd69 | 0:ee339d42b34d | 114 | { |
bcsd69 | 0:ee339d42b34d | 115 | if (!lcd_write_cmd(LCD_HOME+pos)) |
bcsd69 | 0:ee339d42b34d | 116 | error("DOGM204I2C: Write command in lcd_set_pos() failed!\n"); |
bcsd69 | 0:ee339d42b34d | 117 | } |
bcsd69 | 0:ee339d42b34d | 118 | |
bcsd69 | 0:ee339d42b34d | 119 | /* \brief Writes a single character at given position in the given line |
bcsd69 | 0:ee339d42b34d | 120 | * \param pos LCD_LINEx+position |
bcsd69 | 0:ee339d42b34d | 121 | */ |
bcsd69 | 0:ee339d42b34d | 122 | void DOGM204I2C::write_char(char line, char pos, char ch) |
bcsd69 | 0:ee339d42b34d | 123 | { |
bcsd69 | 0:ee339d42b34d | 124 | char lcdpos = pos + 0x20 * line; |
bcsd69 | 0:ee339d42b34d | 125 | |
bcsd69 | 0:ee339d42b34d | 126 | if (!lcd_write_cmd(lcdpos)) |
bcsd69 | 0:ee339d42b34d | 127 | error("DOGM204I2C: Write command in lcd_write_char() failed!\n"); |
bcsd69 | 0:ee339d42b34d | 128 | else |
bcsd69 | 0:ee339d42b34d | 129 | if (!lcd_write_data(ch)) |
bcsd69 | 0:ee339d42b34d | 130 | error("DOGM204I2C: Write data in lcd_write_char() failed!\n"); |
bcsd69 | 0:ee339d42b34d | 131 | } |
bcsd69 | 0:ee339d42b34d | 132 | |
bcsd69 | 0:ee339d42b34d | 133 | /* \brief Writes null terminated string to the LCD |
bcsd69 | 0:ee339d42b34d | 134 | * \param s Null terminated string |
bcsd69 | 0:ee339d42b34d | 135 | */ |
bcsd69 | 0:ee339d42b34d | 136 | void DOGM204I2C::write(char *s) |
bcsd69 | 0:ee339d42b34d | 137 | { |
bcsd69 | 0:ee339d42b34d | 138 | while(*s) |
bcsd69 | 0:ee339d42b34d | 139 | { |
bcsd69 | 0:ee339d42b34d | 140 | if (!lcd_write_data(*s++)) { |
bcsd69 | 0:ee339d42b34d | 141 | error("DOGM204I2C: Write data in lcd_write() failed!\n"); |
bcsd69 | 0:ee339d42b34d | 142 | break; |
bcsd69 | 0:ee339d42b34d | 143 | } |
bcsd69 | 0:ee339d42b34d | 144 | } |
bcsd69 | 0:ee339d42b34d | 145 | } |
bcsd69 | 0:ee339d42b34d | 146 | |
bcsd69 | 0:ee339d42b34d | 147 | /* \brief Clears the LCD display and sets the cursor to the top-left position |
bcsd69 | 0:ee339d42b34d | 148 | */ |
bcsd69 | 0:ee339d42b34d | 149 | void DOGM204I2C::cls() |
bcsd69 | 0:ee339d42b34d | 150 | { |
bcsd69 | 0:ee339d42b34d | 151 | lcd_write_cmd(LCD_CLEAR); // Clear display |
bcsd69 | 0:ee339d42b34d | 152 | lcd_write_cmd(LCD_RTHOME); // Set cursor position to home (top left) |
bcsd69 | 0:ee339d42b34d | 153 | } |
bcsd69 | 0:ee339d42b34d | 154 | |
bcsd69 | 0:ee339d42b34d | 155 | /* \brief Initializes LCD display |
bcsd69 | 0:ee339d42b34d | 156 | * \note This sequence will set 4 lines, 5 dots, and top orientation at maximum |
bcsd69 | 0:ee339d42b34d | 157 | contrast with cursor off |
bcsd69 | 0:ee339d42b34d | 158 | */ |
bcsd69 | 0:ee339d42b34d | 159 | void DOGM204I2C::init() |
bcsd69 | 0:ee339d42b34d | 160 | { |
bcsd69 | 0:ee339d42b34d | 161 | lcd_write_cmd(0x3A); // 8-Bit data length, extension Bit RE=1; REV=0 |
bcsd69 | 0:ee339d42b34d | 162 | lcd_write_cmd(LCD_4LINE_MODE | LCD_FONT_5DOT ); // 4 lines, 5 dots charset |
bcsd69 | 0:ee339d42b34d | 163 | |
bcsd69 | 0:ee339d42b34d | 164 | lcd_write_cmd(0x80); // Pixel shift 0 |
bcsd69 | 0:ee339d42b34d | 165 | |
bcsd69 | 0:ee339d42b34d | 166 | lcd_write_cmd(LCD_TOPVIEW); // Set LCD orientation |
bcsd69 | 0:ee339d42b34d | 167 | lcd_write_cmd(0x1E); // Bias setting BS1=1 |
bcsd69 | 0:ee339d42b34d | 168 | |
bcsd69 | 0:ee339d42b34d | 169 | lcd_write_cmd(0x39); // 8-Bit data length extension Bit RE=0; IS=1 |
bcsd69 | 0:ee339d42b34d | 170 | lcd_write_cmd(0x1B); // BS0=1 -> Bias=1/6 |
bcsd69 | 0:ee339d42b34d | 171 | |
bcsd69 | 0:ee339d42b34d | 172 | lcd_write_cmd(0x6E); // Divider ON and set value |
bcsd69 | 0:ee339d42b34d | 173 | lcd_write_cmd(0x57); // Booster ON and set contrast (BB1=C5, DB0=C4) |
bcsd69 | 0:ee339d42b34d | 174 | lcd_write_cmd(0x7B); // Set optimum contrast (DB3-DB0=C3-C0) |
bcsd69 | 0:ee339d42b34d | 175 | |
bcsd69 | 0:ee339d42b34d | 176 | lcd_write_cmd(0x38); // 8-Bit data length extension Bit RE=0; IS=0 |
bcsd69 | 0:ee339d42b34d | 177 | } |
bcsd69 | 0:ee339d42b34d | 178 |