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