Código para programar un PID por medio de un teclado matricial y un LCD.

Dependencies:   FPointer TextLCD keypad mbed

main.cpp

Committer:
gjusecheo
Date:
2016-06-01
Revision:
0:e4f5d9537189

File content as of revision 0:e4f5d9537189:

#include "mbed.h"
#include "keypad.h"
#include "TextLCD.h"
#include "stdlib.h"

TextLCD lcd(PTB10, PTB11, PTE2, PTE3, PTE4, PTE5); // rs, e, d4-d7
int C1=0x0F; // Cursor
int C4=0x0C; // quito cursor bajo
int kpnum=0,kinum=0,spnum=0,kdnum=0,pos=1,tecla,flag1=1,num=0;
int flagt=0;
Timer t;
char cadena[3]={' ',' ',' '};
int llena=0; //verificar que la entrada es de 3 numeros
// 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) {
    tecla=index;
    flag1=0;
    return 0;
}

int main() {
    //             f0    f1    f2    f3   c0     c1    c2    c3
    Keypad keypad(PTA1, PTA2, PTD4, PTA12,PTC7, PTC0, PTC3, PTC4);
    ini1:
    lcd.cls(); // Borrar Pantalla
    lcd.locate(0,0);
    lcd.printf("**PID-TECLADO**");
    wait(1);
    lcd.cls();
    
    lcd.locate(0,0);
    lcd.printf("Kp=%d",kpnum);
    lcd.locate(8,0);
    lcd.printf("Ki=%d",kinum);
    lcd.locate(0,1);
    lcd.printf("Kd=%d",kdnum);
    lcd.locate(8,1);
    lcd.printf("Sp=%d",spnum);
    
    //lcd.locate(3,0);
    //lcd.writeCommand(C1);//escribimos un comando segun el manual del modulo LCD
    
    ini2:
    keypad.CallAfterInput(&cbAfterInput);
    keypad.Start();
    if(flag1==0){
        if(Keytable[tecla]=='B'){
            cadena[0]=' ';cadena[1]=' ';cadena[2]=' ';
            llena=0;
            num=0;
            if(pos==4){
                pos=1;}
            else {
                pos++;}
        }
        else if(Keytable[tecla]=='A'){
            llena--;
            cadena[llena]=' ';
            num=strtod(cadena,NULL);
        }
        else if((tecla==0)||(tecla==1)||(tecla==2)||(tecla==4)||(tecla==5)||(tecla==6)||(tecla==8)||(tecla==9)||(tecla==10)||(tecla==13)){
            if(llena<3){
                cadena[llena]=Keytable[tecla];
                num=strtod(cadena,NULL);
                llena++;}
        }
        else if(Keytable[tecla]=='D'){
            lcd.writeCommand(C4);//escribimos un comando segun el manual del modulo LCD para quitar cursor bajo
            lcd.cls(); //borra la pantalla
            lcd.printf("   GUARDADOS!");
            wait(1);  
            lcd.cls();
            lcd.locate(0,0);
            lcd.printf("Kp=%d",kpnum);
            lcd.locate(8,0);
            lcd.printf("Ki=%d",kinum);
            lcd.locate(0,1);
            lcd.printf("Kd=%d",kdnum);
            lcd.locate(8,1);
            lcd.printf("Sp=%d",spnum);
            wait(2);
            cicloPID:
                if(Keytable[tecla]=='C'){
                    kpnum=0;kinum=0;kdnum=0;spnum=0;llena=0;pos=1;flag1=1;
                    cadena[0]=' ';cadena[1]=' ';cadena[2]=' ';
                    goto ini1;}
                
                if(flagt==0){//se muestran las variables
                    t.start();
                    flagt=1;}
                if(t>=0.3){
                    lcd.locate(3,0);lcd.printf("    ");
                    lcd.locate(3,0);lcd.printf("%d",kpnum);
                    lcd.locate(11,0);lcd.printf("    ");
                    lcd.locate(11,0);lcd.printf("%d",kinum);
                    lcd.locate(3,1);lcd.printf("    ");
                    lcd.locate(3,1);lcd.printf("%d",kdnum);
                    lcd.locate(11,1);lcd.printf("    ");
                    lcd.locate(11,1);lcd.printf("%d",spnum);
                    flagt=0;
                    t.reset();
                }
                goto cicloPID;
        }            
        if(pos==1){
            kpnum=num;
             lcd.locate(3,0);lcd.writeCommand(C1);
            lcd.locate(3,0);lcd.printf("    ");
            lcd.locate(3,0);lcd.printf("%d",kpnum);
        }
        else if(pos==2){
             kinum=num;
            lcd.locate(11,0);lcd.printf("    ");
            lcd.locate(11,0);lcd.printf("%d",kinum);
        }
        else if(pos==3){
           kdnum=num;
            lcd.locate(3,1);lcd.printf("    ");
            lcd.locate(3,1);lcd.printf("%d",kdnum);
        }
        else if(pos==4){
            spnum=num;
            lcd.locate(11,1);lcd.printf("    ");
            lcd.locate(11,1);lcd.printf("%d",spnum);
        }
    }
    flag1=1;
    goto ini2;        
}