![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Tarea3-Versión1. Uso de un teclado matricial 4x4 para la asignación de parámetros a un PID tal que al presionar cualquier tecla, se escucha un sonido
Dependencies: FPointer TextLCD keypad mbed
Fork of Tarea3-V1 by
main.cpp@1:d030741c7919, 2014-04-23 (annotated)
- Committer:
- juniorACA
- Date:
- Wed Apr 23 16:25:54 2014 +0000
- Revision:
- 1:d030741c7919
- Parent:
- 0:e8c0430c5d6d
- Child:
- 2:5ac1bbc59324
Teclado Matricial
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
avallejopo | 0:e8c0430c5d6d | 1 | #include "mbed.h" |
avallejopo | 0:e8c0430c5d6d | 2 | #include "keypad.h" |
avallejopo | 0:e8c0430c5d6d | 3 | #include "TextLCD.h" |
avallejopo | 0:e8c0430c5d6d | 4 | #include "stdlib.h" |
avallejopo | 0:e8c0430c5d6d | 5 | |
avallejopo | 0:e8c0430c5d6d | 6 | TextLCD lcd(PTB10, PTB11, PTE2, PTE3, PTE4, PTE5); // rs, e, d4-d7 |
avallejopo | 0:e8c0430c5d6d | 7 | AnalogIn y(PTB0); |
avallejopo | 0:e8c0430c5d6d | 8 | AnalogOut u(PTE30); |
avallejopo | 0:e8c0430c5d6d | 9 | int C1=0x0F; // Cursor |
avallejopo | 0:e8c0430c5d6d | 10 | int C4=0x0C; // quito cursor bajo |
avallejopo | 0:e8c0430c5d6d | 11 | int spnum=0,kinum=0,kpnum=0,kdnum=0,pos=1,tecla,flag1=1,num=0; |
avallejopo | 0:e8c0430c5d6d | 12 | int err, med, yr, pid, ap, ai, ad, err_v; |
avallejopo | 0:e8c0430c5d6d | 13 | float pidn; |
avallejopo | 0:e8c0430c5d6d | 14 | int flagt=0; |
avallejopo | 0:e8c0430c5d6d | 15 | Timer t; |
avallejopo | 0:e8c0430c5d6d | 16 | char cadena[3]={' ',' ',' '}; |
avallejopo | 0:e8c0430c5d6d | 17 | int llena=0; //verificar que la entrada es de 3 numeros |
avallejopo | 0:e8c0430c5d6d | 18 | // Define your own keypad values |
avallejopo | 0:e8c0430c5d6d | 19 | char Keytable[] = { '1', '2', '3', 'A', |
avallejopo | 0:e8c0430c5d6d | 20 | '4', '5', '6', 'B', |
avallejopo | 0:e8c0430c5d6d | 21 | '7', '8', '9', 'C', |
avallejopo | 0:e8c0430c5d6d | 22 | '*', '0', '#', 'D' |
avallejopo | 0:e8c0430c5d6d | 23 | }; |
juniorACA | 1:d030741c7919 | 24 | PwmOut sonido(PTA12); |
avallejopo | 0:e8c0430c5d6d | 25 | |
avallejopo | 0:e8c0430c5d6d | 26 | uint32_t cbAfterInput(uint32_t index) { |
avallejopo | 0:e8c0430c5d6d | 27 | tecla=index; |
avallejopo | 0:e8c0430c5d6d | 28 | flag1=0; |
avallejopo | 0:e8c0430c5d6d | 29 | return 0; |
avallejopo | 0:e8c0430c5d6d | 30 | } |
avallejopo | 0:e8c0430c5d6d | 31 | |
avallejopo | 0:e8c0430c5d6d | 32 | int main() { |
juniorACA | 1:d030741c7919 | 33 | //Asignación de pines para el teclado matricial |
juniorACA | 1:d030741c7919 | 34 | // r3 r2 r1 r0 c3 c2 c1 c0 |
avallejopo | 0:e8c0430c5d6d | 35 | Keypad keypad(PTA2, PTD4, PTD3, PTD7,PTA13, PTD5, PTD0, PTD2); |
avallejopo | 0:e8c0430c5d6d | 36 | ini1: |
avallejopo | 0:e8c0430c5d6d | 37 | err=0; med=0; yr=0; pid=0; ap=0; ai=0; ad=0; err_v=0;pidn=0; |
avallejopo | 0:e8c0430c5d6d | 38 | lcd.cls(); // Borrar Pantalla |
avallejopo | 0:e8c0430c5d6d | 39 | lcd.writeCommand(C1);//escribimos un comando segun el manual del modulo LCD |
avallejopo | 0:e8c0430c5d6d | 40 | |
avallejopo | 0:e8c0430c5d6d | 41 | lcd.locate(8,0); |
avallejopo | 0:e8c0430c5d6d | 42 | lcd.printf("Kp=%d",kpnum); |
avallejopo | 0:e8c0430c5d6d | 43 | lcd.locate(0,1); |
avallejopo | 0:e8c0430c5d6d | 44 | lcd.printf("Ki=%d",kinum); |
avallejopo | 0:e8c0430c5d6d | 45 | lcd.locate(8,1); |
avallejopo | 0:e8c0430c5d6d | 46 | lcd.printf("Kd=%d",kdnum); |
avallejopo | 0:e8c0430c5d6d | 47 | lcd.locate(0,0); |
avallejopo | 0:e8c0430c5d6d | 48 | lcd.printf("Sp=%d",spnum); |
avallejopo | 0:e8c0430c5d6d | 49 | |
avallejopo | 0:e8c0430c5d6d | 50 | ini2: |
avallejopo | 0:e8c0430c5d6d | 51 | keypad.CallAfterInput(&cbAfterInput); |
avallejopo | 0:e8c0430c5d6d | 52 | keypad.Start(); |
avallejopo | 0:e8c0430c5d6d | 53 | if(flag1==0){ |
avallejopo | 0:e8c0430c5d6d | 54 | if(Keytable[tecla]=='B'){ |
juniorACA | 1:d030741c7919 | 55 | sonido=0.2f; |
juniorACA | 1:d030741c7919 | 56 | wait(0.1); |
juniorACA | 1:d030741c7919 | 57 | sonido=0.0; |
avallejopo | 0:e8c0430c5d6d | 58 | cadena[0]=' ';cadena[1]=' ';cadena[2]=' '; |
avallejopo | 0:e8c0430c5d6d | 59 | llena=0; |
avallejopo | 0:e8c0430c5d6d | 60 | num=0; |
avallejopo | 0:e8c0430c5d6d | 61 | if(pos==4){ |
avallejopo | 0:e8c0430c5d6d | 62 | pos=1;} |
avallejopo | 0:e8c0430c5d6d | 63 | else { |
avallejopo | 0:e8c0430c5d6d | 64 | pos++;} |
avallejopo | 0:e8c0430c5d6d | 65 | } |
avallejopo | 0:e8c0430c5d6d | 66 | else if(Keytable[tecla]=='A'){ |
juniorACA | 1:d030741c7919 | 67 | sonido=0.2f; |
juniorACA | 1:d030741c7919 | 68 | wait(0.1); |
juniorACA | 1:d030741c7919 | 69 | sonido=0.0; |
avallejopo | 0:e8c0430c5d6d | 70 | llena--; |
avallejopo | 0:e8c0430c5d6d | 71 | cadena[llena]=' '; |
avallejopo | 0:e8c0430c5d6d | 72 | num=strtod(cadena,NULL); |
avallejopo | 0:e8c0430c5d6d | 73 | } |
avallejopo | 0:e8c0430c5d6d | 74 | else if((tecla==0)||(tecla==1)||(tecla==2)||(tecla==4)||(tecla==5)||(tecla==6)||(tecla==8)||(tecla==9)||(tecla==10)||(tecla==13)){ |
juniorACA | 1:d030741c7919 | 75 | sonido=0.2f; |
juniorACA | 1:d030741c7919 | 76 | wait(0.1); |
juniorACA | 1:d030741c7919 | 77 | sonido=0.0; |
avallejopo | 0:e8c0430c5d6d | 78 | if(llena<3){ |
avallejopo | 0:e8c0430c5d6d | 79 | cadena[llena]=Keytable[tecla]; |
avallejopo | 0:e8c0430c5d6d | 80 | num=strtod(cadena,NULL); |
avallejopo | 0:e8c0430c5d6d | 81 | llena++;} |
avallejopo | 0:e8c0430c5d6d | 82 | } |
avallejopo | 0:e8c0430c5d6d | 83 | else if(Keytable[tecla]=='D'){ |
juniorACA | 1:d030741c7919 | 84 | sonido=0.2f; |
juniorACA | 1:d030741c7919 | 85 | wait(0.1); |
juniorACA | 1:d030741c7919 | 86 | sonido=0.0; |
avallejopo | 0:e8c0430c5d6d | 87 | lcd.writeCommand(C4);//escribimos un comando segun el manual del modulo LCD para quitar cursor bajo |
avallejopo | 0:e8c0430c5d6d | 88 | lcd.cls(); //borra la pantalla |
avallejopo | 0:e8c0430c5d6d | 89 | lcd.printf(" GUARDADOS!"); |
avallejopo | 0:e8c0430c5d6d | 90 | wait(1); |
avallejopo | 0:e8c0430c5d6d | 91 | lcd.cls(); |
avallejopo | 0:e8c0430c5d6d | 92 | lcd.printf(" INICIA EL PID"); |
avallejopo | 0:e8c0430c5d6d | 93 | wait(1); |
avallejopo | 0:e8c0430c5d6d | 94 | // se imprimen los parches del control ***************************************** |
avallejopo | 0:e8c0430c5d6d | 95 | lcd.cls(); |
avallejopo | 0:e8c0430c5d6d | 96 | lcd.printf("Er=%d",err); |
avallejopo | 0:e8c0430c5d6d | 97 | lcd.locate(8,0); |
avallejopo | 0:e8c0430c5d6d | 98 | lcd.printf("Me=%d",med); |
avallejopo | 0:e8c0430c5d6d | 99 | lcd.locate(0,1); |
avallejopo | 0:e8c0430c5d6d | 100 | lcd.printf("Sp=%d",spnum); |
avallejopo | 0:e8c0430c5d6d | 101 | lcd.locate(8,1); |
avallejopo | 0:e8c0430c5d6d | 102 | lcd.printf("Co=%d",pid); |
avallejopo | 0:e8c0430c5d6d | 103 | wait(2); |
avallejopo | 0:e8c0430c5d6d | 104 | cicloPID: |
avallejopo | 0:e8c0430c5d6d | 105 | if(Keytable[tecla]=='C'){ |
juniorACA | 1:d030741c7919 | 106 | sonido=0.2f; |
juniorACA | 1:d030741c7919 | 107 | wait(0.1); |
juniorACA | 1:d030741c7919 | 108 | sonido=0.0; |
avallejopo | 0:e8c0430c5d6d | 109 | spnum=0;kinum=0;kpnum=0;kdnum=0;llena=0;pos=1;flag1=1; |
avallejopo | 0:e8c0430c5d6d | 110 | cadena[0]=' ';cadena[1]=' ';cadena[2]=' '; |
avallejopo | 0:e8c0430c5d6d | 111 | goto ini1;} |
avallejopo | 0:e8c0430c5d6d | 112 | med=999*y.read(); //leer puerto analogo y asignar a med |
avallejopo | 0:e8c0430c5d6d | 113 | err = (spnum-med); //se calcula el error |
avallejopo | 0:e8c0430c5d6d | 114 | ap = kpnum*err; //se calcula la accion proporcinal |
avallejopo | 0:e8c0430c5d6d | 115 | if(ai<100){// se verifica que la accion integral no sea muy grande |
avallejopo | 0:e8c0430c5d6d | 116 | ai =(kinum*err)+ai;} //calculo de la integral del error |
avallejopo | 0:e8c0430c5d6d | 117 | ad = kdnum*(err-err_v); //calculo de la accion derivativa |
avallejopo | 0:e8c0430c5d6d | 118 | pid = (ap+ai+ad); |
avallejopo | 0:e8c0430c5d6d | 119 | if(pid<=0){// se verifica que pid sea positivo |
avallejopo | 0:e8c0430c5d6d | 120 | pid=0;} |
avallejopo | 0:e8c0430c5d6d | 121 | if (pid > 999){// se verifica que pid sea menor o igual la valor maximo |
avallejopo | 0:e8c0430c5d6d | 122 | pid=999;} |
avallejopo | 0:e8c0430c5d6d | 123 | err_v = err;// se actualizan las variables |
avallejopo | 0:e8c0430c5d6d | 124 | if(flagt==0){//se muestran las variables |
avallejopo | 0:e8c0430c5d6d | 125 | t.start(); |
avallejopo | 0:e8c0430c5d6d | 126 | flagt=1;} |
avallejopo | 0:e8c0430c5d6d | 127 | if(t>=0.3){ |
avallejopo | 0:e8c0430c5d6d | 128 | lcd.locate(3,0);lcd.printf(" "); |
avallejopo | 0:e8c0430c5d6d | 129 | lcd.locate(3,0);lcd.printf("%d",err); |
avallejopo | 0:e8c0430c5d6d | 130 | lcd.locate(11,0);lcd.printf(" "); |
avallejopo | 0:e8c0430c5d6d | 131 | lcd.locate(11,0);lcd.printf("%d",med); |
avallejopo | 0:e8c0430c5d6d | 132 | lcd.locate(3,1);lcd.printf(" "); |
avallejopo | 0:e8c0430c5d6d | 133 | lcd.locate(3,1);lcd.printf("%d",spnum); |
avallejopo | 0:e8c0430c5d6d | 134 | lcd.locate(11,1);lcd.printf(" "); |
avallejopo | 0:e8c0430c5d6d | 135 | lcd.locate(11,1);lcd.printf("%d",pid); |
avallejopo | 0:e8c0430c5d6d | 136 | flagt=0; |
avallejopo | 0:e8c0430c5d6d | 137 | t.reset(); |
avallejopo | 0:e8c0430c5d6d | 138 | } |
avallejopo | 0:e8c0430c5d6d | 139 | |
avallejopo | 0:e8c0430c5d6d | 140 | pidn=pid/999;//Normalizacion de la salida |
avallejopo | 0:e8c0430c5d6d | 141 | u.write(pidn);//se envia el valor pid a puerto analogico de salida (D/A) |
avallejopo | 0:e8c0430c5d6d | 142 | wait(0.005); |
avallejopo | 0:e8c0430c5d6d | 143 | goto cicloPID; |
avallejopo | 0:e8c0430c5d6d | 144 | } |
avallejopo | 0:e8c0430c5d6d | 145 | if(pos==1){ |
avallejopo | 0:e8c0430c5d6d | 146 | spnum=num; |
avallejopo | 0:e8c0430c5d6d | 147 | lcd.locate(3,0);lcd.printf(" "); |
avallejopo | 0:e8c0430c5d6d | 148 | lcd.locate(3,0);lcd.printf("%d",spnum); |
avallejopo | 0:e8c0430c5d6d | 149 | } |
avallejopo | 0:e8c0430c5d6d | 150 | else if(pos==2){ |
avallejopo | 0:e8c0430c5d6d | 151 | kpnum=num; |
avallejopo | 0:e8c0430c5d6d | 152 | lcd.locate(11,0);lcd.printf(" "); |
avallejopo | 0:e8c0430c5d6d | 153 | lcd.locate(11,0);lcd.printf("%d",kpnum); |
avallejopo | 0:e8c0430c5d6d | 154 | } |
avallejopo | 0:e8c0430c5d6d | 155 | else if(pos==3){ |
avallejopo | 0:e8c0430c5d6d | 156 | kinum=num; |
avallejopo | 0:e8c0430c5d6d | 157 | lcd.locate(3,1);lcd.printf(" "); |
avallejopo | 0:e8c0430c5d6d | 158 | lcd.locate(3,1);lcd.printf("%d",kinum); |
avallejopo | 0:e8c0430c5d6d | 159 | } |
avallejopo | 0:e8c0430c5d6d | 160 | else if(pos==4){ |
avallejopo | 0:e8c0430c5d6d | 161 | kdnum=num; |
avallejopo | 0:e8c0430c5d6d | 162 | lcd.locate(11,1);lcd.printf(" "); |
avallejopo | 0:e8c0430c5d6d | 163 | lcd.locate(11,1);lcd.printf("%d",kdnum); |
avallejopo | 0:e8c0430c5d6d | 164 | } |
avallejopo | 0:e8c0430c5d6d | 165 | } |
avallejopo | 0:e8c0430c5d6d | 166 | flag1=1; |
avallejopo | 0:e8c0430c5d6d | 167 | goto ini2; |
avallejopo | 0:e8c0430c5d6d | 168 | } |