al oprimir cualquier tecla del teclado matricial 4x4 se genera un sonido.
Dependencies: FPointer TextLCD keypad mbed
Fork of Tarea6 by
main.cpp@0:e8c0430c5d6d, 2013-11-29 (annotated)
- Committer:
- avallejopo
- Date:
- Fri Nov 29 18:26:39 2013 +0000
- Revision:
- 0:e8c0430c5d6d
- Child:
- 1:71b5c04df300
Teclado matricial 4X4 + PID
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 | }; |
avallejopo | 0:e8c0430c5d6d | 24 | |
avallejopo | 0:e8c0430c5d6d | 25 | uint32_t cbAfterInput(uint32_t index) { |
avallejopo | 0:e8c0430c5d6d | 26 | tecla=index; |
avallejopo | 0:e8c0430c5d6d | 27 | flag1=0; |
avallejopo | 0:e8c0430c5d6d | 28 | return 0; |
avallejopo | 0:e8c0430c5d6d | 29 | } |
avallejopo | 0:e8c0430c5d6d | 30 | |
avallejopo | 0:e8c0430c5d6d | 31 | int main() { |
avallejopo | 0:e8c0430c5d6d | 32 | Keypad keypad(PTA2, PTD4, PTD3, PTD7,PTA13, PTD5, PTD0, PTD2); |
avallejopo | 0:e8c0430c5d6d | 33 | ini1: |
avallejopo | 0:e8c0430c5d6d | 34 | err=0; med=0; yr=0; pid=0; ap=0; ai=0; ad=0; err_v=0;pidn=0; |
avallejopo | 0:e8c0430c5d6d | 35 | lcd.cls(); // Borrar Pantalla |
avallejopo | 0:e8c0430c5d6d | 36 | lcd.writeCommand(C1);//escribimos un comando segun el manual del modulo LCD |
avallejopo | 0:e8c0430c5d6d | 37 | |
avallejopo | 0:e8c0430c5d6d | 38 | lcd.locate(8,0); |
avallejopo | 0:e8c0430c5d6d | 39 | lcd.printf("Kp=%d",kpnum); |
avallejopo | 0:e8c0430c5d6d | 40 | lcd.locate(0,1); |
avallejopo | 0:e8c0430c5d6d | 41 | lcd.printf("Ki=%d",kinum); |
avallejopo | 0:e8c0430c5d6d | 42 | lcd.locate(8,1); |
avallejopo | 0:e8c0430c5d6d | 43 | lcd.printf("Kd=%d",kdnum); |
avallejopo | 0:e8c0430c5d6d | 44 | lcd.locate(0,0); |
avallejopo | 0:e8c0430c5d6d | 45 | lcd.printf("Sp=%d",spnum); |
avallejopo | 0:e8c0430c5d6d | 46 | |
avallejopo | 0:e8c0430c5d6d | 47 | ini2: |
avallejopo | 0:e8c0430c5d6d | 48 | keypad.CallAfterInput(&cbAfterInput); |
avallejopo | 0:e8c0430c5d6d | 49 | keypad.Start(); |
avallejopo | 0:e8c0430c5d6d | 50 | if(flag1==0){ |
avallejopo | 0:e8c0430c5d6d | 51 | if(Keytable[tecla]=='B'){ |
avallejopo | 0:e8c0430c5d6d | 52 | cadena[0]=' ';cadena[1]=' ';cadena[2]=' '; |
avallejopo | 0:e8c0430c5d6d | 53 | llena=0; |
avallejopo | 0:e8c0430c5d6d | 54 | num=0; |
avallejopo | 0:e8c0430c5d6d | 55 | if(pos==4){ |
avallejopo | 0:e8c0430c5d6d | 56 | pos=1;} |
avallejopo | 0:e8c0430c5d6d | 57 | else { |
avallejopo | 0:e8c0430c5d6d | 58 | pos++;} |
avallejopo | 0:e8c0430c5d6d | 59 | } |
avallejopo | 0:e8c0430c5d6d | 60 | else if(Keytable[tecla]=='A'){ |
avallejopo | 0:e8c0430c5d6d | 61 | llena--; |
avallejopo | 0:e8c0430c5d6d | 62 | cadena[llena]=' '; |
avallejopo | 0:e8c0430c5d6d | 63 | num=strtod(cadena,NULL); |
avallejopo | 0:e8c0430c5d6d | 64 | } |
avallejopo | 0:e8c0430c5d6d | 65 | else if((tecla==0)||(tecla==1)||(tecla==2)||(tecla==4)||(tecla==5)||(tecla==6)||(tecla==8)||(tecla==9)||(tecla==10)||(tecla==13)){ |
avallejopo | 0:e8c0430c5d6d | 66 | if(llena<3){ |
avallejopo | 0:e8c0430c5d6d | 67 | cadena[llena]=Keytable[tecla]; |
avallejopo | 0:e8c0430c5d6d | 68 | num=strtod(cadena,NULL); |
avallejopo | 0:e8c0430c5d6d | 69 | llena++;} |
avallejopo | 0:e8c0430c5d6d | 70 | } |
avallejopo | 0:e8c0430c5d6d | 71 | else if(Keytable[tecla]=='D'){ |
avallejopo | 0:e8c0430c5d6d | 72 | lcd.writeCommand(C4);//escribimos un comando segun el manual del modulo LCD para quitar cursor bajo |
avallejopo | 0:e8c0430c5d6d | 73 | lcd.cls(); //borra la pantalla |
avallejopo | 0:e8c0430c5d6d | 74 | lcd.printf(" GUARDADOS!"); |
avallejopo | 0:e8c0430c5d6d | 75 | wait(1); |
avallejopo | 0:e8c0430c5d6d | 76 | lcd.cls(); |
avallejopo | 0:e8c0430c5d6d | 77 | lcd.printf(" INICIA EL PID"); |
avallejopo | 0:e8c0430c5d6d | 78 | wait(1); |
avallejopo | 0:e8c0430c5d6d | 79 | // se imprimen los parches del control ***************************************** |
avallejopo | 0:e8c0430c5d6d | 80 | lcd.cls(); |
avallejopo | 0:e8c0430c5d6d | 81 | lcd.printf("Er=%d",err); |
avallejopo | 0:e8c0430c5d6d | 82 | lcd.locate(8,0); |
avallejopo | 0:e8c0430c5d6d | 83 | lcd.printf("Me=%d",med); |
avallejopo | 0:e8c0430c5d6d | 84 | lcd.locate(0,1); |
avallejopo | 0:e8c0430c5d6d | 85 | lcd.printf("Sp=%d",spnum); |
avallejopo | 0:e8c0430c5d6d | 86 | lcd.locate(8,1); |
avallejopo | 0:e8c0430c5d6d | 87 | lcd.printf("Co=%d",pid); |
avallejopo | 0:e8c0430c5d6d | 88 | wait(2); |
avallejopo | 0:e8c0430c5d6d | 89 | cicloPID: |
avallejopo | 0:e8c0430c5d6d | 90 | if(Keytable[tecla]=='C'){ |
avallejopo | 0:e8c0430c5d6d | 91 | spnum=0;kinum=0;kpnum=0;kdnum=0;llena=0;pos=1;flag1=1; |
avallejopo | 0:e8c0430c5d6d | 92 | cadena[0]=' ';cadena[1]=' ';cadena[2]=' '; |
avallejopo | 0:e8c0430c5d6d | 93 | goto ini1;} |
avallejopo | 0:e8c0430c5d6d | 94 | med=999*y.read(); //leer puerto analogo y asignar a med |
avallejopo | 0:e8c0430c5d6d | 95 | err = (spnum-med); //se calcula el error |
avallejopo | 0:e8c0430c5d6d | 96 | ap = kpnum*err; //se calcula la accion proporcinal |
avallejopo | 0:e8c0430c5d6d | 97 | if(ai<100){// se verifica que la accion integral no sea muy grande |
avallejopo | 0:e8c0430c5d6d | 98 | ai =(kinum*err)+ai;} //calculo de la integral del error |
avallejopo | 0:e8c0430c5d6d | 99 | ad = kdnum*(err-err_v); //calculo de la accion derivativa |
avallejopo | 0:e8c0430c5d6d | 100 | pid = (ap+ai+ad); |
avallejopo | 0:e8c0430c5d6d | 101 | if(pid<=0){// se verifica que pid sea positivo |
avallejopo | 0:e8c0430c5d6d | 102 | pid=0;} |
avallejopo | 0:e8c0430c5d6d | 103 | if (pid > 999){// se verifica que pid sea menor o igual la valor maximo |
avallejopo | 0:e8c0430c5d6d | 104 | pid=999;} |
avallejopo | 0:e8c0430c5d6d | 105 | err_v = err;// se actualizan las variables |
avallejopo | 0:e8c0430c5d6d | 106 | if(flagt==0){//se muestran las variables |
avallejopo | 0:e8c0430c5d6d | 107 | t.start(); |
avallejopo | 0:e8c0430c5d6d | 108 | flagt=1;} |
avallejopo | 0:e8c0430c5d6d | 109 | if(t>=0.3){ |
avallejopo | 0:e8c0430c5d6d | 110 | lcd.locate(3,0);lcd.printf(" "); |
avallejopo | 0:e8c0430c5d6d | 111 | lcd.locate(3,0);lcd.printf("%d",err); |
avallejopo | 0:e8c0430c5d6d | 112 | lcd.locate(11,0);lcd.printf(" "); |
avallejopo | 0:e8c0430c5d6d | 113 | lcd.locate(11,0);lcd.printf("%d",med); |
avallejopo | 0:e8c0430c5d6d | 114 | lcd.locate(3,1);lcd.printf(" "); |
avallejopo | 0:e8c0430c5d6d | 115 | lcd.locate(3,1);lcd.printf("%d",spnum); |
avallejopo | 0:e8c0430c5d6d | 116 | lcd.locate(11,1);lcd.printf(" "); |
avallejopo | 0:e8c0430c5d6d | 117 | lcd.locate(11,1);lcd.printf("%d",pid); |
avallejopo | 0:e8c0430c5d6d | 118 | flagt=0; |
avallejopo | 0:e8c0430c5d6d | 119 | t.reset(); |
avallejopo | 0:e8c0430c5d6d | 120 | } |
avallejopo | 0:e8c0430c5d6d | 121 | |
avallejopo | 0:e8c0430c5d6d | 122 | pidn=pid/999;//Normalizacion de la salida |
avallejopo | 0:e8c0430c5d6d | 123 | u.write(pidn);//se envia el valor pid a puerto analogico de salida (D/A) |
avallejopo | 0:e8c0430c5d6d | 124 | wait(0.005); |
avallejopo | 0:e8c0430c5d6d | 125 | goto cicloPID; |
avallejopo | 0:e8c0430c5d6d | 126 | } |
avallejopo | 0:e8c0430c5d6d | 127 | if(pos==1){ |
avallejopo | 0:e8c0430c5d6d | 128 | spnum=num; |
avallejopo | 0:e8c0430c5d6d | 129 | lcd.locate(3,0);lcd.printf(" "); |
avallejopo | 0:e8c0430c5d6d | 130 | lcd.locate(3,0);lcd.printf("%d",spnum); |
avallejopo | 0:e8c0430c5d6d | 131 | } |
avallejopo | 0:e8c0430c5d6d | 132 | else if(pos==2){ |
avallejopo | 0:e8c0430c5d6d | 133 | kpnum=num; |
avallejopo | 0:e8c0430c5d6d | 134 | lcd.locate(11,0);lcd.printf(" "); |
avallejopo | 0:e8c0430c5d6d | 135 | lcd.locate(11,0);lcd.printf("%d",kpnum); |
avallejopo | 0:e8c0430c5d6d | 136 | } |
avallejopo | 0:e8c0430c5d6d | 137 | else if(pos==3){ |
avallejopo | 0:e8c0430c5d6d | 138 | kinum=num; |
avallejopo | 0:e8c0430c5d6d | 139 | lcd.locate(3,1);lcd.printf(" "); |
avallejopo | 0:e8c0430c5d6d | 140 | lcd.locate(3,1);lcd.printf("%d",kinum); |
avallejopo | 0:e8c0430c5d6d | 141 | } |
avallejopo | 0:e8c0430c5d6d | 142 | else if(pos==4){ |
avallejopo | 0:e8c0430c5d6d | 143 | kdnum=num; |
avallejopo | 0:e8c0430c5d6d | 144 | lcd.locate(11,1);lcd.printf(" "); |
avallejopo | 0:e8c0430c5d6d | 145 | lcd.locate(11,1);lcd.printf("%d",kdnum); |
avallejopo | 0:e8c0430c5d6d | 146 | } |
avallejopo | 0:e8c0430c5d6d | 147 | } |
avallejopo | 0:e8c0430c5d6d | 148 | flag1=1; |
avallejopo | 0:e8c0430c5d6d | 149 | goto ini2; |
avallejopo | 0:e8c0430c5d6d | 150 | } |