This is a modified version of TextLCD on the Cookbook for 20X4 LCD specific. This does not control RW line, so we can reduce the number of pin usage on the mbed.

Dependencies:   mbed

Committer:
ym1784
Date:
Fri May 14 21:19:39 2010 +0000
Revision:
0:f84245f91a5a

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ym1784 0:f84245f91a5a 1 /* mbed TextLCD Library
ym1784 0:f84245f91a5a 2 * Copyright (c) 2007-2009 sford
ym1784 0:f84245f91a5a 3 * Released under the MIT License: http://mbed.org/license/mit
ym1784 0:f84245f91a5a 4 *
ym1784 0:f84245f91a5a 5 * TODO: Needs serious rework/neatening up!
ym1784 0:f84245f91a5a 6 */
ym1784 0:f84245f91a5a 7
ym1784 0:f84245f91a5a 8 /*
ym1784 0:f84245f91a5a 9 * 2010/05/14 modified for 20X4 LCD by ym1784
ym1784 0:f84245f91a5a 10 */
ym1784 0:f84245f91a5a 11
ym1784 0:f84245f91a5a 12 #include "TextLCD_20X4.h"
ym1784 0:f84245f91a5a 13
ym1784 0:f84245f91a5a 14 #include "mbed.h"
ym1784 0:f84245f91a5a 15 #include "error.h"
ym1784 0:f84245f91a5a 16
ym1784 0:f84245f91a5a 17 using namespace mbed;
ym1784 0:f84245f91a5a 18
ym1784 0:f84245f91a5a 19 /*
ym1784 0:f84245f91a5a 20 * useful info found at http://www.a-netz.de/lcd.en.php
ym1784 0:f84245f91a5a 21 *
ym1784 0:f84245f91a5a 22 *
ym1784 0:f84245f91a5a 23 * Initialization
ym1784 0:f84245f91a5a 24 * ==============
ym1784 0:f84245f91a5a 25 *
ym1784 0:f84245f91a5a 26 * After attaching the supply voltage/after a reset, the display needs to be brought in to a defined state
ym1784 0:f84245f91a5a 27 *
ym1784 0:f84245f91a5a 28 * - wait approximately 15 ms so the display is ready to execute commands
ym1784 0:f84245f91a5a 29 * - Execute the command 0x30 ("Display Settings") three times (wait 1,64ms after each command, the busy flag cannot be queried now).
ym1784 0:f84245f91a5a 30 * - 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.
ym1784 0:f84245f91a5a 31 * - If you want to use the 4 bit mode, now you can execute the command to switch over to this mode now.
ym1784 0:f84245f91a5a 32 * - Execute the "clear display" command
ym1784 0:f84245f91a5a 33 *
ym1784 0:f84245f91a5a 34 * Timing
ym1784 0:f84245f91a5a 35 * ======
ym1784 0:f84245f91a5a 36 *
ym1784 0:f84245f91a5a 37 * Nearly all commands transmitted to the display need 40us for execution.
ym1784 0:f84245f91a5a 38 * Exceptions are the commands "Clear Display and Reset" and "Set Cursor to Start Position"
ym1784 0:f84245f91a5a 39 * These commands need 1.64ms for execution. These timings are valid for all displays working with an
ym1784 0:f84245f91a5a 40 * internal clock of 250kHz. But I do not know any displays that use other frequencies. Any time you
ym1784 0:f84245f91a5a 41 * can use the busy flag to test if the display is ready to accept the next command.
ym1784 0:f84245f91a5a 42 *
ym1784 0:f84245f91a5a 43 * _e is kept high apart from calling clock
ym1784 0:f84245f91a5a 44 * _rw is kept 0 (write) apart from actions that uyse it differently
ym1784 0:f84245f91a5a 45 * -> on this program, the _rw is not used ... ym1784
ym1784 0:f84245f91a5a 46 * _rs is set by the data/command writes
ym1784 0:f84245f91a5a 47 */
ym1784 0:f84245f91a5a 48
ym1784 0:f84245f91a5a 49 TextLCD_20X4::TextLCD_20X4(PinName rs, PinName e, PinName d0, PinName d1,
ym1784 0:f84245f91a5a 50 PinName d2, PinName d3, int columns, int rows) : _rs(rs), _e(e), _d(d0, d1, d2, d3),
ym1784 0:f84245f91a5a 51 _columns(columns), _rows(rows) {
ym1784 0:f84245f91a5a 52
ym1784 0:f84245f91a5a 53 _rows = 4;
ym1784 0:f84245f91a5a 54 _columns = 20;
ym1784 0:f84245f91a5a 55 // Mon, 27 Apr 2009 23:32:34 +0200
ym1784 0:f84245f91a5a 56 // Kevin Konradt:
ym1784 0:f84245f91a5a 57 // When using a LCD with 1 row x 16 characters
ym1784 0:f84245f91a5a 58 // instead of 2x16, try changing _columns to 8.
ym1784 0:f84245f91a5a 59 // (display seems to split the 16 characters into
ym1784 0:f84245f91a5a 60 // 2 virtual rows with 8 characters each.)
ym1784 0:f84245f91a5a 61 //
ym1784 0:f84245f91a5a 62 // 2010/05/14 ym1784
ym1784 0:f84245f91a5a 63 // This program is only for 4 rows x 20 characters specific
ym1784 0:f84245f91a5a 64
ym1784 0:f84245f91a5a 65 // _rw = 0; // on this program, the _rw is not used ... ym1784
ym1784 0:f84245f91a5a 66 _e = 1;
ym1784 0:f84245f91a5a 67 _rs = 0; // command mode
ym1784 0:f84245f91a5a 68
ym1784 0:f84245f91a5a 69 // Should theoretically wait 15ms, but most things will be powered up pre-reset
ym1784 0:f84245f91a5a 70 // so i'll disable that for the minute. If implemented, could wait 15ms post reset
ym1784 0:f84245f91a5a 71 // instead
ym1784 0:f84245f91a5a 72 wait(0.015); // for safety ... ym1784
ym1784 0:f84245f91a5a 73
ym1784 0:f84245f91a5a 74 // send "Display Settings" 3 times (Only top nibble of 0x30 as we've got 4-bit bus)
ym1784 0:f84245f91a5a 75 for(int i=0; i<3; i++) {
ym1784 0:f84245f91a5a 76 writeNibble(0x3);
ym1784 0:f84245f91a5a 77 wait(0.00164); // this command takes 1.64ms, so wait for it
ym1784 0:f84245f91a5a 78 }
ym1784 0:f84245f91a5a 79 writeNibble(0x2); // 4-bit mode
ym1784 0:f84245f91a5a 80
ym1784 0:f84245f91a5a 81 writeCommand(0x28); // Function set 001 BW N F - -
ym1784 0:f84245f91a5a 82 writeCommand(0x0C);
ym1784 0:f84245f91a5a 83 writeCommand(0x6); // Cursor Direction and Display Shift : 0000 01 CD S (CD 0-left, 1-right S(hift) 0-no, 1-yes
ym1784 0:f84245f91a5a 84
ym1784 0:f84245f91a5a 85 cls();
ym1784 0:f84245f91a5a 86 }
ym1784 0:f84245f91a5a 87
ym1784 0:f84245f91a5a 88 int TextLCD_20X4::_putc(int value) {
ym1784 0:f84245f91a5a 89 if(value == '\n') {
ym1784 0:f84245f91a5a 90 newline();
ym1784 0:f84245f91a5a 91 } else {
ym1784 0:f84245f91a5a 92 writeData(value);
ym1784 0:f84245f91a5a 93 }
ym1784 0:f84245f91a5a 94 return value;
ym1784 0:f84245f91a5a 95 }
ym1784 0:f84245f91a5a 96
ym1784 0:f84245f91a5a 97 int TextLCD_20X4::_getc() {
ym1784 0:f84245f91a5a 98 return 0;
ym1784 0:f84245f91a5a 99 }
ym1784 0:f84245f91a5a 100
ym1784 0:f84245f91a5a 101 void TextLCD_20X4::newline() {
ym1784 0:f84245f91a5a 102 _column = 0;
ym1784 0:f84245f91a5a 103 _row++;
ym1784 0:f84245f91a5a 104 if(_row >= _rows) {
ym1784 0:f84245f91a5a 105 _row = 0;
ym1784 0:f84245f91a5a 106 }
ym1784 0:f84245f91a5a 107 locate(_column, _row);
ym1784 0:f84245f91a5a 108 }
ym1784 0:f84245f91a5a 109
ym1784 0:f84245f91a5a 110 void TextLCD_20X4::locate(int column, int row) {
ym1784 0:f84245f91a5a 111 if(column < 0 || column >= _columns || row < 0 || row >= _rows) {
ym1784 0:f84245f91a5a 112 error("locate(%d,%d) out of range on %dx%d display", column, row, _columns, _rows);
ym1784 0:f84245f91a5a 113 return;
ym1784 0:f84245f91a5a 114 }
ym1784 0:f84245f91a5a 115 _row = row;
ym1784 0:f84245f91a5a 116 _column = column;
ym1784 0:f84245f91a5a 117 // modified for 20X4 LCD
ym1784 0:f84245f91a5a 118 switch (_row) {
ym1784 0:f84245f91a5a 119 case (0) : address = 0x80 + _column;
ym1784 0:f84245f91a5a 120 break;
ym1784 0:f84245f91a5a 121 case (1) : address = 0xc0 + _column;
ym1784 0:f84245f91a5a 122 break;
ym1784 0:f84245f91a5a 123 case (2) : address = 0x94 + _column;
ym1784 0:f84245f91a5a 124 break;
ym1784 0:f84245f91a5a 125 case (3) : address = 0xd4 + _column;
ym1784 0:f84245f91a5a 126 break;
ym1784 0:f84245f91a5a 127 } // switch
ym1784 0:f84245f91a5a 128 writeCommand(address);
ym1784 0:f84245f91a5a 129 }
ym1784 0:f84245f91a5a 130
ym1784 0:f84245f91a5a 131 void TextLCD_20X4::cls() {
ym1784 0:f84245f91a5a 132 writeCommand(0x01); // Clear Display
ym1784 0:f84245f91a5a 133 wait(0.00164f); // This command takes 1.64 ms
ym1784 0:f84245f91a5a 134 // locate(0, 0); // We don't have to do this here
ym1784 0:f84245f91a5a 135 }
ym1784 0:f84245f91a5a 136
ym1784 0:f84245f91a5a 137 void TextLCD_20X4::reset() {
ym1784 0:f84245f91a5a 138 cls();
ym1784 0:f84245f91a5a 139 }
ym1784 0:f84245f91a5a 140
ym1784 0:f84245f91a5a 141 void TextLCD_20X4::clock() {
ym1784 0:f84245f91a5a 142 wait(0.000040f);
ym1784 0:f84245f91a5a 143 _e = 0;
ym1784 0:f84245f91a5a 144 wait(0.000040f); // most instructions take 40us
ym1784 0:f84245f91a5a 145 _e = 1;
ym1784 0:f84245f91a5a 146 }
ym1784 0:f84245f91a5a 147
ym1784 0:f84245f91a5a 148 void TextLCD_20X4::writeNibble(int value) {
ym1784 0:f84245f91a5a 149 _d = value;
ym1784 0:f84245f91a5a 150 clock();
ym1784 0:f84245f91a5a 151 }
ym1784 0:f84245f91a5a 152
ym1784 0:f84245f91a5a 153 void TextLCD_20X4::writeByte(int value) {
ym1784 0:f84245f91a5a 154 writeNibble(value >> 4);
ym1784 0:f84245f91a5a 155 writeNibble(value >> 0);
ym1784 0:f84245f91a5a 156 }
ym1784 0:f84245f91a5a 157
ym1784 0:f84245f91a5a 158 void TextLCD_20X4::writeCommand(int command) {
ym1784 0:f84245f91a5a 159 _rs = 0;
ym1784 0:f84245f91a5a 160 writeByte(command);
ym1784 0:f84245f91a5a 161 }
ym1784 0:f84245f91a5a 162
ym1784 0:f84245f91a5a 163 void TextLCD_20X4::writeData(int data) {
ym1784 0:f84245f91a5a 164 _rs = 1;
ym1784 0:f84245f91a5a 165 writeByte(data);
ym1784 0:f84245f91a5a 166 _column++;
ym1784 0:f84245f91a5a 167 if(_column >= _columns) {
ym1784 0:f84245f91a5a 168 newline();
ym1784 0:f84245f91a5a 169 }
ym1784 0:f84245f91a5a 170 }