Code: Select all
signed int pals=0;
unsigned int kod=0;
int bb=0;
float Kp =3 , Ki=500 , Kd=0.207;
float Setpoint=0;
float ut=0;
float InputValue=0;
//
float error,up,ui=0.0,ui_=0.0,error_=0.0,ud,d_error=0;
void InitTimer0(){
OPTION_REG = 0x87;
TMR0 = 2;
INTCON = 0xA0;
}
void Interrupt(){
if (TMR0IF_bit){
TMR0IF_bit = 0;
TMR0 = 2;
bb++;
if(bb==16){
bb=0;
}
}
}
void interrupt1()
{
if(INTF_bit){
if (PORTB.RB1==PORTB.RB0){
pals++;
}
INTEDG_bit=~INTEDG_bit;
INTF_bit=0;
}
if (bb==16){
pals=0;
}
}
void main(){
trisb=0b11100111;
trisc=0x00;
portc=0;
porta=0;
TMR0IF_bit = 1;
pwm1_init(30000);
pwm1_set_duty(0);
pwm1_start();
INTCON=0B01000000;
INTF_bit=0;
INTEDG_bit=0;
INTE_bit=1;
GIE_bit=1;
while(1){
//
const float MinOutput=-255;
const float MaxOutput=255;
if (bb==15){
InputValue = (pals/24)*4000*60;
bb=0;
}
Setpoint=100;
error=setpoint-inputvalue;
//oransal deger
up = error * Kp ;
//integral degerinin hesabi
ui=ui_ + (ki*error);
// türevsel deger
d_error=error-error_;
ud=kd*(d_error);
// toplam hesabi
ut=up+ui+ud;
// cikisi min ve max da sinirla
if (ut < MinOutput)
ut = MinOutput;
if (ut > MaxOutput)
ut = MaxOutput;
ui_=ui;
error_=error;
//
pwm1_set_duty(ut);
}
}