int1 a;
float Vref,Vout;
float er=0,last_er1=0,last_er2=0;
float Vc=0,Vc1=0,Vc2=0;
unsigned int value_duty,Vadc;
unsigned char str[20];
unsigned char count=0;
void PID_isr(void)
{
Vadc= read_adc();
Vout =(float)(0.6* Vadc *5/1023);
er= Vref -Vout;
Vc= m1 *Vc1 +m2 *Vc2 + e0 *er +e1 *last_er1 + e2*last_er2;
value_duty=(int) (Vc*248) ;
if(value_duty >=248) value_duty=248;
if(value_duty <=0) value_duty=1;
set_pwm1_duty(value_duty);
Vc2=Vc1;
Vc1=Vc;
last_er2=last_er1;
last_er1=er;
}
void ngat_timer0(void)
{
setup_timer_1(T1_DISABLED);
count++;
a=1;
PID_isr();
set_timer1(30000);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
}
void main()
{
set_tris_a(0x01);
LCD_Init();
setup_timer_1(T1_INTERNAL |T1_DIV_BY_8);
set_timer1(30000);
setup_ccp1(CCP_PWM);
set_pwm1_duty(0);
setup_timer_2(T2_DIV_BY_4,61,1);
setup_adc(ADC_CLOCK_DIV_8);
setup_adc_ports(AN0);
set_adc_channel(0);
delay_us(10);
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);
while(1)
{
if(a)
{
if(count >=10)
{ a=0;
count=0;
LCD_Gotoxy(0,1);
sprintf(str,"gia tri dien ap chuan :%2f",Vref);
LCD_Puts(str);
delay_ms(10);
LCD_Gotoxy(0,1);
sprintf(str,"gia tri dien ap :%2f",Vout);
LCD_Puts(str);
}
}
}
}