void init_interrupter(void)
{
TMOD=0x21; /* 设置计时器0工作于模式1,设置计时器1工作于模式2*/
TL0=0x00; /* T0=0000定时时间为71.1ms;71.1ms*15=1.066s*/
TH0=0xdc; /*T0=DC00定时时间为10ms;10ms*100=1s*/ /*T0=FC66定时时间为1ms;10ms*1000=1s*/
TL1=0xfd; /* 设置串口通信速率9600bps*/
TH1=0xfd;
PCON=0x00; /* SMOD=0, 速率不倍增*/
SCON=0x50; /* 8位数据通信,串行通信方式1,允许单片机接收数据*/
IP=0x10; /*serial com is preferential*/
IE=0x92; /* 定时器0, 串口中断允许;定时器1中断禁止*/
rs485_receive=0;
rs485_transmitte=0;
TR0=1; /* 启动定时器0*/
TR1=1; /* 启动定时器1*/
}
void timer0_server(void) interrupt 1 using 1 /**/
{
TL0=0x00; TH0=0xdc;/*T0=DC00 timing interval is 10ms;10ms*100=1s*/ /*T0=FC66 timing interval is 1ms;10ms*1000=1s*/
if (flag_serial==1)
{
timer0_counter_3++;
if (timer0_counter_3>11)
{
timer0_counter_3=0;
flag_serial=0;
pointer_serial_com_data=serial_com_data;
counter_serial_com_data=0;
}
}
dog=!dog;/*Timer0 is full(10ms), feed dog*/
if (timing_interval==0) {timing_interval=1;} /*timing_interval is lost then set it to 1 second*/
timer0_counter_1++;/*timer0_counter_1 is software timer. when timer0 interrupt is full, it increase automatically*/
if ((unsigned char) (timer0_counter_1/100)==timing_interval) /*timing_interval arrives*/
{
out_flag=1;/*indexing 占空比 high level begin*/
/*-scan 0809 to get current equipment's temperature-*/
scan_current_Temperature();
origina_address=0x82;
display1_Temperature(current_Temperature,origina_address);
/*-calculate out_value-*/
PID_algorithm_function(PID_mode,PP,II,DD,BB,current_Temperature,seted_temperature);
//out_value=0.5;
if (out_value>0.0) /*out_value=0.0, then 占空比 is zero*/
{
control_0=1;
ledctrl_address=0x8c;
leddata_address=0xff;
}
//reset timer0_counter_1 and timer0_counter_2,
timer0_counter_1=0; //indexing timing_interval's timing
timer0_counter_2=0; //indexing 占空比's timing
}
if(out_flag==1)
{
timer0_counter_2++;
if (out_value<1.0)
{
if( (unsigned char) (timer0_counter_2/out_value/100) >= timing_interval)
{
timer0_counter_2=0;
control_0=0;
ledctrl_address=0x8c;
leddata_address=0x00;
out_flag=0;
}
}
}
}
float PID_algorithm_function(uchar PID_mode_2,float P_2,I_2,D_2,B_2,current_Temperature_2,seted_temperature_2)
{
float idata delta;
switch (PID_mode_2)
{
case 1: break;/*PID mode*/
case 2: D_2=0; break;/*PI mode*/
case 3: I_2=0; break;/*PD mode*/
case 4: I_2=0; D_2=0; break;/*P mode*/
}
if (PID_mode_2<5)/*PID algorithm*/
ek=(seted_temperature_2-current_Temperature_2)/99.9;
delta=P_2*(ek-ek1)+I_2*ek+D_2*(ek-2.0*ek1+ek2);
out_value=out_value+delta;
ek2=ek1;
ek1=ek;
if (out_value>1.0)
{
out_value=1;
}
else if (out_value<=0)
{
out_value=0.0;
}
if (PID_mode_2==5) /*BB algorithm*/
{
if (current_Temperature_2-seted_temperature_2>=B_2) {out_value=0.0;}
if (seted_temperature_2-current_Temperature_2>=B_2) {out_value=1.0;}
}
return (out_value);
上一篇:PID算法原理及调整规律
下一篇:USBASP 熔丝位锁死后的设置方法
推荐阅读最新更新时间:2024-03-16 13:54