<acronym id="xonnx"></acronym>
      <td id="xonnx"></td>
    1. <pre id="xonnx"></pre>

      1. 專注電子技術學習與研究
        當前位置:單片機教程網 >> MCU設計實例 >> 瀏覽文章

        單片機PID算法實現

        作者:huqin   來源:本站原創   點擊數:  更新時間:2014年04月03日   【字體:

        initial interrupter                
        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);

        關閉窗口

        相關文章

        欧美性色欧美精品视频,99热这里只有精品mp4,日韩高清亚洲日韩精品一区二区,2020国自产拍精品高潮