Modified TextLCD library to support DOG series displays with ST7036 controller.

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TextLCD.cpp Source File

TextLCD.cpp

00001 /* mbed TextLCD Library, for a 4-bit LCD based on ST7036 (modified from HD44780 based version)
00002  * Copyright (c) 2007-2010, sford
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy
00005  * of this software and associated documentation files (the "Software"), to deal
00006  * in the Software without restriction, including without limitation the rights
00007  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008  * copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020  * THE SOFTWARE.
00021  */
00022 
00023 #include "TextLCD.h"
00024 #include "mbed.h"
00025 
00026 TextLCD::TextLCD(PinName rs, PinName e, PinName d0, PinName d1,
00027                  PinName d2, PinName d3, LCDType type) : _rs(rs),
00028         _e(e), _d(d0, d1, d2, d3),
00029         _type(type) {
00030 
00031     _e  = 1;
00032     _rs = 0;            // command mode
00033 
00034     wait(0.015);        // Wait 15ms to ensure powered up
00035 
00036     // send "Display Settings" 3 times (Only top nibble of 0x30 as we've got 4-bit bus)
00037     for (int i=0; i<3; i++) {
00038         writeByte(0x3);
00039         wait(0.00164);  // this command takes 1.64ms, so wait for it
00040     }
00041     writeByte(0x2);     // 4-bit mode
00042     wait(0.000040f);    // most instructions take 40us
00043 
00044     writeCommand(0x29); // Function set 001 BW N F - -
00045     writeCommand(0x14); // bias
00046     writeCommand(0x78); // Contrast set
00047     writeCommand(0x5e); // Power/ICON/Contrast
00048     writeCommand(0x6a); // Follower Control
00049     writeCommand(0x0c); // Display ON
00050     writeCommand(0x01); // Clear Display
00051     writeCommand(0x06); // Entry Mode Set
00052     cls();
00053 }
00054 
00055 void TextLCD::character(int column, int row, int c) {
00056     int a = address(column, row);
00057     writeCommand(a);
00058     writeData(c);
00059 }
00060 
00061 void TextLCD::cls() {
00062     writeCommand(0x01); // cls, and set cursor to 0
00063     wait(0.00164f);     // This command takes 1.64 ms
00064     locate(0, 0);
00065 }
00066 
00067 void TextLCD::locate(int column, int row) {
00068     _column = column;
00069     _row = row;
00070 }
00071 
00072 int TextLCD::_putc(int value) {
00073     if (value == '\n') {
00074         _column = 0;
00075         _row++;
00076         if (_row >= rows()) {
00077             _row = 0;
00078         }
00079     } else {
00080         character(_column, _row, value);
00081         _column++;
00082         if (_column >= columns()) {
00083             _column = 0;
00084             _row++;
00085             if (_row >= rows()) {
00086                 _row = 0;
00087             }
00088         }
00089     }
00090     return value;
00091 }
00092 
00093 int TextLCD::_getc() {
00094     return -1;
00095 }
00096 
00097 void TextLCD::writeByte(int value) {
00098     _d = value >> 4;
00099     wait(0.000040f); // most instructions take 40us
00100     _e = 0;
00101     wait(0.000040f);
00102     _e = 1;
00103     _d = value >> 0;
00104     wait(0.000040f);
00105     _e = 0;
00106     wait(0.000040f);  // most instructions take 40us
00107     _e = 1;
00108 }
00109 
00110 void TextLCD::writeCommand(int command) {
00111     _rs = 0;
00112     writeByte(command);
00113 }
00114 
00115 void TextLCD::writeData(int data) {
00116     _rs = 1;
00117     writeByte(data);
00118 }
00119 
00120 int TextLCD::address(int column, int row) {
00121     switch (_type) {
00122         case LCD20x4:
00123             switch (row) {
00124                 case 0:
00125                     return 0x80 + column;
00126                 case 1:
00127                     return 0xc0 + column;
00128                 case 2:
00129                     return 0x94 + column;
00130                 case 3:
00131                     return 0xd4 + column;
00132             }
00133         case LCD16x2B:
00134             return 0x80 + (row * 40) + column;
00135         case LCD16x2:
00136         case LCD20x2:
00137         default:
00138             return 0x80 + (row * 0x40) + column;
00139     }
00140 }
00141 
00142 int TextLCD::columns() {
00143     switch (_type) {
00144         case LCD20x4:
00145         case LCD20x2:
00146             return 20;
00147         case LCD16x2:
00148         case LCD16x2B:
00149         default:
00150             return 16;
00151     }
00152 }
00153 
00154 int TextLCD::rows() {
00155     switch (_type) {
00156         case LCD20x4:
00157             return 4;
00158         case LCD16x2:
00159         case LCD16x2B:
00160         case LCD20x2:
00161         default:
00162             return 2;
00163     }
00164 }