To enable TextDisplays\' use as a lib I removed main.cpp

Dependents:   TextLCD_Serial

Committer:
giryan
Date:
Sun Sep 05 09:21:49 2010 +0000
Revision:
0:0e729fc7275a
Version of sford\s TextDisplays lib without main.cpp

Who changed what in which revision?

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