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

Dependencies:   mbed

Committer:
simon
Date:
Wed Nov 25 00:01:31 2009 +0000
Revision:
0:e8a5ca303ebd

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
simon 0:e8a5ca303ebd 1 /* mbed TextLCD Library
simon 0:e8a5ca303ebd 2 * Copyright (c) 2007-2009 sford
simon 0:e8a5ca303ebd 3 * Released under the MIT License: http://mbed.org/license/mit
simon 0:e8a5ca303ebd 4 */
simon 0:e8a5ca303ebd 5
simon 0:e8a5ca303ebd 6 #include "TextLCD.h"
simon 0:e8a5ca303ebd 7 #include "mbed.h"
simon 0:e8a5ca303ebd 8
simon 0:e8a5ca303ebd 9 /*
simon 0:e8a5ca303ebd 10 * useful info found at http://www.a-netz.de/lcd.en.php
simon 0:e8a5ca303ebd 11 *
simon 0:e8a5ca303ebd 12 * Initialisation
simon 0:e8a5ca303ebd 13 * ==============
simon 0:e8a5ca303ebd 14 *
simon 0:e8a5ca303ebd 15 * After attaching the supply voltage/after a reset, the display needs to be brought in to a defined state
simon 0:e8a5ca303ebd 16 *
simon 0:e8a5ca303ebd 17 * - wait approximately 15 ms so the display is ready to execute commands
simon 0:e8a5ca303ebd 18 * - Execute the command 0x30 ("Display Settings") three times (wait 1,64ms after each command, the busy flag cannot be queried now).
simon 0:e8a5ca303ebd 19 * - 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.
simon 0:e8a5ca303ebd 20 * - If you want to use the 4 bit mode, now you can execute the command to switch over to this mode now.
simon 0:e8a5ca303ebd 21 * - Execute the "clear display" command
simon 0:e8a5ca303ebd 22 *
simon 0:e8a5ca303ebd 23 * Timing
simon 0:e8a5ca303ebd 24 * ======
simon 0:e8a5ca303ebd 25 *
simon 0:e8a5ca303ebd 26 * Nearly all commands transmitted to the display need 40us for execution.
simon 0:e8a5ca303ebd 27 * Exceptions are the commands "Clear Display and Reset" and "Set Cursor to Start Position"
simon 0:e8a5ca303ebd 28 * These commands need 1.64ms for execution. These timings are valid for all displays working with an
simon 0:e8a5ca303ebd 29 * internal clock of 250kHz. But I do not know any displays that use other frequencies. Any time you
simon 0:e8a5ca303ebd 30 * can use the busy flag to test if the display is ready to accept the next command.
simon 0:e8a5ca303ebd 31 *
simon 0:e8a5ca303ebd 32 * _e is kept high apart from calling clock
simon 0:e8a5ca303ebd 33 * _rw is kept 0 (write) apart from actions that uyse it differently
simon 0:e8a5ca303ebd 34 * _rs is set by the data/command writes
simon 0:e8a5ca303ebd 35 */
simon 0:e8a5ca303ebd 36
simon 0:e8a5ca303ebd 37 TextLCD::TextLCD(PinName rs, PinName rw, PinName e, PinName d0, PinName d1,
simon 0:e8a5ca303ebd 38 PinName d2, PinName d3) : _rw(rw), _rs(rs),
simon 0:e8a5ca303ebd 39 _e(e), _d(d0, d1, d2, d3) {
simon 0:e8a5ca303ebd 40
simon 0:e8a5ca303ebd 41 _rw = 0;
simon 0:e8a5ca303ebd 42 _e = 1;
simon 0:e8a5ca303ebd 43 _rs = 0; // command mode
simon 0:e8a5ca303ebd 44
simon 0:e8a5ca303ebd 45 // Should theoretically wait 15ms, but most things will be powered up pre-reset
simon 0:e8a5ca303ebd 46 // so i'll disable that for the minute. If implemented, could wait 15ms post reset
simon 0:e8a5ca303ebd 47 // instead
simon 0:e8a5ca303ebd 48 // wait(0.015);
simon 0:e8a5ca303ebd 49
simon 0:e8a5ca303ebd 50 // send "Display Settings" 3 times (Only top nibble of 0x30 as we've got 4-bit bus)
simon 0:e8a5ca303ebd 51 for(int i=0; i<3; i++) {
simon 0:e8a5ca303ebd 52 writeByte(0x3);
simon 0:e8a5ca303ebd 53 wait(0.00164); // this command takes 1.64ms, so wait for it
simon 0:e8a5ca303ebd 54 }
simon 0:e8a5ca303ebd 55 writeByte(0x2); // 4-bit mode
simon 0:e8a5ca303ebd 56
simon 0:e8a5ca303ebd 57 writeCommand(0x28); // Function set 001 BW N F - -
simon 0:e8a5ca303ebd 58 writeCommand(0x0C);
simon 0:e8a5ca303ebd 59 writeCommand(0x6); // Cursor Direction and Display Shift : 0000 01 CD S (CD 0-left, 1-right S(hift) 0-no, 1-yes
simon 0:e8a5ca303ebd 60 cls();
simon 0:e8a5ca303ebd 61 }
simon 0:e8a5ca303ebd 62
simon 0:e8a5ca303ebd 63 void TextLCD::character(int column, int row, int c) {
simon 0:e8a5ca303ebd 64 int address = 0x80 + (row * 40) + column; // memory starts at 0x80, and is 40 chars long per row
simon 0:e8a5ca303ebd 65 writeCommand(address);
simon 0:e8a5ca303ebd 66 writeData(c);
simon 0:e8a5ca303ebd 67 }
simon 0:e8a5ca303ebd 68
simon 0:e8a5ca303ebd 69 int TextLCD::columns() {
simon 0:e8a5ca303ebd 70 return 16;
simon 0:e8a5ca303ebd 71 }
simon 0:e8a5ca303ebd 72
simon 0:e8a5ca303ebd 73 int TextLCD::rows() {
simon 0:e8a5ca303ebd 74 return 2;
simon 0:e8a5ca303ebd 75 }
simon 0:e8a5ca303ebd 76
simon 0:e8a5ca303ebd 77 void TextLCD::writeByte(int value) {
simon 0:e8a5ca303ebd 78 _d = value >> 4;
simon 0:e8a5ca303ebd 79 wait(0.000040f); // most instructions take 40us
simon 0:e8a5ca303ebd 80 _e = 0;
simon 0:e8a5ca303ebd 81 wait(0.000040f);
simon 0:e8a5ca303ebd 82 _e = 1;
simon 0:e8a5ca303ebd 83 _d = value >> 0;
simon 0:e8a5ca303ebd 84 wait(0.000040f);
simon 0:e8a5ca303ebd 85 _e = 0;
simon 0:e8a5ca303ebd 86 wait(0.000040f); // most instructions take 40us
simon 0:e8a5ca303ebd 87 _e = 1;
simon 0:e8a5ca303ebd 88 }
simon 0:e8a5ca303ebd 89
simon 0:e8a5ca303ebd 90 void TextLCD::writeCommand(int command) {
simon 0:e8a5ca303ebd 91 _rs = 0;
simon 0:e8a5ca303ebd 92 writeByte(command);
simon 0:e8a5ca303ebd 93 }
simon 0:e8a5ca303ebd 94
simon 0:e8a5ca303ebd 95 void TextLCD::writeData(int data) {
simon 0:e8a5ca303ebd 96 _rs = 1;
simon 0:e8a5ca303ebd 97 writeByte(data);
simon 0:e8a5ca303ebd 98 }