fgfdgfg
Dependents: tarea_miercoles Tarea6_Teclado Tarea4_Encoder Tarea2_Teclado
Fork of TextLCD by
TextLCD.cpp@1:419979c1e228, 2013-10-16 (annotated)
- Committer:
- javiernegrette
- Date:
- Wed Oct 16 22:53:35 2013 +0000
- Revision:
- 1:419979c1e228
- Parent:
- 0:de90b67b2bca
fgfg
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pprasad7 | 0:de90b67b2bca | 1 | /* mbed TextLCD Library, for a 4-bit LCD based on HD44780 |
pprasad7 | 0:de90b67b2bca | 2 | * Copyright (c) 2007-2010, sford |
pprasad7 | 0:de90b67b2bca | 3 | * |
pprasad7 | 0:de90b67b2bca | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
pprasad7 | 0:de90b67b2bca | 5 | * of this software and associated documentation files (the "Software"), to deal |
pprasad7 | 0:de90b67b2bca | 6 | * in the Software without restriction, including without limitation the rights |
pprasad7 | 0:de90b67b2bca | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
pprasad7 | 0:de90b67b2bca | 8 | * copies of the Software, and to permit persons to whom the Software is |
pprasad7 | 0:de90b67b2bca | 9 | * furnished to do so, subject to the following conditions: |
pprasad7 | 0:de90b67b2bca | 10 | * |
pprasad7 | 0:de90b67b2bca | 11 | * The above copyright notice and this permission notice shall be included in |
pprasad7 | 0:de90b67b2bca | 12 | * all copies or substantial portions of the Software. |
pprasad7 | 0:de90b67b2bca | 13 | * |
pprasad7 | 0:de90b67b2bca | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
pprasad7 | 0:de90b67b2bca | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
pprasad7 | 0:de90b67b2bca | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
pprasad7 | 0:de90b67b2bca | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
pprasad7 | 0:de90b67b2bca | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
pprasad7 | 0:de90b67b2bca | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
pprasad7 | 0:de90b67b2bca | 20 | * THE SOFTWARE. |
pprasad7 | 0:de90b67b2bca | 21 | */ |
pprasad7 | 0:de90b67b2bca | 22 | |
pprasad7 | 0:de90b67b2bca | 23 | #include "TextLCD.h" |
pprasad7 | 0:de90b67b2bca | 24 | #include "mbed.h" |
pprasad7 | 0:de90b67b2bca | 25 | |
pprasad7 | 0:de90b67b2bca | 26 | TextLCD::TextLCD(PinName rs, PinName e, PinName d0, PinName d1, |
pprasad7 | 0:de90b67b2bca | 27 | PinName d2, PinName d3, LCDType type) : _rs(rs), |
pprasad7 | 0:de90b67b2bca | 28 | _e(e), _d(d0, d1, d2, d3), |
pprasad7 | 0:de90b67b2bca | 29 | _type(type) { |
pprasad7 | 0:de90b67b2bca | 30 | |
pprasad7 | 0:de90b67b2bca | 31 | _e = 1; |
pprasad7 | 0:de90b67b2bca | 32 | _rs = 0; // command mode |
pprasad7 | 0:de90b67b2bca | 33 | |
pprasad7 | 0:de90b67b2bca | 34 | wait(0.015); // Wait 15ms to ensure powered up |
pprasad7 | 0:de90b67b2bca | 35 | |
pprasad7 | 0:de90b67b2bca | 36 | // send "Display Settings" 3 times (Only top nibble of 0x30 as we've got 4-bit bus) |
pprasad7 | 0:de90b67b2bca | 37 | for (int i=0; i<3; i++) { |
pprasad7 | 0:de90b67b2bca | 38 | writeByte(0x3); |
pprasad7 | 0:de90b67b2bca | 39 | wait(0.00164); // this command takes 1.64ms, so wait for it |
pprasad7 | 0:de90b67b2bca | 40 | } |
pprasad7 | 0:de90b67b2bca | 41 | writeByte(0x2); // 4-bit mode |
pprasad7 | 0:de90b67b2bca | 42 | wait(0.000040f); // most instructions take 40us |
pprasad7 | 0:de90b67b2bca | 43 | |
pprasad7 | 0:de90b67b2bca | 44 | writeCommand(0x28); // Function set 001 BW N F - - |
pprasad7 | 0:de90b67b2bca | 45 | writeCommand(0x0C); |
pprasad7 | 0:de90b67b2bca | 46 | writeCommand(0x6); // Cursor Direction and Display Shift : 0000 01 CD S (CD 0-left, 1-right S(hift) 0-no, 1-yes |
pprasad7 | 0:de90b67b2bca | 47 | cls(); |
pprasad7 | 0:de90b67b2bca | 48 | } |
pprasad7 | 0:de90b67b2bca | 49 | |
pprasad7 | 0:de90b67b2bca | 50 | void TextLCD::character(int column, int row, int c) { |
pprasad7 | 0:de90b67b2bca | 51 | int a = address(column, row); |
pprasad7 | 0:de90b67b2bca | 52 | writeCommand(a); |
pprasad7 | 0:de90b67b2bca | 53 | writeData(c); |
pprasad7 | 0:de90b67b2bca | 54 | } |
pprasad7 | 0:de90b67b2bca | 55 | |
pprasad7 | 0:de90b67b2bca | 56 | void TextLCD::cls() { |
pprasad7 | 0:de90b67b2bca | 57 | writeCommand(0x01); // cls, and set cursor to 0 |
pprasad7 | 0:de90b67b2bca | 58 | wait(0.00164f); // This command takes 1.64 ms |
pprasad7 | 0:de90b67b2bca | 59 | locate(0, 0); |
pprasad7 | 0:de90b67b2bca | 60 | } |
pprasad7 | 0:de90b67b2bca | 61 | |
pprasad7 | 0:de90b67b2bca | 62 | void TextLCD::locate(int column, int row) { |
pprasad7 | 0:de90b67b2bca | 63 | _column = column; |
pprasad7 | 0:de90b67b2bca | 64 | _row = row; |
pprasad7 | 0:de90b67b2bca | 65 | } |
pprasad7 | 0:de90b67b2bca | 66 | |
pprasad7 | 0:de90b67b2bca | 67 | int TextLCD::_putc(int value) { |
pprasad7 | 0:de90b67b2bca | 68 | if (value == '\n') { |
pprasad7 | 0:de90b67b2bca | 69 | _column = 0; |
pprasad7 | 0:de90b67b2bca | 70 | _row++; |
pprasad7 | 0:de90b67b2bca | 71 | if (_row >= rows()) { |
pprasad7 | 0:de90b67b2bca | 72 | _row = 0; |
pprasad7 | 0:de90b67b2bca | 73 | } |
pprasad7 | 0:de90b67b2bca | 74 | } else { |
pprasad7 | 0:de90b67b2bca | 75 | character(_column, _row, value); |
pprasad7 | 0:de90b67b2bca | 76 | _column++; |
pprasad7 | 0:de90b67b2bca | 77 | if (_column >= columns()) { |
pprasad7 | 0:de90b67b2bca | 78 | _column = 0; |
pprasad7 | 0:de90b67b2bca | 79 | _row++; |
pprasad7 | 0:de90b67b2bca | 80 | if (_row >= rows()) { |
pprasad7 | 0:de90b67b2bca | 81 | _row = 0; |
pprasad7 | 0:de90b67b2bca | 82 | } |
pprasad7 | 0:de90b67b2bca | 83 | } |
pprasad7 | 0:de90b67b2bca | 84 | } |
pprasad7 | 0:de90b67b2bca | 85 | return value; |
pprasad7 | 0:de90b67b2bca | 86 | } |
pprasad7 | 0:de90b67b2bca | 87 | |
pprasad7 | 0:de90b67b2bca | 88 | int TextLCD::_getc() { |
pprasad7 | 0:de90b67b2bca | 89 | return -1; |
pprasad7 | 0:de90b67b2bca | 90 | } |
pprasad7 | 0:de90b67b2bca | 91 | |
pprasad7 | 0:de90b67b2bca | 92 | void TextLCD::writeByte(int value) { |
pprasad7 | 0:de90b67b2bca | 93 | _d = value >> 4; |
pprasad7 | 0:de90b67b2bca | 94 | wait(0.000040f); // most instructions take 40us |
pprasad7 | 0:de90b67b2bca | 95 | _e = 0; |
pprasad7 | 0:de90b67b2bca | 96 | wait(0.000040f); |
pprasad7 | 0:de90b67b2bca | 97 | _e = 1; |
pprasad7 | 0:de90b67b2bca | 98 | _d = value >> 0; |
pprasad7 | 0:de90b67b2bca | 99 | wait(0.000040f); |
pprasad7 | 0:de90b67b2bca | 100 | _e = 0; |
pprasad7 | 0:de90b67b2bca | 101 | wait(0.000040f); // most instructions take 40us |
pprasad7 | 0:de90b67b2bca | 102 | _e = 1; |
pprasad7 | 0:de90b67b2bca | 103 | } |
pprasad7 | 0:de90b67b2bca | 104 | |
pprasad7 | 0:de90b67b2bca | 105 | void TextLCD::writeCommand(int command) { |
pprasad7 | 0:de90b67b2bca | 106 | _rs = 0; |
pprasad7 | 0:de90b67b2bca | 107 | writeByte(command); |
pprasad7 | 0:de90b67b2bca | 108 | } |
pprasad7 | 0:de90b67b2bca | 109 | |
pprasad7 | 0:de90b67b2bca | 110 | void TextLCD::writeData(int data) { |
pprasad7 | 0:de90b67b2bca | 111 | _rs = 1; |
pprasad7 | 0:de90b67b2bca | 112 | writeByte(data); |
pprasad7 | 0:de90b67b2bca | 113 | } |
pprasad7 | 0:de90b67b2bca | 114 | |
pprasad7 | 0:de90b67b2bca | 115 | int TextLCD::address(int column, int row) { |
pprasad7 | 0:de90b67b2bca | 116 | switch (_type) { |
pprasad7 | 0:de90b67b2bca | 117 | case LCD20x4: |
pprasad7 | 0:de90b67b2bca | 118 | switch (row) { |
pprasad7 | 0:de90b67b2bca | 119 | case 0: |
pprasad7 | 0:de90b67b2bca | 120 | return 0x80 + column; |
pprasad7 | 0:de90b67b2bca | 121 | case 1: |
pprasad7 | 0:de90b67b2bca | 122 | return 0xc0 + column; |
pprasad7 | 0:de90b67b2bca | 123 | case 2: |
pprasad7 | 0:de90b67b2bca | 124 | return 0x94 + column; |
pprasad7 | 0:de90b67b2bca | 125 | case 3: |
pprasad7 | 0:de90b67b2bca | 126 | return 0xd4 + column; |
pprasad7 | 0:de90b67b2bca | 127 | } |
pprasad7 | 0:de90b67b2bca | 128 | case LCD16x2B: |
pprasad7 | 0:de90b67b2bca | 129 | return 0x80 + (row * 40) + column; |
pprasad7 | 0:de90b67b2bca | 130 | case LCD16x2: |
pprasad7 | 0:de90b67b2bca | 131 | case LCD20x2: |
pprasad7 | 0:de90b67b2bca | 132 | default: |
pprasad7 | 0:de90b67b2bca | 133 | return 0x80 + (row * 0x40) + column; |
pprasad7 | 0:de90b67b2bca | 134 | } |
pprasad7 | 0:de90b67b2bca | 135 | } |
pprasad7 | 0:de90b67b2bca | 136 | |
pprasad7 | 0:de90b67b2bca | 137 | int TextLCD::columns() { |
pprasad7 | 0:de90b67b2bca | 138 | switch (_type) { |
pprasad7 | 0:de90b67b2bca | 139 | case LCD20x4: |
pprasad7 | 0:de90b67b2bca | 140 | case LCD20x2: |
pprasad7 | 0:de90b67b2bca | 141 | return 20; |
pprasad7 | 0:de90b67b2bca | 142 | case LCD16x2: |
pprasad7 | 0:de90b67b2bca | 143 | case LCD16x2B: |
pprasad7 | 0:de90b67b2bca | 144 | default: |
pprasad7 | 0:de90b67b2bca | 145 | return 16; |
pprasad7 | 0:de90b67b2bca | 146 | } |
pprasad7 | 0:de90b67b2bca | 147 | } |
pprasad7 | 0:de90b67b2bca | 148 | |
pprasad7 | 0:de90b67b2bca | 149 | int TextLCD::rows() { |
pprasad7 | 0:de90b67b2bca | 150 | switch (_type) { |
pprasad7 | 0:de90b67b2bca | 151 | case LCD20x4: |
pprasad7 | 0:de90b67b2bca | 152 | return 4; |
pprasad7 | 0:de90b67b2bca | 153 | case LCD16x2: |
pprasad7 | 0:de90b67b2bca | 154 | case LCD16x2B: |
pprasad7 | 0:de90b67b2bca | 155 | case LCD20x2: |
pprasad7 | 0:de90b67b2bca | 156 | default: |
pprasad7 | 0:de90b67b2bca | 157 | return 2; |
pprasad7 | 0:de90b67b2bca | 158 | } |
pprasad7 | 0:de90b67b2bca | 159 | } |