Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TextLCD.cpp Source File

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