Dependencies:   ChaNFSSD mbed BMP085 SHT2x

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TextLCD_20X4.cpp Source File

TextLCD_20X4.cpp

00001 /* mbed TextLCD Library
00002  * Copyright (c) 2007-2009 sford
00003  * Released under the MIT License: http://mbed.org/license/mit
00004  *
00005  * TODO: Needs serious rework/neatening up!
00006  */
00007 
00008 /*
00009  *  2010/05/14 modified for 20X4 LCD by ym1784
00010  */
00011 
00012 #include "TextLCD_20X4.h"
00013 
00014 #include "mbed.h"
00015 #include "error.h"
00016 
00017 using namespace mbed;
00018 
00019 /*
00020  * useful info found at http://www.a-netz.de/lcd.en.php
00021  *
00022  *
00023  * Initialization
00024  * ==============
00025  *
00026  * After attaching the supply voltage/after a reset, the display needs to be brought in to a defined state
00027  *
00028  * - wait approximately 15 ms so the display is ready to execute commands
00029  * - Execute the command 0x30 ("Display Settings") three times (wait 1,64ms after each command, the busy flag cannot be queried now).
00030  * - 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.
00031  * - If you want to use the 4 bit mode, now you can execute the command to switch over to this mode now.
00032  * - Execute the "clear display" command
00033  *
00034  * Timing
00035  * ======
00036  *
00037  * Nearly all commands transmitted to the display need 40us for execution.
00038  * Exceptions are the commands "Clear Display and Reset" and "Set Cursor to Start Position"
00039  * These commands need 1.64ms for execution. These timings are valid for all displays working with an
00040  * internal clock of 250kHz. But I do not know any displays that use other frequencies. Any time you
00041  * can use the busy flag to test if the display is ready to accept the next command.
00042  *
00043  * _e is kept high apart from calling clock
00044  * _rw is kept 0 (write) apart from actions that uyse it differently
00045  *  -> on this program, the _rw is not used ... ym1784
00046  * _rs is set by the data/command writes
00047  */
00048 
00049 TextLCD_20X4::TextLCD_20X4(PinName rs, PinName e, PinName d0, PinName d1,
00050     PinName d2, PinName d3, int columns, int rows) : _rs(rs), _e(e), _d(d0, d1, d2, d3),
00051     _columns(columns), _rows(rows) {
00052 
00053     _rows = 4;
00054     _columns = 20;
00055     // Mon, 27 Apr 2009 23:32:34 +0200
00056     // Kevin Konradt:
00057     // When using a LCD with 1 row x 16 characters
00058     // instead of 2x16, try changing _columns to 8.
00059     // (display seems to split the 16 characters into 
00060     // 2 virtual rows with 8 characters each.)
00061     //
00062     // 2010/05/14 ym1784
00063     // This program is only for 4 rows x 20 characters specific
00064 
00065 //  _rw = 0; // on this program, the _rw is not used ... ym1784
00066     _e  = 1;
00067     _rs = 0; // command mode
00068 
00069     // Should theoretically wait 15ms, but most things will be powered up pre-reset
00070     // so i'll disable that for the minute. If implemented, could wait 15ms post reset
00071     // instead
00072     wait(0.015); // for safety ... ym1784
00073         
00074     // send "Display Settings" 3 times (Only top nibble of 0x30 as we've got 4-bit bus)
00075     for(int i=0; i<3; i++) {
00076         writeNibble(0x3);
00077         wait(0.00164);  // this command takes 1.64ms, so wait for it
00078     }
00079     writeNibble(0x2);   // 4-bit mode
00080 
00081     writeCommand(0x28); // Function set 001 BW N F - -
00082     writeCommand(0x0C);
00083     writeCommand(0x6);  // Cursor Direction and Display Shift : 0000 01 CD S (CD 0-left, 1-right S(hift) 0-no, 1-yes
00084 
00085     cls();
00086 }
00087 
00088 int TextLCD_20X4::_putc(int value) {
00089     if(value == '\n') {
00090         newline();
00091     } else {
00092         writeData(value);
00093     }
00094     return value;
00095 }
00096 
00097 int TextLCD_20X4::_getc() {
00098     return 0;
00099 }
00100 
00101 void TextLCD_20X4::newline() {
00102     _column = 0;
00103     _row++;
00104     if(_row >= _rows) {
00105         _row = 0;
00106     }
00107     locate(_column, _row);
00108 }
00109 
00110 void TextLCD_20X4::locate(int column, int row) {
00111     if(column < 0 || column >= _columns || row < 0 || row >= _rows) {
00112         error("locate(%d,%d) out of range on %dx%d display", column, row, _columns, _rows);
00113         return;
00114     }
00115     _row = row;
00116     _column = column;
00117 // modified for 20X4 LCD
00118     switch (_row) {
00119         case (0) : address = 0x80 + _column;
00120         break;
00121         case (1) : address = 0xc0 + _column;
00122         break;
00123         case (2) : address = 0x94 + _column;
00124         break;
00125         case (3) : address = 0xd4 + _column;
00126         break;
00127         } // switch
00128     writeCommand(address);
00129 }
00130 
00131 void TextLCD_20X4::cls() {
00132     writeCommand(0x01); // Clear Display
00133     wait(0.00164f);     // This command takes 1.64 ms
00134 //    locate(0, 0);     // We don't have to do this here
00135 }
00136 
00137 void TextLCD_20X4::reset() {
00138     cls();
00139 }
00140 
00141 void TextLCD_20X4::clock() {
00142     wait(0.000040f);
00143     _e = 0;
00144     wait(0.000040f);  // most instructions take 40us
00145     _e = 1;
00146 }
00147 
00148 void TextLCD_20X4::writeNibble(int value) {
00149     _d = value;
00150     clock();
00151 }
00152 
00153 void TextLCD_20X4::writeByte(int value) {
00154     writeNibble(value >> 4);
00155     writeNibble(value >> 0);
00156 }
00157 
00158 void TextLCD_20X4::writeCommand(int command) {
00159     _rs = 0;
00160     writeByte(command);
00161 }
00162 
00163 void TextLCD_20X4::writeData(int data) {
00164     _rs = 1;
00165     writeByte(data);
00166     _column++;
00167     if(_column >= _columns) {
00168         newline();
00169     }
00170 }