teclado lcd
Dependencies: TextLCD keypad mbed
Revision 0:9a67044d749e, committed 2017-02-25
- Comitter:
- serbm
- Date:
- Sat Feb 25 14:21:11 2017 +0000
- Commit message:
- teclado con lcd
Changed in this revision
diff -r 000000000000 -r 9a67044d749e TextLCD.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TextLCD.cpp Sat Feb 25 14:21:11 2017 +0000 @@ -0,0 +1,173 @@ +/* mbed TextLCD Library, for a 4-bit LCD based on HD44780 + * Copyright (c) 2007-2010, sford, http://mbed.org + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +/* + * Comment: Added support for LCD40x2 and LCD8x2 + * Name: davervw + * Date: 02/26/2011 + */ + +#include "TextLCD.h" +#include "mbed.h" + +TextLCD::TextLCD(PinName rs, PinName e, PinName d4, PinName d5, + PinName d6, PinName d7, LCDType type) : _rs(rs), + _e(e), _d(d4, d5, d6, d7), + _type(type) { + + _e = 1; + _rs = 0; // command mode + + wait(0.015); // Wait 15ms to ensure powered up + + // send "Display Settings" 3 times (Only top nibble of 0x30 as we've got 4-bit bus) + for (int i=0; i<3; i++) { + writeByte(0x3); + wait(0.00164); // this command takes 1.64ms, so wait for it + } + writeByte(0x2); // 4-bit mode + wait(0.000040f); // most instructions take 40us + + writeCommand(0x28); // Function set 001 BW N F - - + writeCommand(0x0C); + writeCommand(0x6); // Cursor Direction and Display Shift : 0000 01 CD S (CD 0-left, 1-right S(hift) 0-no, 1-yes + cls(); +} + +void TextLCD::character(int column, int row, int c) { + int a = address(column, row); + writeCommand(a); + writeData(c); +} + +void TextLCD::cls() { + writeCommand(0x01); // cls, and set cursor to 0 + wait(0.00164f); // This command takes 1.64 ms + locate(0, 0); +} + +void TextLCD::locate(int column, int row) { + _column = column; + _row = row; +} + +int TextLCD::_putc(int value) { + if (value == '\n') { + _column = 0; + _row++; + if (_row >= rows()) { + _row = 0; + } + } else { + character(_column, _row, value); + _column++; + if (_column >= columns()) { + _column = 0; + _row++; + if (_row >= rows()) { + _row = 0; + } + } + } + return value; +} + +int TextLCD::_getc() { + return -1; +} + +void TextLCD::writeByte(int value) { + _d = value >> 4; + wait(0.000040f); // most instructions take 40us + _e = 0; + wait(0.000040f); + _e = 1; + _d = value >> 0; + wait(0.000040f); + _e = 0; + wait(0.000040f); // most instructions take 40us + _e = 1; +} + +void TextLCD::writeCommand(int command) { + _rs = 0; + writeByte(command); +} + +void TextLCD::writeData(int data) { + _rs = 1; + writeByte(data); +} + +int TextLCD::address(int column, int row) { + switch (_type) { + case LCD20x4: + switch (row) { + case 0: + return 0x80 + column; + case 1: + return 0xc0 + column; + case 2: + return 0x94 + column; + case 3: + return 0xd4 + column; + } + case LCD16x2B: + return 0x80 + (row * 40) + column; + case LCD16x2: + case LCD40x2: + case LCD20x2: + case LCD8x2: + default: + return 0x80 + (row * 0x40) + column; + } +} + +int TextLCD::columns() { + switch (_type) { + case LCD20x4: + case LCD20x2: + return 20; + case LCD40x2: + return 40; + case LCD8x2: + return 8; + case LCD16x2: + case LCD16x2B: + default: + return 16; + } +} + +int TextLCD::rows() { + switch (_type) { + case LCD20x4: + return 4; + case LCD8x2: + case LCD16x2: + case LCD16x2B: + case LCD20x2: + case LCD40x2: + default: + return 2; + } +}
diff -r 000000000000 -r 9a67044d749e TextLCD.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TextLCD.h Sat Feb 25 14:21:11 2017 +0000 @@ -0,0 +1,119 @@ +/* mbed TextLCD Library, for a 4-bit LCD based on HD44780 + * Copyright (c) 2007-2010, sford, http://mbed.org + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +/* + * Comment: Added support for LCD40x2 and LCD8x2 + * Name: davervw + * Date: 02/26/2011 + */ + +#ifndef MBED_TEXTLCD_H +#define MBED_TEXTLCD_H + +#include "mbed.h" + +/** A TextLCD interface for driving 4-bit HD44780-based LCDs + * + * Currently supports 16x2, 20x2, 20x4, 40x2, and 8x2 panels + * + * @code + * #include "mbed.h" + * #include "TextLCD.h" + * + * TextLCD lcd(p10, p12, p15, p16, p29, p30); // rs, e, d4-d7 + * + * int main() { + * lcd.printf("Hello World!\n"); + * } + * @endcode + */ +class TextLCD : public Stream { +public: + + /** LCD panel format */ + enum LCDType { + LCD16x2 /**< 16x2 LCD panel (default) */ + , LCD16x2B /**< 16x2 LCD panel alternate addressing */ + , LCD20x2 /**< 20x2 LCD panel */ + , LCD20x4 /**< 20x4 LCD panel */ + , LCD40x2 /**< 40x2 LCD panel */ + , LCD8x2 /**< 8x2 LCD panel */ + }; + + /** Create a TextLCD interface + * + * @param rs Instruction/data control line + * @param e Enable line (clock) + * @param d4-d7 Data lines for using as a 4-bit interface + * @param type Sets the panel size/addressing mode (default = LCD16x2) + */ + TextLCD(PinName rs, PinName e, PinName d4, PinName d5, PinName d6, PinName d7, LCDType type = LCD16x2); + +#if DOXYGEN_ONLY + /** Write a character to the LCD + * + * @param c The character to write to the display + */ + int putc(int c); + + /** Write a formated string to the LCD + * + * @param format A printf-style format string, followed by the + * variables to use in formating the string. + */ + int printf(const char* format, ...); +#endif + + /** Locate to a screen column and row + * + * @param column The horizontal position from the left, indexed from 0 + * @param row The vertical position from the top, indexed from 0 + */ + void locate(int column, int row); + + /** Clear the screen and locate to 0,0 */ + void cls(); + + int rows(); + int columns(); + +protected: + + // Stream implementation functions + virtual int _putc(int value); + virtual int _getc(); + + int address(int column, int row); + void character(int column, int row, int c); + void writeByte(int value); + void writeCommand(int command); + void writeData(int data); + + DigitalOut _rs, _e; + BusOut _d; + LCDType _type; + + int _column; + int _row; +}; + +#endif
diff -r 000000000000 -r 9a67044d749e TextLCD.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TextLCD.lib Sat Feb 25 14:21:11 2017 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/davervw/code/TextLCD/#c5318c74f1a9
diff -r 000000000000 -r 9a67044d749e keypad.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/keypad.cpp Sat Feb 25 14:21:11 2017 +0000 @@ -0,0 +1,115 @@ +#include "keypad.h" + +Keypad::Keypad(PinName row3, PinName row2, PinName row1, PinName row0, + PinName col3, PinName col2, PinName col1, PinName col0, + int debounce_ms): + _row0(row0), _row1(row1), _row2(row2), _row3(row3), + _cols(col0, col1, col2, col3) +{ + _debounce = debounce_ms; + _rows[0] = &_row0; + _rows[1] = &_row1; + _rows[2] = &_row2; + _rows[3] = &_row3; + for (int r = 0; r < row_count; r++) + _rows[r]->mode(PullUp); + // _cols.mode(OpenDrain); + _cols.output(); +} + +void Keypad::_setupFallTrigger(void) +{ + for (int r = 0; r < row_count; r++) + _rows[r]->fall(this, &Keypad::_callback); +} + +void Keypad::Start(void) +{ + /* make the columns zero so they can pull rows down */ + _cols = 0x00; +} + +void Keypad::Stop(void) +{ + /* make the columns one so they cannot pull any rows down anymore */ + _cols = ~0x00; +} + +void Keypad::CallAfterInput(uint32_t (*fptr)(uint32_t index)) +{ + _input.attach(fptr); + _setupFallTrigger(); +} + +int Keypad::DebouncedScan() +{ + /* debounce */ + wait_ms(_debounce); + + return Scan(); +} + +int Keypad::Scan() +{ + /* lookup row */ + int r = -1; + for (r = 0; r < row_count; r++) { + if (*_rows[r] == 0) + break; + } + + /* if we didn't find a valid row, return */ + if (!(0 <= r && r < row_count)) + return -1; + + /* scan columns to find out which one pulls down the row */ + int c = -1; + for (c = 0; c < col_count; c++) { + _cols = ~(1 << c); + if (*_rows[r] == 0) + break; + } + + /* re-energize all columns */ + Start(); + + /* if we didn't find a valid column, return */ + if (!(0 <= c && c < col_count)) + return -1; + + return r * col_count + c; +} + +int Keypad::DebouncedScanMultiple() +{ + /* debounce */ + wait_ms(_debounce); + + return ScanMultiple(); +} + +int Keypad::ScanMultiple() +{ + int res = 0; + for (int c = 0; c < col_count; c++) { + _cols = ~(1 << c); + for (int r = 0; r < row_count; r++) { + if (*_rows[r] == 0) { + res |= 1 << (r * col_count + c); + } + } + } + + return res; +} + +void Keypad::_callback() +{ + /* lookup */ + int position = DebouncedScan(); + + /* call back a valid position */ + if (position >= 0) + _input.call(position); +} +
diff -r 000000000000 -r 9a67044d749e keypad.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/keypad.h Sat Feb 25 14:21:11 2017 +0000 @@ -0,0 +1,127 @@ +/* mbed Keypad library, using user-defined interrupt callback + * Copyright (c) 2012 Yoong Hor Meng + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE + */ + +#ifndef KEYPAD_H +#define KEYPAD_H + +#include "mbed.h" +#include "FPointer.h" + +/** + * An interrupt-based interface to 4x4 keypad. + * + * On each key pressed on a keypad, the index of the key is passed to a + * user-defined function. User is free to define what to be done with the + * input. + * + * This library makes use of + * @see http://mbed.org/cookbook/FPointer by Andy Kirkham + * + * Example: + * @code + * #include "mbed.h" + * #include "keypad.h" + * + * // Define your own keypad values + * char Keytable[] = { '1', '2', '3', 'A', + * '4', '5', '6', 'B', + * '7', '8', '9', 'C', + * '*', '0', '#', 'D' + * }; + * + * uint32_t cbAfterInput(uint32_t index) { + * printf("Index:%d => Key:%c\n", key, Keytable[index]); + * return 0; + * } + * + * int main() { + * Keypad keypad(p25, p26, p27, p28, p21, p22, p23, p24); + * keypad.CallAfterInput(&cbAfterInput); + * keypad.Start(); + * + * while (1) { + * wait_ms(100); + * } + * } + * @endcode + */ +class Keypad +{ +public: + /** Create a Keypad interface + * + * @param row<3..0> Row data lines + * @param col<3..0> Column data lines + * @param debounce_ms Debounce in ms (Default to 20ms) + */ + Keypad(PinName row3, PinName row2, PinName row1, PinName row0, + PinName col3, PinName col2, PinName col1, PinName col0, + int debounce_ms = 20); + + /** Start the keypad interrupt routines + */ + void Start(void); + + /** Stop the keypad interrupt routines + */ + void Stop(void); + + /** Scan the keyboard for a debounced pressed key + */ + int DebouncedScan(void); + + /** Scan the keyboard for a pressed key + */ + int Scan(void); + + /** Scan the keyboard for multiple debounced pressed keys + */ + int DebouncedScanMultiple(void); + + /** Scan the keyboard for multiple pressed keys + */ + int ScanMultiple(void); + + /** User-defined function that to be called when a key is pressed + * @param fptr A function pointer takes a uint32_t and + * returns uint32_t + */ + void CallAfterInput(uint32_t (*fptr)(uint32_t)); + +protected: + static const int row_count = 4; + static const int col_count = 4; + + InterruptIn _row0; + InterruptIn _row1; + InterruptIn _row2; + InterruptIn _row3; + InterruptIn *_rows[row_count]; + BusInOut _cols; // BusOut doesn't support mode() yet; need open drain to prevent short circuits... + int _debounce; + FPointer _input; // Called after each input + + void _callback(); + void _setupFallTrigger(void); +}; + +#endif // KEYPAD_H
diff -r 000000000000 -r 9a67044d749e keypad.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/keypad.lib Sat Feb 25 14:21:11 2017 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/jyrodriguezg/code/keypad/#0012aa4908fa
diff -r 000000000000 -r 9a67044d749e main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Sat Feb 25 14:21:11 2017 +0000 @@ -0,0 +1,298 @@ +# include "mbed.h" +# include "keypad.h" +# include "TextLCD.h" + + + TextLCD lcd (D15,D14,D13,D12,D11,D10); // RS, e, d4-d7 + + AnalogIn Ain (A0); + AnalogOut Aout (A2); + +int C4 = 0x0C ; +int C1 = 0x0E ; +int p = 0 ; + +int c = 0 ; +int y = 0 ; +float i; // establecer el punto +float j = 0 ; // salida planta +float ji = 0 ; // variable de Proceso interno +float spi = 0 ; // variable de Proceso interno +float h = 0 ; // entrada planta +float m = 0 ; // Error +float mv = 0 ; +float g = 0 ; + + + float ap, ai; + int sp = 0 ; + int kp = 0 ; + int ki = 0 ; + int k = 0 ; + + + + + // Definir sus propios valores del teclado +char tabla de teclas [] = { '1' , '2' , '3' , '*' , + '4' , '5' , '6' , '0' , + '7' , ' 8 ' , ' 9 ' , ' # ' , + ' A ' , ' B ' , ' C ' , ' D '}; + + int Teclado [] = { 1 , 4 , 7 , 13 , + 2 , 5 , 8 , 0 , + 3 , 6 , 9 , 14 , + 10 , 11 , 12 , 15 + }; + + uint32_t cbAfterInput ( uint32_t clave) + { + + if (KEYPAD [clave] == 'A' ) + { + Teclado [clave] = 0 ; + C ++; + } + + if (KEYPAD [clave] == 'B' ) { + Teclado [clave] = 0 ; + sp = 0 ; + kp = 0 ; + ki = 0 ; + kd = 0 ; + } + + + if (KEYTABLE [clave] == 'D' ) { + Teclado [clave] = 0 ; + p = 1 ; + } + +//////////// + + if (c == 0 ) { + + SP = SP + Teclado [clave]; + + } + + if (c == 1 ) { + + kp kp = + Teclado [clave]; + + } + if (c == 2 ) { + + ki ki = + Teclado [clave]; + + } + if (c == 3 ) { + + KD = kd + Teclado [clave]; + + } + +//////////// + + + volver 0 ; + } // Fin unit32_t + + + + int main () { + teclado teclado (D9,D8,D7,D6,D5,D4,D3,D2); + keypad.CallAfterInput (y cbAfterInput); + keypad.Start (); + + lcd.cls (); + lcd. printf ( "SP:% d" , sp); + lcd.locate ( 8 , 0 ); + lcd. printf ( "Kp:% d" , kp); + lcd.locate ( 0 , 1 ); + lcd. printf ( "Ki:% d" , ki); + lcd.locate ( 8 , 1 ); + lcd. printf ( "Kd:% d" , kd); + lcd.writeCommand (C1); // escribimos ONU comando segun el manual del módulo del LCD + lcd.locate ( 0 , 0 ); + lcd. printf ( "SP:% d" , sp); + + mientras que ( 1 ) { + + + if (c == 0 ) { + lcd.locate ( 3 , 0 ); + . lcd putc ( 0xFE ); + lcd.locate ( 4 , 0 ); + lcd. printf ( "% d" , sp); + + // NUEVO !! (no ceros en - proceso) + if (sp < 10 ) { + lcd.locate ( 5 , 0 ); + . lcd putc ( 0xFE ); + } + + if (sp < 100 ) { + lcd.locate ( 6 , 0 ); + . lcd putc ( 0xFE ); + } + + lcd.locate ( 4 , 0 ); + lcd. printf ( "% d" , sp); + //wait(0.1); + + // + + } + + if (c == 1 ) { + lcd.locate ( 11 , 0 ); + . lcd putc ( 0xFE ); + lcd.locate ( 12 , 0 ); + lcd. printf ( "% d" , kp); + + // NUEVO !! (no ceros en - proceso) + if (kp < 10 ) { + lcd.locate ( 13 , 0 ); + . lcd putc ( 0xFE ); + } + + if (kp < 100 ) { + lcd.locate ( 14 , 0 ); + . lcd putc ( 0xFE ); + } + + lcd.locate ( 12 , 0 ); + lcd. printf ( "% d" , kp); + //wait(0.1); + + // + + } + + if (c == 2 ) { + lcd.locate ( 3 , 1 ); + . lcd putc ( 0xFE ); + lcd.locate ( 4 , 1 ); + lcd. printf ( "% d" , ki); + + // NUEVO !! (no ceros en - proceso) + if (Ki < 10 ) { + lcd.locate ( 5 , 1 ); + . lcd putc ( 0xFE ); + } + + if (ki < 100 ) { + lcd.locate ( 6 , 1 ); + . lcd putc ( 0xFE ); + } + + lcd.locate ( 4 , 1 ); + lcd. printf ( "% d" , ki); + //wait(0.2); + + // + + } + if (c == 3 ) { + lcd.locate ( 11 , 1 ); + . lcd putc ( 0xFE ); + lcd.locate ( 12 , 1 ); + lcd. printf ( "% d" , kd); + + // NUEVO !! (no ceros en - proceso) + if (kd < 10 ) { + lcd.locate ( 13 , 1 ); + . lcd putc ( 0xFE ); + } + + if (kd < 100 ) { + lcd.locate ( 14 , 1 ); + . lcd putc ( 0xFE ); + } + + lcd.locate ( 12 , 1 ); + lcd. printf ( "% d" , kd); + //wait(0.2); + } + + if (c == 4 || c> 4 ) { + c = 0 ; + } + + if (p == 1 ) { + c = 0 ; + romper ; + } + + } + + + lcd.writeCommand (C4); // escribimos ONU comando segun el manual del módulo del LCD para quitar el cursor bajo + lcd.cls (); // Borra la Pantalla + . lcd printf ( "Guardados!" ); + wait ( 2 ); + lcd.cls (); + . lcd printf ( "INICIA EL PID" ); + wait ( 2 ); + // se imprimen los reseca del control de ************************************* **** + + i = sp / 999 ; + lcd.cls (); + lcd. printf ( "Er:% 2F." , m); + lcd.locate ( 8 , 0 ); + lcd. printf ( "Yo:% 2F." , j); + lcd.locate ( 0 , 1 ); + lcd. printf ( "SP:% d" , sp); + lcd.locate ( 8 , 1 ); + lcd. printf ( "Co: 1f%." , h); + wait ( 2 ); + + // + while ( 1 ) { + + spi = sp * ( 3.3 / 999 ); + + //wait(0.3); + i = sp / 999 ; + if (i <= 1 ) { + + j = Ain; + ji = j * 3.3 ; + m = (SPI-ji); + // N = m * 100; + ap = kp * m; + ai = (Ki * m) + ai; + + ad = kd * (m-mv); + h = p + ai + ad; + + si (h> 999 ) { + h = 999 ; + } + si (h < 0 ) { + h = 0 ; + } + + g = (h / 999 ); + + Aout = g; + esperar ( 0,22 ); + + + lcd.cls (); + lcd. printf ( "Er:% 2F." , m); + lcd.locate ( 8 , 0 ); + lcd. printf ( "Yo:% 2F." , j); + lcd.locate ( 0 , 1 ); + lcd. printf ( "SP:% d" , sp); + lcd.locate ( 8 , 1 ); + lcd. printf ( "Co: 1f%." , h); + } + + + MV = m; + } // Si bien + // + + } // Int main \ No newline at end of file
diff -r 000000000000 -r 9a67044d749e mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Sat Feb 25 14:21:11 2017 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/2e9cc70d1897 \ No newline at end of file