sao mình làm mô phỏng thì nó chạy êm ru mà lúc gắn vào mạch là nó tự chạy max tốc độ rồi ngưng rồi lại lặp lại . bác nào biết chỉ em với
code:
#if defined(__PCM__)
#include <16F877.h>
#include<lib_lcd_4bit.c>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#endif
#byte portc=0x07
#bit in=portc.1
double speed;
int timer1;
double count;
int8 u,ng,ch,tr,dv;
int32 s_xung,s_vong;
#INT_EXT
void InterruptExternal(){
speed=speed+1;
s_xung++;
}
#INT_TIMER1
void InterruptTimer1(){
count=count+1;
timer1=timer1+1;
s_vong=3*s_xung;
s_xung=0;
set_TIMER1(0x0BDC); //Nap lai gia tri cho Timer 1
}
void chuyen()
{
dv=s_vong%10;
ng=s_vong/1000;
tr=(s_vong/100)%10;
ch=(s_vong%100)/10;
}
void main() {
lcd_init();
double uk;double uk_1;
double ek;
double ek_1;
double ek_2;
double Kp;
double Ki;
double Kd;
double T;
double ref;
int8 u;
speed=0;
T=0.3;
ref=400;
uk=0;uk_1=0;ek=0;ek_1=0;ek_2=0;
Kp=10;Ki=8;Kd=0.50;
count=0;
set_tris_d(0); //Thiet lap Port D o che do OutPut
set_tris_c(0);
enable_interrupts(INT_EXT); //Cho phep ngat NGOAI
ext_int_edge(L_TO_H); //Ngat canh LEN
setup_timer_2(t2_div_by_16,255,1);
setup_ccp1(ccp_pwm);
set_pwm1_duty(77);
enable_interrupts(INT_TIMER1);//Cho phep ngat TIMER 1
setup_TIMER_1(T1_INTERNAL | T1_DIV_BY_8);
set_TIMER1(0x0BDC); //Ngat se tran sau 0.1giay
timer1=0;
enable_interrupts(GLOBAL); //Cho phep ngat TOAN CUC
while(1){
if(timer1==3){
in=0;
disable_interrupts(INT_EXT); //Khong cho phep ngat NGOAI
disable_interrupts(INT_TIMER1);//Khong cho phep ngat TIMER 1
ek=ref-speed;
uk=uk_1+(Kp+Ki*T/2+Kd/T)*ek;
uk=uk-(Kp+2*Kd/T)*ek_1;
uk=uk+(Kd/T)*ek_2;
if (uk>255){
uk=255;
}
if (uk<0){
uk=0;
}
uk_1=uk;
ek_2=ek_1;
ek_1=ek;
u=uk;
set_pwm1_duty(u);
lcd_putcmd(line_1);
chuyen();
delay_us(10);
printf(lcd_putchar,"speed=%u%u%u%u v/p ",ng,tr,ch,dv);
speed=0;
enable_interrupts(INT_EXT); //Cho phep ngat NGOAI
ext_int_edge(L_TO_H); //Ngat canh LEN
enable_interrupts(INT_TIMER1);//Cho phep ngat TIMER 1
setup_TIMER_1(T1_INTERNAL | T1_DIV_BY_8);
set_TIMER1(0x0BDC);
timer1=0;
}
}
}
code:
#if defined(__PCM__)
#include <16F877.h>
#include<lib_lcd_4bit.c>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#endif
#byte portc=0x07
#bit in=portc.1
double speed;
int timer1;
double count;
int8 u,ng,ch,tr,dv;
int32 s_xung,s_vong;
#INT_EXT
void InterruptExternal(){
speed=speed+1;
s_xung++;
}
#INT_TIMER1
void InterruptTimer1(){
count=count+1;
timer1=timer1+1;
s_vong=3*s_xung;
s_xung=0;
set_TIMER1(0x0BDC); //Nap lai gia tri cho Timer 1
}
void chuyen()
{
dv=s_vong%10;
ng=s_vong/1000;
tr=(s_vong/100)%10;
ch=(s_vong%100)/10;
}
void main() {
lcd_init();
double uk;double uk_1;
double ek;
double ek_1;
double ek_2;
double Kp;
double Ki;
double Kd;
double T;
double ref;
int8 u;
speed=0;
T=0.3;
ref=400;
uk=0;uk_1=0;ek=0;ek_1=0;ek_2=0;
Kp=10;Ki=8;Kd=0.50;
count=0;
set_tris_d(0); //Thiet lap Port D o che do OutPut
set_tris_c(0);
enable_interrupts(INT_EXT); //Cho phep ngat NGOAI
ext_int_edge(L_TO_H); //Ngat canh LEN
setup_timer_2(t2_div_by_16,255,1);
setup_ccp1(ccp_pwm);
set_pwm1_duty(77);
enable_interrupts(INT_TIMER1);//Cho phep ngat TIMER 1
setup_TIMER_1(T1_INTERNAL | T1_DIV_BY_8);
set_TIMER1(0x0BDC); //Ngat se tran sau 0.1giay
timer1=0;
enable_interrupts(GLOBAL); //Cho phep ngat TOAN CUC
while(1){
if(timer1==3){
in=0;
disable_interrupts(INT_EXT); //Khong cho phep ngat NGOAI
disable_interrupts(INT_TIMER1);//Khong cho phep ngat TIMER 1
ek=ref-speed;
uk=uk_1+(Kp+Ki*T/2+Kd/T)*ek;
uk=uk-(Kp+2*Kd/T)*ek_1;
uk=uk+(Kd/T)*ek_2;
if (uk>255){
uk=255;
}
if (uk<0){
uk=0;
}
uk_1=uk;
ek_2=ek_1;
ek_1=ek;
u=uk;
set_pwm1_duty(u);
lcd_putcmd(line_1);
chuyen();
delay_us(10);
printf(lcd_putchar,"speed=%u%u%u%u v/p ",ng,tr,ch,dv);
speed=0;
enable_interrupts(INT_EXT); //Cho phep ngat NGOAI
ext_int_edge(L_TO_H); //Ngat canh LEN
enable_interrupts(INT_TIMER1);//Cho phep ngat TIMER 1
setup_TIMER_1(T1_INTERNAL | T1_DIV_BY_8);
set_TIMER1(0x0BDC);
timer1=0;
}
}
}
Comment