Rod Coleman / I2CTextLCD
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers I2CTextLCD.cpp Source File

I2CTextLCD.cpp

00001 /* mbed I2CTextLCD Library
00002  * Copyright (c) 2007-2009 sford
00003  * Copyright (c) 2010 Wim De Roeve  changed to work with I2C PCF8575
00004  * Released under the MIT License: http://mbed.org/license/mit
00005  */
00006 
00007 #include "I2CTextLCD.h"
00008 #include "mbed.h"
00009 #include "error.h"
00010 
00011 using namespace mbed;
00012 
00013 /*
00014      * useful info found at http://www.a-netz.de/lcd.en.php
00015      *
00016      *
00017      * Initialisation
00018      * ==============
00019      *
00020      * After attaching the supply voltage/after a reset, the display needs to be brought in to a defined state
00021      *
00022      * - wait approximately 15 ms so the display is ready to execute commands
00023      * - Execute the command 0x30 ("Display Settings") three times (wait 1,64ms after each command, the busy flag cannot be queried now).
00024      * - The display is in 8 bit mode, so if you have only connected 4 data pins you should only transmit the higher nibble of each command.
00025      * - If you want to use the 4 bit mode, now you can execute the command to switch over to this mode now.
00026          * - Execute the "clear display" command
00027      *
00028      * Timing
00029      * ======
00030      *
00031      * Nearly all commands transmitted to the display need 40us for execution.
00032      * Exceptions are the commands "Clear Display and Reset" and "Set Cursor to Start Position"
00033      * These commands need 1.64ms for execution. These timings are valid for all displays working with an
00034      * internal clock of 250kHz. But I do not know any displays that use other frequencies. Any time you
00035      * can use the busy flag to test if the display is ready to accept the next command.
00036      *
00037      */
00038 
00039 I2CTextLCD::I2CTextLCD(PinName sda, PinName scl, int i2cAddress , int columns, int rows,
00040                        bool backlight) : _i2c(sda, scl) {
00041 
00042     _i2cAddress = i2cAddress;
00043     _columns = columns;
00044     _rows = rows;
00045     _backlight=backlight;
00046     _i2c.frequency(70000); //  RC:2011-12-1 put this back in
00047  // Winstar 20x4 WH2004-NYG- needs 40ms after VDD> 4,5V
00048     /*
00049     wait(0.5);
00050     writeCommand(0x2); // 4-bit mode
00051     wait(0.05);
00052     writeCommand(0x28);    // Function set 001 BW N F - -
00053     wait(0.05);
00054     */
00055     //RC:2011-11-23: Newhaven 20x4 OLED data sheet method
00056     writeCommand(0x2); // 4-bit mode
00057     wait(0.05);
00058     writeCommand(0x2); // 4-bit mode
00059     wait(0.05);
00060     writeCommand(0x28); // display OFF, "Function Set". Newhaven say 0x08, but this loses 2 rows!
00061     wait(0.05); 
00062     writeCommand(0x1); // display clear
00063     wait(0.05);
00064     writeCommand(0x6); // entry mode set
00065     wait(0.05); 
00066     writeCommand(0x2); // 4-bit mode
00067     wait(0.05);                  
00068     writeCommand(0x0C); // ON-OFF ctrl: turns display ON, no cursor. Use 0x0E for cursor ON.
00069     /* 0x28 also works for Winstar WEH002004ALPP5N00000 OLED display. 0x29= westEuro fon table, 0x2A = UK/Russian
00070 */
00071     // Added RC 2011-8-11
00072     /*
00073     writeCommand(0x05); // clear display RAM all to 00
00074     wait(0.05);        // 6.2ms specified for OLED display to recover from RAM clear
00075     
00076     writeCommand(0x06);  //  Entry mode Set. Cursor Direction and Display Shift : 0000 01 CD S (CD 0-left, 1-right S(hift) 0-no, 1-yes
00077     wait(0.05); 
00078    writeCommand(0x14);  // OLED display shift disable etc
00079     wait(0.05);
00080     writeCommand(0x17);  // disable graphic mode, power ON
00081     //cls();
00082     wait(0.05);
00083     writeCommand (0x01);  //clear entire display.
00084     wait(0.07);
00085 */
00086 
00087 }
00088 
00089 int I2CTextLCD::_putc(int value) {
00090     if (value == '\n') {
00091         newline();
00092     } else {
00093         writeData(value);
00094     }
00095     return value;
00096 }
00097 
00098 int I2CTextLCD::_getc() {
00099     return 0;
00100 }
00101 
00102 /* void I2CTextLCD::backlight(bool status) {
00103     _backlight=status;
00104     if (_backlight)
00105         writeI2CByte(BACKLIGHT_ON | E1_ON);
00106     else
00107         writeI2CByte(E1_ON);
00108 }
00109 */
00110 
00111 void I2CTextLCD::newline() {
00112     _column = 0;
00113     _row++;
00114     if (_row >= _rows) {
00115         _row = 0;
00116     }
00117     locate(_column, _row);
00118 }
00119 
00120 void I2CTextLCD::locate(int column, int row) {
00121     if (column < 0 || column >= _columns || row < 0 || row >= _rows) {
00122         error("locate(%d,%d) out of range on %dx%d display", column, row, _columns, _rows);
00123         return;
00124     }
00125     _row = row;
00126     _column = column;
00127     int address = 0x80;
00128     if (row==0){
00129     address = 0x80+_column;
00130     }
00131     else if (row==1){
00132     address= 0xc0+_column;
00133     }
00134     else if (row==2){
00135     address=0x94+_column;
00136     }
00137     else if(row==3){
00138     address=0xd4+_column;
00139     }
00140     /*
00141     int address = 0x80 + (_row * 40) + _column; // memory starts at 0x00, and is 40 chars long per row
00142     Set bit 7 also, to signify  ADDRESS SET mode.
00143      // pc_LCD.traceOut("locate %dx%d\r\n", column, row);
00144      */
00145     writeCommand(address);  // must set bit 7 to indicate address SET command
00146     wait(0.004);      // takes 1.5ms on Winstar LCD 20x4
00147 }
00148 
00149 void I2CTextLCD::cls() {
00150     writeCommand(0x01); // Clear Display
00151     wait(0.01f);     // This command takes 1.64 ms (LCD), 6.2ms for Winstar OLED
00152     locate(0, 0);
00153 }
00154 
00155 void I2CTextLCD::reset() {
00156     cls();
00157 }
00158 
00159 void I2CTextLCD::writeByte(int c, bool rs) {  // This is the actual I2C transfer of the display data:
00160         char cmd[2]; //led = 1;
00161         cmd[0]=c>>2;                  // upper nibble
00162         cmd[0]=cmd[0] & 0x3c;         // mask out bits other than 5:2 for data (RS and E are enabled separately, below:)
00163         cmd[1]=c<<2;                 // lower nibble
00164         cmd[1]=cmd[1] & 0x3c;
00165         if (rs) {
00166         cmd[0]=cmd[0] | 0x01;        // RS selects display DATA or a COMMAND. It's on bit 0:
00167         cmd[1]=cmd[1] | 0x01;
00168         }
00169         _i2c.write( _i2cAddress,cmd,1); // E=0, write upper nibble. E's on bit 1!
00170         wait_us(1);
00171         cmd[0]=cmd[0]|0x02; //E=1
00172         _i2c.write( _i2cAddress, cmd,1); 
00173         wait_us(1);
00174         cmd[0]=cmd[0]&0x3d; //E=0, RS preserved in bit 0
00175         _i2c.write( _i2cAddress, cmd,1); 
00176         wait_us(1);
00177         cmd[0]=cmd[1]; //E=0, write lower nibble
00178         wait_us(1);
00179         cmd[0]=cmd[0]|0x02; //E=1
00180         _i2c.write( _i2cAddress, cmd,1); 
00181         wait_us(1);
00182         cmd[0]=cmd[0]&0x3d; //E=0, RS preserved in bit 0
00183         _i2c.write( _i2cAddress, cmd,1);
00184         _i2c.write ( _i2cAddress,cmd,1);              
00185         // led = 0;
00186         wait_us(1);
00187     }
00188  // This is the original authors method:
00189     //void I2CTextLCD::writeByte(int data, bool rs) {
00190 
00191 
00192 /*
00193     writeNibble(data >> 4 , rs);
00194     writeNibble(data >> 0 , rs);
00195     RC 2011-8-11. change to d4..d7 - PCF8574 ports P2 .. P5
00196 */
00197 //data = data>>2;
00198 //data = data & 0x3c;
00199 //writeNibble(data , rs);
00200 //data = data<<2;
00201 //data = data & 0x3c;
00202 //writeNibble(data , rs);
00203 
00204 void I2CTextLCD::writeCommand(int command) {
00205     // RS = 0;
00206     writeByte(command,false);
00207 }
00208 
00209 void I2CTextLCD::writeData(int data) {
00210     //RS = 1
00211     writeByte(data,true);
00212 
00213     _column++;
00214     if (_column >= _columns) {
00215         newline();
00216     }
00217 }
00218 
00219 /* void I2CTextLCD::writeI2CByte(int data) {
00220     char cmd[2];
00221     cmd[0] = (data & 0xFF);
00222     cmd[1] = (data >> 8);
00223      _i2c.write(_i2cAddress, cmd, 2);
00224 */
00225 
00226 
00227 
00228 
00229 
00230