LCD I2C

Dependents:   proyectoRTOS proyectoRTOS2 proyectoRTOS proyectoRTOSp ... more

Committer:
simon
Date:
Sat May 22 18:18:19 2010 +0000
Revision:
0:edfb85c53631
Child:
1:ac48b187213c

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
simon 0:edfb85c53631 1 /* mbed TextLCD Library, 4-bit 2x16
simon 0:edfb85c53631 2 * Copyright (c) 2007-2010, sford
simon 0:edfb85c53631 3 *
simon 0:edfb85c53631 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
simon 0:edfb85c53631 5 * of this software and associated documentation files (the "Software"), to deal
simon 0:edfb85c53631 6 * in the Software without restriction, including without limitation the rights
simon 0:edfb85c53631 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
simon 0:edfb85c53631 8 * copies of the Software, and to permit persons to whom the Software is
simon 0:edfb85c53631 9 * furnished to do so, subject to the following conditions:
simon 0:edfb85c53631 10 *
simon 0:edfb85c53631 11 * The above copyright notice and this permission notice shall be included in
simon 0:edfb85c53631 12 * all copies or substantial portions of the Software.
simon 0:edfb85c53631 13 *
simon 0:edfb85c53631 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
simon 0:edfb85c53631 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
simon 0:edfb85c53631 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
simon 0:edfb85c53631 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
simon 0:edfb85c53631 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
simon 0:edfb85c53631 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
simon 0:edfb85c53631 20 * THE SOFTWARE.
simon 0:edfb85c53631 21 */
simon 0:edfb85c53631 22
simon 0:edfb85c53631 23 #include "TextLCD.h"
simon 0:edfb85c53631 24 #include "mbed.h"
simon 0:edfb85c53631 25
simon 0:edfb85c53631 26 /*
simon 0:edfb85c53631 27 * useful info found at http://www.a-netz.de/lcd.en.php
simon 0:edfb85c53631 28 *
simon 0:edfb85c53631 29 * Initialisation
simon 0:edfb85c53631 30 * ==============
simon 0:edfb85c53631 31 *
simon 0:edfb85c53631 32 * After attaching the supply voltage/after a reset, the display needs to be brought in to a defined state
simon 0:edfb85c53631 33 *
simon 0:edfb85c53631 34 * - wait approximately 15 ms so the display is ready to execute commands
simon 0:edfb85c53631 35 * - Execute the command 0x30 ("Display Settings") three times (wait 1,64ms after each command, the busy flag cannot be queried now).
simon 0:edfb85c53631 36 * - 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:edfb85c53631 37 * - If you want to use the 4 bit mode, now you can execute the command to switch over to this mode now.
simon 0:edfb85c53631 38 * - Execute the "clear display" command
simon 0:edfb85c53631 39 *
simon 0:edfb85c53631 40 * Timing
simon 0:edfb85c53631 41 * ======
simon 0:edfb85c53631 42 *
simon 0:edfb85c53631 43 * Nearly all commands transmitted to the display need 40us for execution.
simon 0:edfb85c53631 44 * Exceptions are the commands "Clear Display and Reset" and "Set Cursor to Start Position"
simon 0:edfb85c53631 45 * These commands need 1.64ms for execution. These timings are valid for all displays working with an
simon 0:edfb85c53631 46 * internal clock of 250kHz. But I do not know any displays that use other frequencies. Any time you
simon 0:edfb85c53631 47 * can use the busy flag to test if the display is ready to accept the next command.
simon 0:edfb85c53631 48 *
simon 0:edfb85c53631 49 * _e is kept high apart from calling clock
simon 0:edfb85c53631 50 * _rw is kept 0 (write) apart from actions that uyse it differently
simon 0:edfb85c53631 51 * _rs is set by the data/command writes
simon 0:edfb85c53631 52 */
simon 0:edfb85c53631 53
simon 0:edfb85c53631 54 TextLCD::TextLCD(PinName rs, PinName rw, PinName e, PinName d0, PinName d1,
simon 0:edfb85c53631 55 PinName d2, PinName d3) : _rw(rw), _rs(rs),
simon 0:edfb85c53631 56 _e(e), _d(d0, d1, d2, d3) {
simon 0:edfb85c53631 57
simon 0:edfb85c53631 58 _rw = 0;
simon 0:edfb85c53631 59 _e = 1;
simon 0:edfb85c53631 60 _rs = 0; // command mode
simon 0:edfb85c53631 61
simon 0:edfb85c53631 62 // Should theoretically wait 15ms, but most things will be powered up pre-reset
simon 0:edfb85c53631 63 // so i'll disable that for the minute. If implemented, could wait 15ms post reset
simon 0:edfb85c53631 64 // instead
simon 0:edfb85c53631 65 // wait(0.015);
simon 0:edfb85c53631 66
simon 0:edfb85c53631 67 // send "Display Settings" 3 times (Only top nibble of 0x30 as we've got 4-bit bus)
simon 0:edfb85c53631 68 for(int i=0; i<3; i++) {
simon 0:edfb85c53631 69 writeByte(0x3);
simon 0:edfb85c53631 70 wait(0.00164); // this command takes 1.64ms, so wait for it
simon 0:edfb85c53631 71 }
simon 0:edfb85c53631 72 writeByte(0x2); // 4-bit mode
simon 0:edfb85c53631 73
simon 0:edfb85c53631 74 writeCommand(0x28); // Function set 001 BW N F - -
simon 0:edfb85c53631 75 writeCommand(0x0C);
simon 0:edfb85c53631 76 writeCommand(0x6); // Cursor Direction and Display Shift : 0000 01 CD S (CD 0-left, 1-right S(hift) 0-no, 1-yes
simon 0:edfb85c53631 77 cls();
simon 0:edfb85c53631 78 }
simon 0:edfb85c53631 79
simon 0:edfb85c53631 80 void TextLCD::character(int column, int row, int c) {
simon 0:edfb85c53631 81 int address = 0x80 + (row * 40) + column; // memory starts at 0x80, and is 40 chars long per row
simon 0:edfb85c53631 82 writeCommand(address);
simon 0:edfb85c53631 83 writeData(c);
simon 0:edfb85c53631 84 }
simon 0:edfb85c53631 85
simon 0:edfb85c53631 86 int TextLCD::columns() {
simon 0:edfb85c53631 87 return 16;
simon 0:edfb85c53631 88 }
simon 0:edfb85c53631 89
simon 0:edfb85c53631 90 int TextLCD::rows() {
simon 0:edfb85c53631 91 return 2;
simon 0:edfb85c53631 92 }
simon 0:edfb85c53631 93
simon 0:edfb85c53631 94 void TextLCD::writeByte(int value) {
simon 0:edfb85c53631 95 _d = value >> 4;
simon 0:edfb85c53631 96 wait(0.000040f); // most instructions take 40us
simon 0:edfb85c53631 97 _e = 0;
simon 0:edfb85c53631 98 wait(0.000040f);
simon 0:edfb85c53631 99 _e = 1;
simon 0:edfb85c53631 100 _d = value >> 0;
simon 0:edfb85c53631 101 wait(0.000040f);
simon 0:edfb85c53631 102 _e = 0;
simon 0:edfb85c53631 103 wait(0.000040f); // most instructions take 40us
simon 0:edfb85c53631 104 _e = 1;
simon 0:edfb85c53631 105 }
simon 0:edfb85c53631 106
simon 0:edfb85c53631 107 void TextLCD::writeCommand(int command) {
simon 0:edfb85c53631 108 _rs = 0;
simon 0:edfb85c53631 109 writeByte(command);
simon 0:edfb85c53631 110 }
simon 0:edfb85c53631 111
simon 0:edfb85c53631 112 void TextLCD::writeData(int data) {
simon 0:edfb85c53631 113 _rs = 1;
simon 0:edfb85c53631 114 writeByte(data);
simon 0:edfb85c53631 115 }