Test LCD

Dependencies:   mbed

Fork of TextLCDTest by Simon Ford

Committer:
Tomoseec
Date:
Tue Dec 25 06:56:22 2012 +0000
Revision:
1:8ffd3f6b11b6
Parent:
0:b77078f30b97
test

Who changed what in which revision?

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