yo declaro asi las variables, en el main lo uso asi (codigo donde usas las variables).... en la interrupcion las uso asi (codigo donde se usan las variables).... pero a veces falla y no responde como corresponde se que el codigo del PID es correcto, no se que puede estar sucediendo
estaba lidiando con u pequeño pero molesta problema al momento de declarar variables en un pic 16f877a con MikroC Pro, el problema surge cuando necesito declarar una variable que voy a utilizar en el void main y en el vector interrupt
// Lcd pinout settings
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D4 at RB0_bit;
// Pin direction
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D7_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB0_bit;
char text [4] ;
char text1 [6] ;
char text2 [6] ;
char text3 [6] ;
char text4 [5] ;
int sp , my_P , I , my_D , pid, oldpid ;
float kp,ki,kd, E, E_1, E_2, sal, pv , Radc;
int T =10000;
void interrupt (){
// Toma de datos
Radc = Adc_Read (0) ;
pv = 0.244 * Radc ; //2.5 * Radc /10,23 ;
E = sp - pv ;
floattostr(pv,text4) ;
//Operaciones
kp = (100/my_P*sp) ;
Ki = (E+(I/T*(E_1+E_2))) ;
kd = (my_D*(E-E_1));
sal = kp*ki+kd ;
if (sal > 255){
sal = 255 ;
}
if (sal < 0){
sal = 0 ;
}
PWM1_set_duty(sal) ;
delay_ms(10) ;
INTCON.F2=0;
}
void main() {
sal = 0 ;
sp = 100 ;
my_P = 0 ;
I = 0 ;
my_D = 0 ;
pid = 0 ;
oldpid = 0 ;
Adcon1 = 0b00000001 ;
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_out(1,1,"Set Point:") ;
Lcd_out(2,1,"Temp:") ;
inttostr(sp,text3);
PWM1_Init(20000) ;
PWM1_set_duty(0) ;
PWM1_Start();
trisd = 0x07 ;
portc = 0 ;
OPTION_REG = 0b00000110;
INTCON = 0b10100000;
while (1){
while (pid > 0) {
if (Button(&PORTD, 0,50, 1)) {
pid = +1 ;
if (pid ==5) {
pid = 0 ;
}
}
if (Button(&PORTD, 1,50, 1)) {
if (pid == 1){
sp =+0.1 ;
if (sp > 150){
sp = 150 ;
}
}
if (pid ==2){
my_P=+0.1 ;
}
if (pid ==3){
I=+1 ;
}
if (pid ==4){
my_D=+1 ;
}
}
if (Button(&PORTD, 2,50, 1)) {
if (pid == 1){
sp =-0.1 ;
if (sp < 0){
sp = 0 ;
}
}
if (pid ==2){
my_p=-0.1 ;
if (P < 0){
my_P= 0 ;
}
}
if (pid ==3){
I=-1 ;
if (I < 0){
I= 0 ;
}
}
if(pid ==4){
my_D = -1 ;
if (D < 0){
my_D= 0 ;
}
}
}
intTostr(P,text) ;
intToStr(I,text1) ;
intToStr(D,text2) ;
intTostr(sp,text3) ;
if (oldpid > 0){
if (pid ==1) {
Lcd_out(1,12,text3) ;
delay_ms(5) ;
}
if ((pid >1)&&(pid < 5)){
Lcd_out(2,1,text) ;
Lcd_out(2,5,text1 ) ;
Lcd_out(2,12,text2 ) ;
delay_ms (5) ;
}
}
else{
if (pid == 1){
Lcd_cmd(_Lcd_clear) ;
Lcd_out(1,1,"Set Point:") ;
Lcd_out(1,11,text3) ;
delay_ms(5) ;
}
if ((pid >1)&&(pid <5)){
Lcd_cmd(_Lcd_clear) ;
Lcd_out(1,1,"P:") ;
Lcd_out(1,6,"I:") ;
Lcd_out(1,12,"D:") ;
Lcd_out(2,1,text) ;
Lcd_out(2,5,text1 ) ;
Lcd_out(2,12,text2 ) ;
delay_ms (5) ;
}
}
}
if (pid == 0) {
if (oldpid == 0){
Lcd_out(1,11,text3) ; //Indicacion del error actual
Lcd_out(2,13,text4) ;
delay_ms(5) ;
}
else{
Lcd_Cmd(_LCD_CLEAR);
Lcd_out(1,1,"Set Point:") ;
Lcd_out(2,1,"Temp:") ;
Lcd_out(1,11,text3) ; //Indicacion del error actual
Lcd_out(2,13,text4) ;
delay_ms(5) ;
}
}
oldpid = pid ;
}
} :o
Muchas gracias una vez mas. lo de los decrementos es por que no deseo q los valores sean negativos. Me gustaría que el valor de p se incremente de 0.0 hasta 100 en 0.1, debo usar float?. Alguna sugerencia para no tener el problema de del espacio en ram? Gracias una vez mas, voy a trabajar en la observaciones.