The start of a generic Text Display library to drive multiple types of text display in the same way

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  
00006 #include "TextLCD.h"
00007 #include "mbed.h"
00008 
00009 /*
00010  * useful info found at http://www.a-netz.de/lcd.en.php
00011  *
00012  * Initialisation
00013  * ==============
00014  *
00015  * After attaching the supply voltage/after a reset, the display needs to be brought in to a defined state
00016  *
00017  * - wait approximately 15 ms so the display is ready to execute commands
00018  * - Execute the command 0x30 ("Display Settings") three times (wait 1,64ms after each command, the busy flag cannot be queried now). 
00019  * - 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.
00020  * - If you want to use the 4 bit mode, now you can execute the command to switch over to this mode now.
00021  * - Execute the "clear display" command
00022  *
00023  * Timing
00024  * ======
00025  *
00026  * Nearly all commands transmitted to the display need 40us for execution. 
00027  * Exceptions are the commands "Clear Display and Reset" and "Set Cursor to Start Position" 
00028  * These commands need 1.64ms for execution. These timings are valid for all displays working with an 
00029  * internal clock of 250kHz. But I do not know any displays that use other frequencies. Any time you 
00030  * can use the busy flag to test if the display is ready to accept the next command.
00031  * 
00032  * _e is kept high apart from calling clock
00033  * _rw is kept 0 (write) apart from actions that uyse it differently
00034  * _rs is set by the data/command writes
00035  */
00036 
00037 TextLCD::TextLCD(PinName rs, PinName rw, PinName e, PinName d0, PinName d1, 
00038     PinName d2, PinName d3) : _rw(rw), _rs(rs), 
00039     _e(e), _d(d0, d1, d2, d3) {
00040 
00041     _rw = 0;
00042     _e  = 1;
00043     _rs = 0; // command mode
00044 
00045     // Should theoretically wait 15ms, but most things will be powered up pre-reset
00046     // so i'll disable that for the minute. If implemented, could wait 15ms post reset
00047     // instead
00048     // wait(0.015); 
00049         
00050     // send "Display Settings" 3 times (Only top nibble of 0x30 as we've got 4-bit bus)
00051     for(int i=0; i<3; i++) {
00052         writeByte(0x3);
00053         wait(0.00164);      // this command takes 1.64ms, so wait for it
00054     }
00055     writeByte(0x2); // 4-bit mode
00056             
00057     writeCommand(0x28);    // Function set 001 BW N F - -  
00058     writeCommand(0x0C);
00059     writeCommand(0x6);  //  Cursor Direction and Display Shift : 0000 01 CD S (CD 0-left, 1-right S(hift) 0-no, 1-yes    
00060     cls();
00061 }
00062 
00063 void TextLCD::character(int column, int row, int c) {
00064       int address = 0x80 + (row * 40) + column; // memory starts at 0x80, and is 40 chars long per row
00065       writeCommand(address);           
00066       writeData(c); 
00067 }
00068 
00069 int TextLCD::columns() {
00070     return 16;
00071 }
00072 
00073 int TextLCD::rows() { 
00074     return 2; 
00075 }
00076 
00077 void TextLCD::writeByte(int value) {
00078     _d = value >> 4;
00079     wait(0.000040f); // most instructions take 40us
00080     _e = 0;
00081     wait(0.000040f);  
00082     _e = 1;    
00083     _d = value >> 0;
00084     wait(0.000040f);
00085     _e = 0;
00086     wait(0.000040f);  // most instructions take 40us
00087     _e = 1;    
00088 }
00089 
00090 void TextLCD::writeCommand(int command) {
00091     _rs = 0;
00092     writeByte(command);
00093 }
00094 
00095 void TextLCD::writeData(int data) {
00096     _rs = 1;
00097     writeByte(data);
00098 }