You are on page 1of 7

program controlpid include "libPID" '++++++++++++++ Declaracion de variables para control PID +++++++++++++++++++++ dim i ,PID as byte dim

valorpwm as float const VREF as byte = 5 dim temp_res as word dim temp as float dim Tp,kp1, opt, cnt, oldstate as byte dim txt as string[20] absolute 120 dim txt1 as string[20] absolute 140 dim txt2 as string[20] absolute 160 dim Val_Kp,Val_Ki, Val_Kd as float dim setpoint as integer dim conta as byte '+++++++++++++++++ fin de declaracion de variables +++++++++++++++++++++++++++++ ' rutina que realiza la lectura de los pulsadores y devuelve un valor segun la ' tecla pulsada Sub function leerteclado as byte dim Tp as byte Tp = 0 while (Tp= 0) if Button(PORTD, 0, 85, 1) then Tp = 65 ' A end if if Button(PORTD,1,85,1) then Tp = 66 ' B end if if Button(PORTD,2,85,1) then Tp = 67 ' C end if if Button(PORTD,3,85,1) then Tp = 68 ' D end if wend result = Tp end sub ' Procedimiento que modifica la variable KP del sistema sub procedure modificarKp ' 012345678901234567890 lcd_out(1,1," Cambiando valor ") lcd_out(2,1," Proporcional ") lcd_out(3,1,"Kp= ") floatToStr(Val_Kp, txt) ' Transforma la variable a string for i = 0 to 3 Lcd_chr(3,14+i , txt[i]) next i while true kp1= leerteclado if kp1 = "D" then ' 012345678901234567890 lcd_out(1,1," Cambio realizado") lcd_out(2,1," Correctamente ") break end if IF (kp1 <> oldstate ) then

select case kp1 case "B" Val_Kp= Val_Kp + 0.010 case "C" Val_Kp = Val_Kp - 0.010 end select floatToStr(Val_Kp, txt) for i = 0 to 3 Lcd_chr(3,14+i , txt[i]) next i end if wend end sub sub procedure modificarKi ' 012345678901234567890 lcd_out(1,1," Cambiando valor ") lcd_out(2,1," Integrativo ") lcd_out(3,1,"Ki= ") floatToStr(Val_Ki, txt) for i = 0 to 3 Lcd_chr(3,14+i , txt[i]) next i while true kp1= leerteclado if kp1 = "D" then ' 012345678901234567890 lcd_out(1,1," Cambio realizado") lcd_out(2,1," Correctamente ") break end if IF (kp1 <> oldstate ) then select case kp1 case "B" Val_Ki= Val_Ki + 0.010 case "C" Val_Ki = Val_Ki - 0.010 end select floatToStr(Val_Ki, txt) for i = 0 to 3 Lcd_chr(3,14+i , txt[i]) next i end if wend end sub sub procedure modificarKd ' 01234567890123456789 lcd_out(1,1," Cambiando valor ") lcd_out(2,1," Derivativo ") lcd_out(3,1,"Kd= ") floatToStr(Val_Kd, txt) for i = 0 to 3 Lcd_chr(3,14+i , txt[i]) next i while true kp1= leerteclado

if kp1 = "D" then ' 012345678901234567890 lcd_out(1,1," Cambio realizado ") lcd_out(2,1," Correctamente ") lcd_out(3,1," ") break end if IF (kp1 <> oldstate ) then select case kp1 case "B" Val_Kd= Val_Kd + 0.010 case "C" Val_Kd = Val_Kd - 0.010 end select floatToStr(Val_Kd, txt) for i = 0 to 3 Lcd_chr(3,14+i , txt[i]) next i end if wend end sub sub procedure modificarSP ' 012345678901234567890 lcd_out(1,1," Cambiando valor ") lcd_out(2,1, " Temperatura ") lcd_out(3,1, "SetPoint= C ") lcd_chr(3,16,223) IntToStr(setpoint, txt1) Lcd_Out(3,10, txt1) while true kp1= leerteclado if kp1 = "D" then lcd_out(1,1," lcd_out(2,1," lcd_out(3,1," exit end if

Cambio realizado ") Correctamente ") ")

IF (kp1 <> oldstate ) then select case kp1 case "B" inc(setpoint) case "C" dec(setpoint) end select intToStr(setpoint, txt1) Lcd_Out(3,10 , txt1) end if wend end sub main: TRISB = 0 Adcon1 = 0 TRISE.1 = 1

' PORTB como salida ' PORTA como digital ' Configure RE1 como entrada

TRISC = 0x00 PORTC = 0x00 TRISD= 0xFF portd = 0 Pwm_Init(5000) Pwm_Start

' Initializa modulo PWM, freq = 5kHz. ' Inicia PWM

Lcd_Init(portb) ' Initializa LCD en PORTB Lcd_Cmd(Lcd_CURSOR_OFF) '01234567890123456789 Lcd_Out(1, 1, " CONTROL PID DE ") LCD_OUT(2, 1, " TEMPERATURA ") ' 01234567890123456789 Lcd_Out(3, 1, " REALIZADO POR ") LCD_Out(4, 1, "RAUL L. MESTAS TOLA ") Delay_ms(4000) lcd_cmd(lcd_clear) PID="Q" conta = 0 setpoint = 23 Val_Kp = 0.81 Val_Ki = 0.76 Val_Kd = 0.19 init_PID(Val_Kp,Val_Ki,Val_Kd,0.0,255.0) reset_pid LCD_OUT(1,1, " PRESIONE MENU PARA ") LCD_OUT(2,1, " EMPEZAR ") PORTC.1= 0 While true '012345678901234567890

kp1 = leerteclado iniciar: IF (kp1 <> oldstate ) then select case kp1 case "A" opt = "M" Lcd_cmd(lcd_clear) ' 01234567890123456789 Lcd_Out(4, 1, "ESC OK") lcd_chr(4,7,127) lcd_chr(4,14,126) inc(conta) cnt=0 if opt = "M" and (conta = 2) then opt="Q" lcd_cmd(lcd_clear) '01234567890123456789 LCD_OUT(1,1, " PRESIONE MENU PARA ") LCD_OUT(2,1, " EMPEZAR ") conta = 0 end if case "B" if opt = "M" then Dec(cnt) end if case "C" if opt = "M" then

inc(cnt) end if end select end if if cnt = 255 then cnt = 6 else if cnt = 6 then cnt = 0 end if end if if opt = "M" then select case cnt case 0 ' 01234567890123456789 Lcd_Out(1, 1, " ") Lcd_Out(2, 1, " ") Lcd_Out(3, 1, " MENU ") case 1 ' 0123456789012345 Lcd_Out(1, 1, " CAMBIAR VALORES Lcd_Out(2, 1, " DE SETPOINT Lcd_Out(3, 1, " if leerteclado = "D" then modificarSp end if case 2 ' 01234567890123456789 Lcd_Out(1, 1, " CAMBIAR VARIABLE ") Lcd_Out(2, 1, " KP ") Lcd_Out(3, 1, " ") if leerteclado = "D" then modificarKp end if case 3 ' 0123456789012345 Lcd_Out(1, 1, " CAMBIAR VARIABLE Lcd_Out(2, 1, " KI Lcd_Out(3, 1, " if leerteclado = "D" then modificarKi end if case 4 ' 0123456789012345 Lcd_Out(1, 1, " CAMBIAR VARIABLE ") Lcd_Out(2, 1, " KD ") Lcd_Out(3, 1, " ") if leerteclado = "D" then modificarKd end if case 5 ' 01234567890123456789 Lcd_Out(1, 1, " INICIAR ") Lcd_Out(2, 1, " CONTROL PID ") Lcd_Out(3, 1, " ") ") ") ") ") ") ")

if leerteclado = "D" then PID = "H" ' 01234567890123456789 LCD_out(1, 1, "Temp= ") lcd_out(2, 1, "Setpoint= C ") '01234567890123456789 lcd_out(3, 1, "Kp= Ki= ") lcd_out(4, 1, "Kd= ") lcd_chr(2,15,223) IntToStr(setpoint, txt1) Lcd_Out(2,9, txt1) floatToStr(Val_Kp, txt) for i = 0 to 4 Lcd_chr(3,4+i , txt[i]) next i floatToStr(Val_Ki, txt) for i = 0 to 4 Lcd_chr(3,15+i , txt[i]) next i floatToStr(Val_Kd, txt) for i = 0 to 4 Lcd_chr(4,4+i , txt[i]) next i

end if

'01234567890123456789 case 6 Lcd_Out(1, 1, " ACERCA DE.. ") Lcd_Out(2, 1, " ") Lcd_Out(3, 1, " ") if leerteclado = "D" then lcd_out(1,1, " CONTROL PID ") lcd_out(2,1, " REALIZADO POR ") lcd_out(3,1, "RAUL L. MESTAS TOLA ") end if end select end if while (PID = "H") portc.1= 1 temp_res = ADC_read(6) ' lee resultado de AD conversion en 10 bit temp = (temp_res * VREF)/10.24 ' Calcula temperatura en Celsuis delay_ms(20) if temp >= setpoint then portc.0 = 1 else portc.0 = 0 end if valorpwm= PID_Calculate(setpoint,temp) 'delay_ms(20) Pwm_Change_Duty(valorpwm) FloatToStr(temp, txt2) for i = 0 to 4 Lcd_chr(1,13+i , txt2[i]) next i ' lcd_out(1,8,txt2) ' FloatToStr(valorpwm, txt2) ' lcd_out(2,1,txt2)

if

Button(PORTD, 0, 85, 1) then PID ="Q"'kp1 = 65 ' A Pwm_Change_Duty(0) portc.1 = 0 portc.0 = 0 break end if

wend wend End.

You might also like