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