Matteo Costa / PCF2119LCD
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers pcf2119.cpp Source File

pcf2119.cpp

00001 /* mbed CD Library, for a i2c LCD based on PCF2119
00002  * Copyright (c) 2016, aracosta, http://mbed.org
00003  * Copyright (c) 2007-2010, sford, http://mbed.org
00004  *
00005  * Permission is hereby granted, free of charge, to any person obtaining a copy
00006  * of this software and associated documentation files (the "Software"), to deal
00007  * in the Software without restriction, including without limitation the rights
00008  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00009  * copies of the Software, and to permit persons to whom the Software is
00010  * furnished to do so, subject to the following conditions:
00011  *
00012  * The above copyright notice and this permission notice shall be included in
00013  * all copies or substantial portions of the Software.
00014  *
00015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00018  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00020  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00021  * THE SOFTWARE.
00022  */
00023 
00024 #include "pcf2119.h"
00025 #include "mbed.h"
00026 
00027 extern Serial pc;
00028 
00029 TextLCD::TextLCD(void) : _i2c(NC, NC), _reset(NC)
00030 {
00031     return;
00032 }
00033 
00034 TextLCD::TextLCD(PinName pSDA, PinName pSCL, PinName pReset, LCDType type) : _i2c(pSDA, pSCL), _reset(pReset), _type(type) {  
00035     if (_type != LCDuser)
00036         setLCDparam(_type); // otherwise rows, colums, comdelay, adresses must be set before
00037 
00038 
00039     _reset = 1;
00040     wait(0.05f);
00041     _reset = 0;
00042     wait(0.05f);
00043     _reset = 1;
00044     wait(0.05f);
00045     _reset = 0;
00046     
00047     wait(0.2f);       // Wait 200ms to ensure powered up
00048   
00049     writeCommand(0x24); // Function set 4 Bit, 2Line, 5*7
00050     cls();              // clear display, reset _column and _row
00051     writeCommand(0x0c); // Display on
00052     writeCommand(0x06); // cursor right, Display is not shifted
00053 }
00054 
00055 void TextLCD::character(int column, int row, int c) {
00056     int a = 0x80 | (LCDparam.addresses[row & 3] + column);
00057     writeCommand(a);    // set cursor address
00058     writeData(c + 0x80);       // write char
00059 }
00060 
00061 void TextLCD::cls() {
00062     writeCommand(0x08); // Display on
00063     int a = 0x80 | (LCDparam.addresses[0 & 3] + 0);
00064     writeCommand(a);    // set cursor address
00065     writeCommand(0x80); // set cursor address
00066     for( int i=0;i<LCDparam.columns;i++) {
00067         writeData(' ' + 0x80);       // write char
00068     }
00069     a = 0x80 | (LCDparam.addresses[1 & 3] + 0);
00070     writeCommand(a);    // set cursor address
00071     for( int i=0;i<LCDparam.columns;i++) {
00072         writeData(' ' + 0x80);       // write char
00073     }
00074     locate(0, 0);       // set internal position
00075     writeCommand(0x0c); // Display on
00076     wait_us(45 * LCDparam.delay);   // CLS need much time
00077 }
00078 
00079 void TextLCD::locate(int column, int row) {
00080     _column = column;   // set position for next char
00081     _row = row;         // note: cursor is not set yet
00082 }
00083 
00084 int TextLCD::_putc(int value) {
00085     if (value == '\n') {
00086         _column = 0;
00087         _row++;
00088         if (_row >= LCDparam.rows) {
00089             _row = 0;
00090         }
00091     } else {
00092         character(_column, _row, value);
00093         _column++;
00094         if (_column >= LCDparam.columns) {
00095             _column = 0;
00096             _row++;
00097             if (_row >= LCDparam.rows) {
00098                 _row = 0;
00099             }
00100         }
00101     }
00102     return value;
00103 }
00104 
00105 // Dummy function - read not supported
00106 int TextLCD::_getc() {
00107     return -1;
00108 }
00109 
00110 void TextLCD::writeCommand(int command) {
00111     _i2cbuffer[0] = 0x00;
00112     _i2cbuffer[1] = command;
00113     _i2c.write(LCDparam.i2c_address, _i2cbuffer, 2, false);
00114     wait_us(LCDparam.delay);
00115 }
00116 
00117 
00118 void TextLCD::writeData(int data) {
00119     _i2cbuffer[0] = 0x40;
00120     _i2cbuffer[1] = data;
00121     _i2c.write(LCDparam.i2c_address, _i2cbuffer, 2, false);
00122     wait_us(LCDparam.delay);
00123 }
00124 
00125 
00126 // set user defined char 
00127 void  TextLCD::writeCGRAM(int address, int pattern[8]){
00128     int i;
00129     address = address & 7;  //max 8 char
00130     for(i=0;i<8;i++){
00131         writeCommand(0x40 | (address * 8) + i);
00132         writeData(pattern[i]);
00133     }
00134 }   
00135 
00136 void TextLCD::setLCDparam(LCDType _type){
00137     switch (_type) {
00138         case LCD16x2:
00139         case LCD16x2B:
00140             LCDparam.columns = 16;
00141             break;
00142         case LCD20x2:
00143         case LCD20x4:
00144             LCDparam.columns = 20;
00145             break;
00146         case LCD24x2:
00147             LCDparam.columns = 24;
00148             break;
00149     }
00150     if (_type == LCD20x4) 
00151         LCDparam.rows = 4;
00152     else 
00153         LCDparam.rows = 2;
00154         
00155     LCDparam.addresses[0] = 0;
00156     
00157     if (_type == LCD16x2B)
00158         LCDparam.addresses[1] = 40;
00159     else
00160         LCDparam.addresses[1] = 0x40;
00161         
00162     if (_type == LCD20x4) {
00163         LCDparam.addresses[2] = 0x14;
00164         LCDparam.addresses[3] = 0x54;}
00165     else {
00166         LCDparam.addresses[2] = 0;
00167         LCDparam.addresses[3] = 0;}
00168         
00169     LCDparam.delay = 50;            // 50 us delays as default       
00170     
00171     LCDparam.i2c_address = 0x76;       
00172 }
00173