/*
trinh dich MikroC
*/
#include"built_in.h"
//Khai bao cac bien
unsigned short cnt;
unsigned short T_dat;
unsigned short T_do;
unsigned short Hang_chuc;
unsigned short Hang_dv;
unsigned short t2;
unsigned int temp;
unsigned int T;
char *txt;
float t1;
//Cac tham so cua bo dieu khien
float a0;
float a1;
float a2;
float b1;
float b2;
//Cac bien qua trinh
float ek;
float rk;
float rk_1;
float rk_2;
float uk;
float M1;
float M2;
//Ham tinh dau ra
float Caculate_PID(){
ek = T_dat - T_do;
rk = ek + M1;
uk = a0*rk + M2;
rk_2 = rk_1;
rk_1 = rk;
M1 = (-1)*(b1*rk_1 + b2*rk_2);
M2 = a1*rk_1 + a2*rk_2;
t2=0;
return uk;
}
//Ham phuc vu ngat timer1
void interrupt() {
cnt++ ;
PIR1.TMR1IF = 0; // clear TMR1IF
T = T + Adc_Read(0);
if (cnt==20) {
temp = T/20;
T_do = temp*100/614;
T = 0;
temp = 0;
cnt = 0;
t2 = 1;
}
//Nap lai so dem
TMR1H = 0x3C;
TMR1L = 0xB0;
}//~
void init_Timer1() {
//dat thoi gian tre cho timer
TMR1H = 0x3C;
TMR1L = 0xB0;
TRISB = 0;
T1CON = 1;
PIR1.TMR1IF = 0; // clear TMR1IF
PIE1 = 1; // enable interrupts
}
void init_LCD(){
Lcd8_Config(&PORTD,&PORTB,0,2,1,7,6,5,4,3,2,1,0);
txt = "T_dat:";
Lcd8_Cmd(LCD_CURSOR_OFF); // Turn off cursor
Lcd8_Out(1, 1, txt); // Print text on LCD
txt = "T_do:";
Lcd8_Out(2,1,txt);
}
void update_LCD(){
//update T_dat
Hang_chuc = T_dat/10;
Lcd8_Chr(1,8,48+Hang_chuc);
Hang_dv = T_dat - Hang_chuc*10;
Lcd8_Chr(1,9,48+Hang_dv);
//update T_do
Hang_chuc = T_do/10;
Lcd8_Chr(2,8,48+Hang_chuc);
Hang_dv = T_do - Hang_chuc*10;
Lcd8_Chr(2,9,48+Hang_dv);
}
void main() {
ADCON1 = 0x80; // Configure analog inputs and Vref
TRISA = 0xFF; // PORTA is input
INTCON = 0xC0;
PORTB = 0xF0;
//Khoi tao gia tri ban dau cho cac bien
cnt=0;
T_dat=60;
//khoi tao timer1
init_Timer1();
//Khoi tao LCD
init_LCD();
//Khoi tao PWM
Pwm_Init(1000); // Initialize PWM module
Pwm_Start(); // Start PWM
while (1) {
update_LCD();
if (t2) {
t1 = Caculate_PID()/220;
};
Pwm_Change_Duty(t1*255); // set new duty ratio,
};
}//~!
Các bac thông dịch hộ em! Đang cần gấp
trinh dich MikroC
*/
#include"built_in.h"
//Khai bao cac bien
unsigned short cnt;
unsigned short T_dat;
unsigned short T_do;
unsigned short Hang_chuc;
unsigned short Hang_dv;
unsigned short t2;
unsigned int temp;
unsigned int T;
char *txt;
float t1;
//Cac tham so cua bo dieu khien
float a0;
float a1;
float a2;
float b1;
float b2;
//Cac bien qua trinh
float ek;
float rk;
float rk_1;
float rk_2;
float uk;
float M1;
float M2;
//Ham tinh dau ra
float Caculate_PID(){
ek = T_dat - T_do;
rk = ek + M1;
uk = a0*rk + M2;
rk_2 = rk_1;
rk_1 = rk;
M1 = (-1)*(b1*rk_1 + b2*rk_2);
M2 = a1*rk_1 + a2*rk_2;
t2=0;
return uk;
}
//Ham phuc vu ngat timer1
void interrupt() {
cnt++ ;
PIR1.TMR1IF = 0; // clear TMR1IF
T = T + Adc_Read(0);
if (cnt==20) {
temp = T/20;
T_do = temp*100/614;
T = 0;
temp = 0;
cnt = 0;
t2 = 1;
}
//Nap lai so dem
TMR1H = 0x3C;
TMR1L = 0xB0;
}//~
void init_Timer1() {
//dat thoi gian tre cho timer
TMR1H = 0x3C;
TMR1L = 0xB0;
TRISB = 0;
T1CON = 1;
PIR1.TMR1IF = 0; // clear TMR1IF
PIE1 = 1; // enable interrupts
}
void init_LCD(){
Lcd8_Config(&PORTD,&PORTB,0,2,1,7,6,5,4,3,2,1,0);
txt = "T_dat:";
Lcd8_Cmd(LCD_CURSOR_OFF); // Turn off cursor
Lcd8_Out(1, 1, txt); // Print text on LCD
txt = "T_do:";
Lcd8_Out(2,1,txt);
}
void update_LCD(){
//update T_dat
Hang_chuc = T_dat/10;
Lcd8_Chr(1,8,48+Hang_chuc);
Hang_dv = T_dat - Hang_chuc*10;
Lcd8_Chr(1,9,48+Hang_dv);
//update T_do
Hang_chuc = T_do/10;
Lcd8_Chr(2,8,48+Hang_chuc);
Hang_dv = T_do - Hang_chuc*10;
Lcd8_Chr(2,9,48+Hang_dv);
}
void main() {
ADCON1 = 0x80; // Configure analog inputs and Vref
TRISA = 0xFF; // PORTA is input
INTCON = 0xC0;
PORTB = 0xF0;
//Khoi tao gia tri ban dau cho cac bien
cnt=0;
T_dat=60;
//khoi tao timer1
init_Timer1();
//Khoi tao LCD
init_LCD();
//Khoi tao PWM
Pwm_Init(1000); // Initialize PWM module
Pwm_Start(); // Start PWM
while (1) {
update_LCD();
if (t2) {
t1 = Caculate_PID()/220;
};
Pwm_Change_Duty(t1*255); // set new duty ratio,
};
}//~!
Các bac thông dịch hộ em! Đang cần gấp
Comment