Simon Ford / Mbed 2 deprecated displays

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