proyecto prueba ros

Dependencies:   mbed Servo HC-SR04 max6675

Committer:
20172573063
Date:
Mon Jun 08 21:16:50 2020 +0000
Revision:
0:4cce815f7d81
prueba_rtos;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
20172573063 0:4cce815f7d81 1 /* mbed I2CTextLCD Library, for a 4-bit LCD driven by I2C and a PCF8574
20172573063 0:4cce815f7d81 2 * Copyright (c) 2007-2010, sford, rcocking
20172573063 0:4cce815f7d81 3 *
20172573063 0:4cce815f7d81 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
20172573063 0:4cce815f7d81 5 * of this software and associated documentation files (the "Software"), to deal
20172573063 0:4cce815f7d81 6 * in the Software without restriction, including without limitation the rights
20172573063 0:4cce815f7d81 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
20172573063 0:4cce815f7d81 8 * copies of the Software, and to permit persons to whom the Software is
20172573063 0:4cce815f7d81 9 * furnished to do so, subject to the following conditions:
20172573063 0:4cce815f7d81 10 *
20172573063 0:4cce815f7d81 11 * The above copyright notice and this permission notice shall be included in
20172573063 0:4cce815f7d81 12 * all copies or substantial portions of the Software.
20172573063 0:4cce815f7d81 13 *
20172573063 0:4cce815f7d81 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20172573063 0:4cce815f7d81 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20172573063 0:4cce815f7d81 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20172573063 0:4cce815f7d81 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20172573063 0:4cce815f7d81 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20172573063 0:4cce815f7d81 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20172573063 0:4cce815f7d81 20 * THE SOFTWARE.
20172573063 0:4cce815f7d81 21 */
20172573063 0:4cce815f7d81 22
20172573063 0:4cce815f7d81 23 /* This is a hack of Simon Ford's direct-driven TextLCD code.
20172573063 0:4cce815f7d81 24 * It should be refactored to extract a common superclass
20172573063 0:4cce815f7d81 25 * but my C++ isn't yet good enough :)
20172573063 0:4cce815f7d81 26 *
20172573063 0:4cce815f7d81 27 * The code assumes the following connections between the PCF8574
20172573063 0:4cce815f7d81 28 * and the LCD
20172573063 0:4cce815f7d81 29 *
20172573063 0:4cce815f7d81 30 * nc - D0
20172573063 0:4cce815f7d81 31 * nc - D1
20172573063 0:4cce815f7d81 32 * nc - D2
20172573063 0:4cce815f7d81 33 * nc - D3
20172573063 0:4cce815f7d81 34 * P0 - D4
20172573063 0:4cce815f7d81 35 * P1 - D5
20172573063 0:4cce815f7d81 36 * P2 - D6
20172573063 0:4cce815f7d81 37 * P3 - D7
20172573063 0:4cce815f7d81 38 * P4 - E
20172573063 0:4cce815f7d81 39 * P5 - nc
20172573063 0:4cce815f7d81 40 * P6 - nc
20172573063 0:4cce815f7d81 41 * P7 - RS
20172573063 0:4cce815f7d81 42 * gnd - R/W
20172573063 0:4cce815f7d81 43 *
20172573063 0:4cce815f7d81 44 * D0-3 of the LCD are not connected because we work in 4-bit mode
20172573063 0:4cce815f7d81 45 * R/W is hardwired to gound, as we only ever write to the LCD
20172573063 0:4cce815f7d81 46 * A0-2 on the PCF8574 can be set in any combination; you will need to modify
20172573063 0:4cce815f7d81 47 * the I2C address in the I2CTextLCD constructor.
20172573063 0:4cce815f7d81 48 * Remember that the mbed uses 8-bit addresses, which should be
20172573063 0:4cce815f7d81 49 * in the range 0x40-0x4E for the PCF8574
20172573063 0:4cce815f7d81 50 */
20172573063 0:4cce815f7d81 51 #include "TextLCD.h"
20172573063 0:4cce815f7d81 52 #include "mbed.h"
20172573063 0:4cce815f7d81 53
20172573063 0:4cce815f7d81 54 TextLCD::TextLCD(PinName sda, PinName scl, int i2cAddress, LCDType type) : _i2c(sda, scl),
20172573063 0:4cce815f7d81 55 _type(type) {
20172573063 0:4cce815f7d81 56 _i2cAddress = i2cAddress;
20172573063 0:4cce815f7d81 57 wait(0.015); // Wait 15ms to ensure powered up
20172573063 0:4cce815f7d81 58
20172573063 0:4cce815f7d81 59 // send "Display Settings" 3 times (Only top nibble of 0x30 as we've got 4-bit bus)
20172573063 0:4cce815f7d81 60 for (int i=0; i<3; i++) {
20172573063 0:4cce815f7d81 61 writeByte(0x3, false);
20172573063 0:4cce815f7d81 62 wait(0.00164); // this command takes 1.64ms, so wait for it
20172573063 0:4cce815f7d81 63 }
20172573063 0:4cce815f7d81 64 writeByte(0x2, false); // 4-bit mode
20172573063 0:4cce815f7d81 65 wait(0.000040f); // most instructions take 40us
20172573063 0:4cce815f7d81 66
20172573063 0:4cce815f7d81 67 writeCommand(0x28); // Function set 001 BW N F - -
20172573063 0:4cce815f7d81 68 writeCommand(0x0C);
20172573063 0:4cce815f7d81 69 writeCommand(0x6); // Cursor Direction and Display Shift : 0000 01 CD S (CD 0-left, 1-right S(hift) 0-no, 1-yes
20172573063 0:4cce815f7d81 70 cls();
20172573063 0:4cce815f7d81 71 }
20172573063 0:4cce815f7d81 72
20172573063 0:4cce815f7d81 73 void TextLCD::character(int column, int row, int c) {
20172573063 0:4cce815f7d81 74 int a = address(column, row);
20172573063 0:4cce815f7d81 75 writeCommand(a);
20172573063 0:4cce815f7d81 76 writeData(c);
20172573063 0:4cce815f7d81 77 }
20172573063 0:4cce815f7d81 78
20172573063 0:4cce815f7d81 79 void TextLCD::cls() {
20172573063 0:4cce815f7d81 80 writeCommand(0x01); // cls, and set cursor to 0
20172573063 0:4cce815f7d81 81 wait(0.00164f); // This command takes 1.64 ms
20172573063 0:4cce815f7d81 82 locate(0, 0);
20172573063 0:4cce815f7d81 83 }
20172573063 0:4cce815f7d81 84
20172573063 0:4cce815f7d81 85 void TextLCD::locate(int column, int row) {
20172573063 0:4cce815f7d81 86 _column = column;
20172573063 0:4cce815f7d81 87 _row = row;
20172573063 0:4cce815f7d81 88 }
20172573063 0:4cce815f7d81 89
20172573063 0:4cce815f7d81 90 int TextLCD::_putc(int value) {
20172573063 0:4cce815f7d81 91 if (value == '\n') {
20172573063 0:4cce815f7d81 92 _column = 0;
20172573063 0:4cce815f7d81 93 _row++;
20172573063 0:4cce815f7d81 94 if (_row >= rows()) {
20172573063 0:4cce815f7d81 95 _row = 0;
20172573063 0:4cce815f7d81 96 }
20172573063 0:4cce815f7d81 97 } else {
20172573063 0:4cce815f7d81 98 character(_column, _row, value);
20172573063 0:4cce815f7d81 99 _column++;
20172573063 0:4cce815f7d81 100 if (_column >= columns()) {
20172573063 0:4cce815f7d81 101 _column = 0;
20172573063 0:4cce815f7d81 102 _row++;
20172573063 0:4cce815f7d81 103 if (_row >= rows()) {
20172573063 0:4cce815f7d81 104 _row = 0;
20172573063 0:4cce815f7d81 105 }
20172573063 0:4cce815f7d81 106 }
20172573063 0:4cce815f7d81 107 }
20172573063 0:4cce815f7d81 108 return value;
20172573063 0:4cce815f7d81 109 }
20172573063 0:4cce815f7d81 110
20172573063 0:4cce815f7d81 111 int TextLCD::_getc() {
20172573063 0:4cce815f7d81 112 return -1;
20172573063 0:4cce815f7d81 113 }
20172573063 0:4cce815f7d81 114
20172573063 0:4cce815f7d81 115 void TextLCD::writeI2CByte(int data) {
20172573063 0:4cce815f7d81 116 // equivalent to writeI2CByte
20172573063 0:4cce815f7d81 117 char * cmd = new char[1];
20172573063 0:4cce815f7d81 118 cmd[0] = data;
20172573063 0:4cce815f7d81 119 _i2c.write(_i2cAddress, cmd, 1);
20172573063 0:4cce815f7d81 120 }
20172573063 0:4cce815f7d81 121
20172573063 0:4cce815f7d81 122 void TextLCD::writeNibble(int data, bool rs) {
20172573063 0:4cce815f7d81 123 data<<=4;
20172573063 0:4cce815f7d81 124 data|=8;
20172573063 0:4cce815f7d81 125 if (rs) {
20172573063 0:4cce815f7d81 126 data = data | RS_ON; // set rs bit
20172573063 0:4cce815f7d81 127 }
20172573063 0:4cce815f7d81 128 data |= E_ON; // E on
20172573063 0:4cce815f7d81 129 writeI2CByte(data);
20172573063 0:4cce815f7d81 130 data ^= E_ON; // E off
20172573063 0:4cce815f7d81 131 wait_us(1);
20172573063 0:4cce815f7d81 132 writeI2CByte(data);
20172573063 0:4cce815f7d81 133 wait_us(1000);
20172573063 0:4cce815f7d81 134 }
20172573063 0:4cce815f7d81 135
20172573063 0:4cce815f7d81 136 void TextLCD::writeByte(int data, bool rs) {
20172573063 0:4cce815f7d81 137 writeNibble(data >> 4, rs);
20172573063 0:4cce815f7d81 138 writeNibble(data & 0x0F, rs);
20172573063 0:4cce815f7d81 139 }
20172573063 0:4cce815f7d81 140
20172573063 0:4cce815f7d81 141 void TextLCD::writeCommand(int command) {
20172573063 0:4cce815f7d81 142 // equivalent to ard commandWrite
20172573063 0:4cce815f7d81 143 writeByte(command, false);
20172573063 0:4cce815f7d81 144 }
20172573063 0:4cce815f7d81 145
20172573063 0:4cce815f7d81 146 void TextLCD::writeData(int data) {
20172573063 0:4cce815f7d81 147
20172573063 0:4cce815f7d81 148 writeByte(data, true);
20172573063 0:4cce815f7d81 149 }
20172573063 0:4cce815f7d81 150
20172573063 0:4cce815f7d81 151 int TextLCD::address(int column, int row) {
20172573063 0:4cce815f7d81 152 switch (_type) {
20172573063 0:4cce815f7d81 153 case LCD20x4:
20172573063 0:4cce815f7d81 154 switch (row) {
20172573063 0:4cce815f7d81 155 case 0:
20172573063 0:4cce815f7d81 156 return 0x80 + column;
20172573063 0:4cce815f7d81 157 case 1:
20172573063 0:4cce815f7d81 158 return 0xc0 + column;
20172573063 0:4cce815f7d81 159 case 2:
20172573063 0:4cce815f7d81 160 return 0x94 + column;
20172573063 0:4cce815f7d81 161 case 3:
20172573063 0:4cce815f7d81 162 return 0xd4 + column;
20172573063 0:4cce815f7d81 163 }
20172573063 0:4cce815f7d81 164 case LCD16x2B:
20172573063 0:4cce815f7d81 165 return 0x80 + (row * 40) + column;
20172573063 0:4cce815f7d81 166 case LCD16x2:
20172573063 0:4cce815f7d81 167 case LCD20x2:
20172573063 0:4cce815f7d81 168 default:
20172573063 0:4cce815f7d81 169 return 0x80 + (row * 0x40) + column;
20172573063 0:4cce815f7d81 170 }
20172573063 0:4cce815f7d81 171 }
20172573063 0:4cce815f7d81 172
20172573063 0:4cce815f7d81 173 int TextLCD::columns() {
20172573063 0:4cce815f7d81 174 switch (_type) {
20172573063 0:4cce815f7d81 175 case LCD20x4:
20172573063 0:4cce815f7d81 176 case LCD20x2:
20172573063 0:4cce815f7d81 177 return 20;
20172573063 0:4cce815f7d81 178 case LCD16x2:
20172573063 0:4cce815f7d81 179 case LCD16x2B:
20172573063 0:4cce815f7d81 180 default:
20172573063 0:4cce815f7d81 181 return 16;
20172573063 0:4cce815f7d81 182 }
20172573063 0:4cce815f7d81 183 }
20172573063 0:4cce815f7d81 184
20172573063 0:4cce815f7d81 185 int TextLCD::rows() {
20172573063 0:4cce815f7d81 186 switch (_type) {
20172573063 0:4cce815f7d81 187 case LCD20x4:
20172573063 0:4cce815f7d81 188 return 4;
20172573063 0:4cce815f7d81 189 case LCD16x2:
20172573063 0:4cce815f7d81 190 case LCD16x2B:
20172573063 0:4cce815f7d81 191 case LCD20x2:
20172573063 0:4cce815f7d81 192 default:
20172573063 0:4cce815f7d81 193 return 2;
20172573063 0:4cce815f7d81 194 }
20172573063 0:4cce815f7d81 195 }