Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: FPointer TextLCDlib mbed
Fork of keypad by
Revision 10:66b580afa3ea, committed 2013-12-07
- Comitter:
- caaruizze
- Date:
- Sat Dec 07 11:31:27 2013 +0000
- Parent:
- 9:e48ba5b4c497
- Commit message:
- Programa para ingresar par?mentros a un controlador PID por medio de un teclado matricial 4x4 y controlar el voltaje en una planta de primer grado simulada por un capacitor de 33uF y una resistencia de 39K
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/FPointer.lib Sat Dec 07 11:31:27 2013 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/AjK/code/FPointer/#56e309e76c19
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TextLCD.lib Sat Dec 07 11:31:27 2013 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/caaruizze/code/TextLCDlib/#036c7df921be
--- a/keypad.cpp Tue Jan 31 00:31:58 2012 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,67 +0,0 @@
-#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;
- _setupRiseTrigger();
-}
-
-void Keypad::Start(void) {
- _cols = 0x0F;
-}
-
-void Keypad::Stop(void) {
- _cols = 0x00;
-}
-
-void Keypad::CallAfterInput(uint32_t (*fptr)(uint32_t index)) {
- _input.attach(fptr);
-}
-
-void Keypad::_callback(int row, InterruptIn &therow) {
- wait_ms(_debounce);
- if (therow != 1)
- return;
-
- int c = -1;
- _cols = _cols & 0x0E;
- if (therow == 0)
- c = 0;
- else {
- _cols = _cols & 0x0D;
- if (therow == 0)
- c = 1;
- else {
- _cols = _cols & 0x0B;
- if (therow == 0)
- c = 2;
- else
- c = 3;
- }
- }
- _input.call(row * 4 + c);
- Start(); // Re-energize all columns
-}
-
-void Keypad::_cbRow0Rise(void) {
- _callback(0, _row0);
-}
-void Keypad::_cbRow1Rise(void) {
- _callback(1, _row1);
-}
-void Keypad::_cbRow2Rise(void) {
- _callback(2, _row2);
-}
-void Keypad::_cbRow3Rise(void) {
- _callback(3, _row3);
-}
-
-void Keypad::_setupRiseTrigger(void) {
- _row0.rise(this, &Keypad::_cbRow0Rise);
- _row1.rise(this, &Keypad::_cbRow1Rise);
- _row2.rise(this, &Keypad::_cbRow2Rise);
- _row3.rise(this, &Keypad::_cbRow3Rise);
-}
\ No newline at end of file
--- a/keypad.h Tue Jan 31 00:31:58 2012 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,111 +0,0 @@
-/* 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);
-
- /** 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:
- InterruptIn _row0;
- InterruptIn _row1;
- InterruptIn _row2;
- InterruptIn _row3;
- BusOut _cols;
- int _debounce;
- FPointer _input; // Called after each input
-
- void _callback(int row, InterruptIn &therow);
- void _cbRow0Rise(void);
- void _cbRow1Rise(void);
- void _cbRow2Rise(void);
- void _cbRow3Rise(void);
- void _setupRiseTrigger(void);
- void _dummy(void) { };
-};
-
-#endif // KEYPAD_H
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/keypad.lib Sat Dec 07 11:31:27 2013 +0000 @@ -0,0 +1,1 @@ +https://mbed.org/users/leorestrepo93/code/keypad/#e48ba5b4c497
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Sat Dec 07 11:31:27 2013 +0000
@@ -0,0 +1,426 @@
+#include "mbed.h"
+#include "keypad.h"
+#include "TextLCD.h"
+
+//Utilizar funciones del teclado
+#define KEYLEN 4 //tamaño del vector
+#define ENDKEY 15//Guardar datos < que 3 cifras
+#define BORKEY 7//Borrar datos
+#define PASKEY 12//Pasar de parametro
+#define MENKEY 14//Guardar datos
+//Se guardan los aprametros como una cadena de datos para luego convertirlos a un numero de 3 cifras
+char SP[KEYLEN];
+char KP[KEYLEN];
+char KI[KEYLEN];
+char KD[KEYLEN];
+//Se definen las variables a utilizar
+int Index = 0;
+int p=0;
+int i=0;
+int c=1;
+int sp=0;
+int kp=0;
+int ki=0;
+int kd=0;
+
+//Se DEclara los puertos de la pantalla LCD
+TextLCD lcd(PTB10,PTB11,PTE2,PTE3,PTE4,PTE5);
+// Se Declaran los puertos PTC2 PTE30
+AnalogIn me(PTC2);
+AnalogOut co(PTE30);
+
+
+// se declara el teclado y el arreglo
+int teclado[] = { 1,2,3,0,
+ 4,5,6,0,
+ 7,8,9,0,
+ 0,0,0,0};
+
+
+//Comienza programa:
+uint32_t cbAfterInput(uint32_t tecla) {
+ bool finish = false;
+
+
+ if (Index==0){
+ if (tecla==PASKEY){
+ p++;
+
+ if (p==0){
+ lcd.locate(2,0);
+ lcd.printf(":");
+ }
+ if (p==1){
+ lcd.locate(10,0);
+ lcd.printf(":");
+
+ }
+ if (p==2){
+ lcd.locate(2,1);
+ lcd.printf(":");
+ }
+ if (p==3){
+ lcd.locate(10,1);
+ lcd.printf(":");
+ }
+ if (p==4){
+ lcd.locate(2,0);
+ lcd.printf(":");
+ p=0;
+ }
+
+
+ }
+ }
+
+ if(p==0){
+
+
+ if (Index < KEYLEN - 1)
+ {
+ if ((tecla != ENDKEY) & (tecla != BORKEY) & (tecla != PASKEY)){
+ SP[Index] = teclado[tecla];
+ Index++;
+ c--;
+ if (c==0){
+ lcd.locate(3,0);
+ lcd.printf(" ");
+ c=3;
+ }
+ lcd.locate(3+i,0);
+ lcd.printf("%d",SP[i]);
+ i++;
+
+ }
+
+ if (tecla == ENDKEY){
+ finish = true;
+ c=1;
+ }
+
+ if (tecla == BORKEY){
+ memset(&SP, 0, KEYLEN);
+ Index = 0;
+ i=0;
+ lcd.locate(3,0);
+ lcd.printf(" ");
+ c=1;
+ lcd.locate(2,0);
+ lcd.printf("=");
+ }
+
+ }
+
+ if (finish || (Index == KEYLEN - 1)) {
+ sp=0;
+ if (i==3){
+ sp=(100*SP[0])+(10*SP[1])+(SP[2]);
+ }
+ if (i==2){
+ sp=(10*SP[0])+(1*SP[1]);
+ }
+ if (i==1){
+ sp=SP[0];
+ }
+ lcd.locate(3,0);
+ lcd.printf(" ");
+
+ lcd.locate(3,0);
+ lcd.printf("%d",sp);
+ memset(&SP, 0, KEYLEN);
+ Index = 0;
+ i=0;
+
+ }
+ }
+
+ if(p==1){
+
+
+
+ if (Index < KEYLEN - 1)
+ {
+ if ((tecla != ENDKEY) & (tecla != BORKEY) & (tecla != PASKEY)){
+ KP[Index] = teclado[tecla];
+ Index++;
+ c--;
+ if (c==0){
+ lcd.locate(11,0);
+ lcd.printf(" ");
+ c=3;
+ }
+ lcd.locate(11+i,0);
+ lcd.printf("%d",KP[i]);
+ i++;
+
+ }
+
+ if (tecla == ENDKEY){
+ finish = true;
+ c=1;
+ }
+
+ if (tecla == BORKEY){
+ memset(&KP, 0, KEYLEN);
+ Index = 0;
+ i=0;
+ lcd.locate(11,0);
+ lcd.printf(" ");
+ c=1;
+ lcd.locate(10,0);
+ lcd.printf("=");
+ }
+
+ }
+
+ if (finish || (Index == KEYLEN - 1)) {
+ kp=0;
+ if (i==3){
+ kp=(100*KP[0])+(10*KP[1])+(KP[2]);
+ }
+ if (i==2){
+ kp=(10*KP[0])+(1*KP[1]);
+ }
+ if (i==1){
+ kp=KP[0];
+ }
+ lcd.locate(11,0);
+ lcd.printf(" ");
+
+ lcd.locate(11,0);
+ lcd.printf("%d",kp);
+ memset(&KP, 0, KEYLEN);
+ Index = 0;
+ i=0;
+
+ }
+ }
+ if(p==2){
+
+
+
+ if (Index < KEYLEN - 1)
+ {
+ if ((tecla != ENDKEY) & (tecla != BORKEY) & (tecla != PASKEY)){
+ KI[Index] = teclado[tecla];
+ Index++;
+ c--;
+ if (c==0){
+ lcd.locate(3,1);
+ lcd.printf(" ");
+ c=3;
+ }
+ lcd.locate(3+i,1);
+ lcd.printf("%d",KI[i]);
+ i++;
+
+ }
+
+ if (tecla == ENDKEY){
+ finish = true;
+ c=1;
+ }
+
+ if (tecla == BORKEY){
+ memset(&KI, 0, KEYLEN);
+ Index = 0;
+ i=0;
+ lcd.locate(3,1);
+ lcd.printf(" ");
+ c=1;
+ lcd.locate(2,1);
+ lcd.printf("=");
+ }
+
+ }
+
+ if (finish || (Index == KEYLEN - 1)) {
+ ki=0;
+ if (i==3){
+ ki=(100*KI[0])+(10*KI[1])+(KI[2]);
+ }
+ if (i==2){
+ ki=(10*KI[0])+(1*KI[1]);
+ }
+ if (i==1){
+ ki=KI[0];
+ }
+ lcd.locate(3,1);
+ lcd.printf(" ");
+
+ lcd.locate(3,1);
+ lcd.printf("%d",ki);
+ memset(&KI, 0, KEYLEN);
+ Index = 0;
+ i=0;
+
+ }
+ }
+
+if(p==3){
+
+
+ if (Index < KEYLEN - 1)
+ {
+ if ((tecla != ENDKEY) & (tecla != BORKEY) & (tecla != PASKEY)){
+ KD[Index] = teclado[tecla];
+ Index++;
+ c--;
+ if (c==0){
+ lcd.locate(11,1);
+ lcd.printf(" ");
+ c=3;
+ }
+ lcd.locate(11+i,1);
+ lcd.printf("%d",KD[i]);
+ i++;
+
+ }
+
+ if (tecla == ENDKEY){
+ finish = true;
+ c=1;
+ }
+
+ if (tecla == BORKEY){
+ memset(&KD, 0, KEYLEN);
+ Index = 0;
+ i=0;
+ lcd.locate(11,1);
+ lcd.printf(" ");
+ c=1;
+ lcd.locate(10,1);
+ lcd.printf("=");
+ }
+
+ }
+
+ if (finish || (Index == KEYLEN - 1)) {
+ kd=0;
+ if (i==3){
+ kd=(100*KD[0])+(10*KD[1])+(KD[2]);
+ }
+ if (i==2){
+ kd=(10*KD[0])+(1*KD[1]);
+ }
+ if (i==1){
+ kd=KD[0];
+ }
+ lcd.locate(11,1);
+ lcd.printf(" ");
+
+ lcd.locate(11,1);
+ lcd.printf("%d",kd);
+ memset(&KD, 0, KEYLEN);
+ Index = 0;
+ i=0;
+
+ }
+ }
+
+ if(tecla==MENKEY){//inicia control PID:
+
+ float med,err,Sp,ap,ai,ad,PID,err_v,sal;
+ med=err=Sp=ap=ai=ad=PID=err_v=sal=0;
+
+ int k=0;
+ lcd.cls();
+ lcd.locate(3,0);
+ lcd.printf("Guardado!!");
+ wait(2);
+ lcd.cls();
+ lcd.locate(0,0);
+ lcd.printf("Er=%.2f",err);
+ lcd.locate(8,0);
+ lcd.printf("Me=%.2f",med);
+ lcd.locate(0,1);
+ lcd.printf("Sp=0.00");
+ lcd.locate(8,1);
+ lcd.printf("AC=%.2f",PID);
+
+
+
+ while (1){
+ wait(0.001);
+ k++;
+ med=me*3.3;
+ Sp=(3.3/999)*sp;
+ err=Sp-med;
+ ap=kp*err;
+ ai=(ki*err)+ai;
+ if(ai>999){
+ ai=999;
+ }
+ ad=kd*(err-err_v);
+ PID=ap+ai+ad;
+
+ if (PID<0){
+ PID=0;
+ }
+ if (PID>999){
+ PID=999;
+ }
+ co=(PID/999);
+ sal=co*3.3;
+ err_v=err;
+
+
+ if(k>100){
+
+ lcd.locate(3,0);
+ lcd.printf(" ");
+ lcd.locate(3,0);
+ lcd.printf("%.2f",err);
+ lcd.locate(11,0);
+ lcd.printf(" ");
+ lcd.locate(11,0);
+ lcd.printf("%.2f",med);
+ lcd.locate(11,1);
+ lcd.printf(" ");
+ lcd.locate(11,1);
+ lcd.printf("%.2f",sal);
+ lcd.locate(3,1);
+ lcd.printf("%.2f",Sp);
+ k=0;
+ }
+
+ }//while
+
+ }
+
+
+
+
+
+ return 0;
+}
+
+
+int main() {
+//Se elimina el bufer de los vectores
+ memset(&SP, 0, KEYLEN);
+ memset(&KP, 0, KEYLEN);
+ memset(&KI, 0, KEYLEN);
+ memset(&KD, 0, KEYLEN);
+/***********************************************/
+ Index = 0;
+ Keypad keypad(PTA2,PTD4,PTD3,PTD1,PTA13,PTD5,PTD0,PTD2);
+ keypad.CallAfterInput(&cbAfterInput);
+ keypad.Start();
+ lcd.writeCommand(0x0e);
+ lcd.locate(0,0);
+ lcd.printf("Sp:0");
+ lcd.locate(8,0);
+ lcd.printf("Kp:0");
+ lcd.locate(0,1);
+ lcd.printf("Ki:0");
+ lcd.locate(8,1);
+ lcd.printf("Kd:0");
+ while (1) {
+
+
+ wait(0.5);
+
+ }
+
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Sat Dec 07 11:31:27 2013 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/a9913a65894f \ No newline at end of file
