Control PID para horno de reflujo con seguimiento de consigna
Dependencies: Debounced Pulse1 QEI RTC-DS1307 TextLCD mbed
Fork of Tarea3_procesadores_Ds1307 by
Tarea 4 procesadores 2018-1
Juan Camilo Londoño Julieta Serrano Escalante
PID para horno de reflujo conseguimiento de consigna
Diff: main.cpp
- Revision:
- 2:bef1b1c9e387
- Parent:
- 1:0639f31dd59f
- Child:
- 3:35a40be1047f
diff -r 0639f31dd59f -r bef1b1c9e387 main.cpp --- a/main.cpp Wed Sep 28 06:48:39 2016 +0000 +++ b/main.cpp Wed May 09 19:55:50 2018 +0000 @@ -3,138 +3,201 @@ #include "TextLCD.h" #include "QEI.h" #include "Rtc_Ds1307.h" +#include <Pulse1.h> -TextLCD lcd(PTB10, PTB11, PTE2, PTE3, PTE4, PTE5); // rs, e, d4-d7 +TextLCD lcd(PTB0, PTB1, PTB2, PTB3, PTC2, PTC1); // rs, e, d4, d5, d6, d7 Rtc_Ds1307 rtc(PTE0, PTE1); -QEI wheel (PTA16, PTA17, NC, 48); - +QEI wheel (PTD7, PTD6, NC, 30); DebouncedIn button_enco(PTC5); +PwmOut Sonido(PTD1); +PulseInOut irda(PTD5);// en este puerto se pone el sensor infrarrojo -PwmOut Sonido(PTA5); float periodo; -float Frecuencia=4000; -int C1=0x0C; -int m; -int Conf=0; // 0: Alarma y 1:Reloj -int Lugar=0; // Definen el punto en el cual va el programa -int i=0; -int visual=0; -int alarma=0; +float Frecuencia = 4000; +int C1 = 0x0C; +int m = 0, flag = 0; +uint8_t pot[7] = {1, 2, 4, 8, 16, 32, 64}; +uint8_t pot1[7] = {254, 253, 251, 248, 239, 223, 191}; +int Lugar = 0; // Definen el punto en el cual va el programa +int i = 0, j = 0; +int pos = 0; // Reloj:0, Alarma1:1, Alarma2:2, ... , Alarma7:7 +uint8_t state_alarms = 0, pos_alarm = 0, alarma = 0; + +//irda +int header = 0; //tiempo de cabecera pulso abajo +const int head_H = 4532; //+20% medida con osciloscopio en microsegundos +const int head_L = 4494;//-20% medida con osciloscopio +int g = 0, h = 0, count = 0; +const int T_alto = 1600;//ponga su tiempo de la prueba +const int T_bajo = 485;//ponga su tiempo de la prueba +const int num_bits = 32;//ponga su numero de bits +int num[num_bits];//cadena para almacenar todos los tiempos que conforman los bits de datos +int sec[num_bits];//cadena para almacenar la cadena codificada en binario +int dato; // tiempo de cada dato que se lee +int boton_sel = 10; +int boton[10][32]= {{1,1,1,0,0,0,0,0,1,1,1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,1,1,1,0,1,1,0}, //0 + {1,1,1,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,1,0,0,0,0,0,1,1,0,1,1,1,1,0}, //1 + {1,1,1,0,0,0,0,0,1,1,1,0,0,0,0,0,1,0,1,0,0,0,0,0,0,1,0,1,1,1,1,0}, //2 + {1,1,1,0,0,0,0,0,1,1,1,0,0,0,0,0,0,1,1,0,0,0,0,0,1,0,0,1,1,1,1,0}, //3 + {1,1,1,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,1,1,1,0,1,1,1,0}, //4 + {1,1,1,0,0,0,0,0,1,1,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,1,0,1,1,1,0}, //5 + {1,1,1,0,0,0,0,0,1,1,1,0,0,0,0,0,0,1,0,1,0,0,0,0,1,0,1,0,1,1,1,0}, //6 + {1,1,1,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,1,1,0,0,0,0,1,1,0,0,1,1,1,0}, //7 + {1,1,1,0,0,0,0,0,1,1,1,0,0,0,0,0,1,0,1,1,0,0,0,0,0,1,0,0,1,1,1,0}, //8 + {1,1,1,0,0,0,0,0,1,1,1,0,0,0,0,0,0,1,1,1,0,0,0,0,1,0,0,0,1,1,1,0}}; //9 + int main() { Rtc_Ds1307::Time_rtc tm = {}; - Rtc_Ds1307::Time_rtc Alarm = {}; + Rtc_Ds1307::Time_rtc Alarm[7] = {}; lcd.cls(); lcd.writeCommand(C1);//escribimos un comando segun el manual del modulo LCD - lcd.locate(0,0); - lcd.printf("--Alarma --Hora"); - periodo=(1/Frecuencia); Sonido.period(periodo); - rtc.getTime(tm); //lee el tiempo del DS1307 + Sonido.write(255); + while(1) { switch (Lugar) - { + { case 0: - m=wheel.getPulses(); // m son los datos del encoder - if(m!=0 && Conf==0){ - Conf=1; - wheel.reset(); - m=0; - } - - if(m!=0 && Conf==1){ - Conf=0; - wheel.reset(); - m=0; + + rtc.getTime(tm); + m = wheel.getPulses(); + if(m != 0) + { + wheel.reset(); + pos = pos + m; + m = 0; + lcd.cls(); + } + if(pos > 7) + { + pos = -1; + } + if(pos < -1) + { + pos = 7; + } + if(pos == 0) + { + lcd.locate(0,0); + lcd.printf("Time: "); + lcd.locate(8,0); + lcd.printf("%02d:",tm.hour); + lcd.printf("%02d:",tm.min); + lcd.printf("%02d",tm.sec); + lcd.locate(0,1); + lcd.printf("%02d/",tm.mon); + lcd.printf("%02d/",tm.date); + lcd.printf("%02d",tm.year); + wait(0.1); } - if(Conf==0){ - - lcd.locate(11,0); - lcd.printf("-"); - lcd.locate(1,0); - lcd.printf(">"); - lcd.locate(0,1); - wait(0.1); - - } - - if(Conf==1){ - - lcd.locate(1,0); - lcd.printf("-"); - lcd.locate(11,0); - lcd.printf(">"); - lcd.locate(0,1); + if(pos == -1) + { + lcd.locate(0,0); + lcd.printf("Alarm Setup IRDA"); wait(0.1); } - if (button_enco.falling()){ //si se pulsa boton encoder - Lugar++; - m=0; + + if(pos > 0) + { + lcd.locate(0,0); + lcd.printf("Alarm %d", pos); + lcd.locate(8,0); + lcd.printf("%02d:",Alarm[pos - 1].hour); + lcd.printf("%02d:",Alarm[pos - 1].min); + lcd.printf("%02d",Alarm[pos - 1].sec); + lcd.locate(0,1); + lcd.printf("%02d/",Alarm[pos - 1].mon); + lcd.printf("%02d/",Alarm[pos - 1].date); + lcd.printf("%02d",Alarm[pos - 1].year); + lcd.locate(13,1); + if((state_alarms&(pot[pos - 1])) == pot[pos - 1]) + { + lcd.printf("On "); + } + else + { + lcd.printf("Off"); + } + + wait(0.1); } - + for(j=0;j<7;j++) + { + if( (tm.date==Alarm[j].date)&&(tm.mon==Alarm[j].mon)&&(tm.year==Alarm[j].year)&&(tm.hour==Alarm[j].hour)&&(tm.min==Alarm[j].min)&&(tm.sec==Alarm[j].sec)&&((state_alarms&(pot[j])) == pot[j])) + { + Lugar = 2; + lcd.cls(); + lcd.locate(0,0); + lcd.printf("Active Alarm%d!!!",(j+1)); + lcd.locate(0,1); + lcd.printf("->Disable "); + lcd.locate(10,1); + lcd.printf("--5min"); + pos_alarm = j; + } + } + + if (button_enco.falling()) //si se pulsa boton encoder + { + if(pos == -1) + {Lugar = 3; + lcd.cls();} + else + {Lugar = 1;} + + } + break; case 1: switch(i) { case 0: - - - if(Conf==0) + if(pos == 0) { lcd.cls(); lcd.locate(0,0); - lcd.printf("Config. Alarma "); - wait(1); - i++; - m=0; - lcd.cls(); - lcd.locate(0,0); - lcd.printf("Dia: "); - tm.date=1; - } - - if(Conf==1) + lcd.printf("Time Setup"); + } + + if(pos > 0) { lcd.cls(); lcd.locate(0,0); - lcd.printf("Config. hora "); - wait(1); - i++; - m=0; - lcd.cls(); - lcd.locate(0,0); - lcd.printf("Dia: "); - - } - + lcd.printf("Alarm %d Setup", pos); + } + wait(1); + i++; + lcd.cls(); + lcd.locate(0,0); + lcd.printf("Day: "); break; case 1: - m=wheel.getPulses(); - if (m!=0) + m = wheel.getPulses(); + if (m != 0) { - tm.date=tm.date+m; - if(tm.date>31) + tm.date = tm.date + m; + if(tm.date > 31) { - tm.date=31; + tm.date = 1; } - if(tm.date<1) + if(tm.date < 1) { - tm.date=1; + tm.date = 31; } - wheel.reset(); - m=0; + m = 0; } lcd.locate(5,0); lcd.printf("%02d",tm.date); @@ -142,60 +205,51 @@ if (button_enco.falling()) { i++; - m=0; lcd.cls(); lcd.locate(0,0); - lcd.printf("Mes: "); - //tm.mon=1; + lcd.printf("Month: "); } - break; case 2: - m=wheel.getPulses(); - if (m!=0) + m = wheel.getPulses(); + if (m != 0) { - tm.mon=tm.mon+m; - if(tm.mon>12) + tm.mon = tm.mon + m; + if(tm.mon > 12) { - tm.mon=12; + tm.mon = 1; } - if(tm.mon<1) + if(tm.mon < 1) { - tm.mon=1; + tm.mon = 12; } - wheel.reset(); - m=0; + m = 0; } - lcd.locate(5,0); + lcd.locate(7,0); lcd.printf("%02d",tm.mon); if (button_enco.falling()) { i++; - m=0; lcd.cls(); lcd.locate(0,0); lcd.printf("Year: "); - } - break; case 3: - m=wheel.getPulses(); - if (m!=0) + m = wheel.getPulses(); + if (m != 0) { - tm.year=tm.year+m; - - if(tm.year<2000) + tm.year = tm.year + m; + if(tm.year < 2000) { - tm.year=2000; + tm.year = 2000; } - wheel.reset(); - m=0; + m = 0; } lcd.locate(6,0); lcd.printf("%02d",tm.year); @@ -203,306 +257,297 @@ if (button_enco.falling()) { i++; - m=0; lcd.cls(); lcd.locate(0,0); - lcd.printf("Hora: "); - + lcd.printf("Hours: "); } - break; - case 4: - m=wheel.getPulses(); - - if (m!=0) + case 4: + m = wheel.getPulses(); + if (m != 0) { - tm.hour=tm.hour + m; - if(tm.hour>23) + tm.hour = tm.hour + m; + if(tm.hour > 23) { - tm.hour=23; + tm.hour = 0; } - if(tm.hour<0) + if(tm.hour < 0) { - tm.hour=0; + tm.hour = 23; } - wheel.reset(); - m=0; + m = 0; } - lcd.locate(6,0); + lcd.locate(7,0); lcd.printf("%02d",tm.hour); if (button_enco.falling()) { i++; - m=0; lcd.cls(); lcd.locate(0,0); - lcd.printf("Minuto: "); - //tm.min=0; + lcd.printf("Minutes: "); } break; case 5: - m=wheel.getPulses(); - if (m!=0) + m = wheel.getPulses(); + if (m != 0) { - tm.min=tm.min+m; - if(tm.min>59) + tm.min = tm.min + m; + if(tm.min > 59) { - tm.min=59; + tm.min = 0; } - if(tm.min<0) + if(tm.min < 0) { - tm.min=0; + tm.min = 59; } wheel.reset(); - m=0; + m = 0; } - lcd.locate(8,0); + lcd.locate(9,0); lcd.printf("%02d",tm.min); if (button_enco.falling()) { i++; - m=0; lcd.cls(); lcd.locate(0,0); - lcd.printf("Segundo: "); - //tm.sec=0; + lcd.printf("Seconds: "); } - break; case 6: - m=wheel.getPulses(); - if (m!=0) + m = wheel.getPulses(); + if (m != 0) { - tm.sec=tm.sec+m; - if(tm.sec>59) + tm.sec = tm.sec + m; + if(tm.sec > 59) { - tm.sec=59; + tm.sec = 0; } - if(tm.sec<0) + if(tm.sec < 0) { - tm.sec=0; + tm.sec = 59; } - wheel.reset(); - m=0; + m = 0; } lcd.locate(9,0); lcd.printf("%02d",tm.sec); if (button_enco.falling()) { - /////////////////// - if(Conf==0) - { - Alarm=tm; - lcd.cls(); - lcd.locate(0,0); - lcd.printf("Alarma config. "); - wait(1); - i=0; - m=0; - Lugar++; - lcd.cls(); - - } - - if(Conf==1) + if(pos==0) { rtc.setTime(tm, false, false); rtc.startClock(); lcd.cls(); - lcd.locate(0,0); - lcd.printf("Hora Config. "); + lcd.locate(0, 0); + lcd.printf("Time Set"); wait(1); - i=0; - m=0; - Lugar=0; + i = 0; + Lugar = 0; + lcd.cls(); + } + if(pos > 0) + { + i++; + lcd.cls(); lcd.locate(0,0); - lcd.printf("--Alarma --Hora"); - } - - /////////////////// + lcd.printf("Enable: Off"); + } } - break; - + + case 7: + m = wheel.getPulses(); + if (m != 0) + { + if(flag == 0) + { + lcd.locate(8,0); + lcd.printf("On "); + flag = 1; //se activa la alarma + } + else + if(flag == 1) + { + lcd.locate(8,0); + lcd.printf("Off"); + flag = 0; //se desactiva la alarma + } + wheel.reset(); + m = 0; + } + + if (button_enco.falling()) + { + if(flag == 1) + { + state_alarms = state_alarms | pot[(pos - 1)]; + } + if(flag == 0) + { + state_alarms = state_alarms & pot1[(pos - 1)]; + } + Alarm[pos - 1] = tm; + lcd.cls(); + lcd.locate(0,0); + lcd.printf("Alarm %d Set", pos); + wait(1); + i = 0; + Lugar = 0; + flag = 0; + lcd.cls(); + } + break; } break; case 2: - - rtc.getTime(tm); - - if(visual==0) - { - - lcd.locate(0,0); - lcd.printf("Hora: "); - lcd.locate(8,0); - lcd.printf("%02d:",tm.hour); - lcd.printf("%02d:",tm.min); - lcd.printf("%02d",tm.sec); - - lcd.locate(0,1); - lcd.printf("%02d/",tm.mon); - lcd.printf("%02d/",tm.date); - lcd.printf("%02d",tm.year); - wait(0.1); - ////////// - m=wheel.getPulses(); - if(m!=0) - { - visual=1; - wheel.reset(); - m=0; - } - - ////// + Sonido.write(0.5); + wait(0.2); + Sonido.write(255); + wait(0.2); + Sonido.write(0.5); + wait(0.2); + Sonido.write(255); + wait(0.2); - } - - if(visual==1) - { - lcd.locate(0,0); - lcd.printf("Alarma: "); - lcd.locate(8,0); - lcd.printf("%02d:",Alarm.hour); - lcd.printf("%02d:",Alarm.min); - lcd.printf("%02d",Alarm.sec); + m = wheel.getPulses(); - - lcd.locate(0,1); - lcd.printf("%02d/",Alarm.mon); - lcd.printf("%02d/",Alarm.date); - lcd.printf("%02d",Alarm.year); - wait(0.1); - //// - m=wheel.getPulses(); - if(m!=0) + if(alarma == 0) //pos_alarm + { + if(m != 0) { - visual=0; - wheel.reset(); - m=0; - } - - //// - } - - if( (tm.date==Alarm.date)&&(tm.mon==Alarm.mon)&&(tm.year==Alarm.year)&&(tm.hour==Alarm.hour)&&(tm.min==Alarm.min)&&(tm.sec==Alarm.sec) ) - { - Lugar++; - lcd.cls(); - lcd.locate(0,0); - lcd.printf("Alarma Activa!!!"); - wait(1); - lcd.locate(0,0); - lcd.printf("->Desactivar "); - lcd.locate(0,1); - lcd.printf("--Aplazar "); - m=0; - } - - break; - - case 3: - - if(alarma==0) - { - Sonido.write(0.5); - wait(0.2); - Sonido.write(0); - wait(0.2); - Sonido.write(0.5); - wait(0.2); - Sonido.write(0); - wait(0.2); - - m=wheel.getPulses(); - if(m!=0) - { - alarma=1; - m=0; + alarma = 1; wheel.reset(); - - lcd.locate(1,0); + m = 0; + lcd.locate(1,1); lcd.printf("-"); - lcd.locate(1,1); + lcd.locate(11,1); lcd.printf(">"); } } - - /////////////////// - if(alarma==1) + if(alarma == 1) { - Sonido.write(0.5); - wait(0.2); - Sonido.write(0); - wait(0.2); - Sonido.write(0.5); - wait(0.2); - Sonido.write(0); - wait(0.2); - - m=wheel.getPulses(); - if(m!=0) + if(m != 0) { - alarma=0; - m=0; + alarma = 0; wheel.reset(); - - lcd.locate(1,0); + m = 0; + lcd.locate(1,1); lcd.printf(">"); - lcd.locate(1,1); + lcd.locate(11,1); lcd.printf("-"); } } if (button_enco.falling()) { - if(alarma==0) + if(alarma == 0) { - Lugar=0; wheel.reset(); m=0; - Sonido.write(0); + Sonido.write(255); lcd.cls(); - lcd.locate(0,0); - lcd.printf("--Alarma --Hora"); + wait(0.5); } if(alarma==1) { - Lugar=2; rtc.getTime(tm); - if(tm.min!=58) + if(tm.min >= 55) { - Alarm.min=tm.min+1; + Alarm[pos_alarm].min = (tm.min + 5) - 60; + Alarm[pos_alarm].hour = tm.hour + 1; } - if(tm.min==59) + else { - Alarm.min=0; - Alarm.hour=tm.hour+1; + Alarm[pos_alarm].min = tm.min + 5; } - Alarm.sec=tm.sec; lcd.cls(); alarma=0; } - + Lugar = 0; } - + break; + + case 3: + lcd.locate(0,0); + lcd.printf("Waiting IRDA"); + header = 0; + header = irda.read_low_us(); //funcion para leer un pulso de caida o bajo + if (header > head_L && header < head_H)//verificar que este en la tolerancia +-20% + { + //leo los datos de la trama y se meten a un arreglo + wait_us(2000); + for(g = 0; g<(num_bits-1); g++) // POR OSCILOSCOPIO se determina que llegan (num_bits),datos + { + dato = irda.read_high_us(); //leer un bit de datos que es pulso arriba en este control + num[g]=dato; + wait_us(332); + } + for(g = 0; g<num_bits; g++) // guardo la secuancia en binario + { + if(num[g] > ((T_alto+T_bajo)/2)){sec[g]=1;} + else {sec[g]=0;} + } + for(h = 0; h < 8; h++) + { + for(g = 0; g < 32; g++) + { + if(sec[g]==boton[h][g]) //en caso de que un bit no coincida se descarta el boton 1 + { + count++; + if(count==32) + { + boton_sel = h - 1; + lcd.cls(); + lcd.locate(0,0); + lcd.printf("Alarm %d: ",(boton_sel+1)); + if(boton_sel >= 0) + { + if((state_alarms&(pot[boton_sel])) == pot[boton_sel]) + { + state_alarms = state_alarms & pot1[boton_sel]; + lcd.locate(10,0); + lcd.printf("Off"); + wait(1.5); + lcd.cls(); + } + else + { + state_alarms = state_alarms | pot[boton_sel]; + lcd.locate(10,0); + lcd.printf("On "); + wait(1.5); + lcd.cls(); + } + } + else + { + Lugar = 0; + pos = 0; + lcd.cls(); + } + } + } + } + count = 0; + } + } + break; } } -} \ No newline at end of file +}