Linija pogona

Dependencies:   mbed

Fork of RFID-RC522 by Project5_Software

Committer:
mario_meh
Date:
Wed Nov 22 08:55:19 2017 +0000
Revision:
4:e0bc23d5134a
Lilnija pogona

Who changed what in which revision?

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