Dependencies:   mbed

Committer:
simon
Date:
Mon Nov 30 09:32:28 2009 +0000
Revision:
0:42626fc1bbc5

        

Who changed what in which revision?

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