LCD driver

Dependents:   ecu_reader ecu_reader ecu_simulator regulator_napona

Committer:
pangsk
Date:
Fri Jul 27 15:06:59 2012 +0000
Revision:
0:ec079a141883
[mbed] converted /ecu_reader/TextLCD

Who changed what in which revision?

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