|
這個(gè)程序NB在哪兒呢,在用了自適應(yīng)模糊PID算法,不過(guò)主函數(shù)中我把整個(gè)結(jié)構(gòu)去除了,因?yàn)樽龅捻?xiàng)目屬于公司的,但是保留了所有功能函數(shù),可以直接使用功能。另外要說(shuō)一句,最好用14引腳的同款芯片來(lái)實(shí)現(xiàn)功能,因?yàn)閺?fù)用編程器引腳會(huì)經(jīng)常在寫入程序時(shí)報(bào)錯(cuò)。
單片機(jī)源程序:
- /* ----------------
- * VDD----------|1(VDD) (GND)8|------------GND
- * RED--------- |2(RA6) (PA0)7|------------GREEN
- * PWM3--------|3(PC3) (PA1)6|------------AD2
- * AD6-----------|4(PC2) (PC4)5|------------PWM4
- * ----------------
- */
- /*程序說(shuō)明:ADC轉(zhuǎn)換,通過(guò)ADC測(cè)量實(shí)現(xiàn)燈的轉(zhuǎn)變,通過(guò)ADC測(cè)量,對(duì)輸出mcu引腳頻率以及占空比進(jìn)行控制,從而達(dá)到控制輸出電流電壓*/
- /*timer0 用的是1:256分頻,timer0增1周期為32us,中斷一次耗時(shí)32us*255=8.16ms*/
- /*自設(shè)的pwm~pa6采用系統(tǒng)時(shí)鐘的80分頻,即一個(gè)pwm周期為0.125us*80=10us,由軟件方法實(shí)現(xiàn)設(shè)定,PA6的震動(dòng)頻率是80的整倍數(shù),占空比自調(diào)(必須由中斷計(jì)時(shí))*/
- /*為降低定時(shí)器中斷產(chǎn)生的難度,暫時(shí)將pwm周期設(shè)置為20us,由__delay_us()函數(shù)來(lái)完成。*/
- /* 注意:本程序所有的占空比是小數(shù)表示,不是用百分?jǐn)?shù)表示*/
- #include "syscfg.h"
- #include "MS83Fxx02.h"
- #include<stdio.h>
- #define _XTAL_FREQ 32000000 //內(nèi)部晶振頻率為16M
- //時(shí)鐘周期與機(jī)器周期為1:1,2T模式下,2個(gè)機(jī)器周期為一個(gè)指令周期,當(dāng)前系統(tǒng)指令周期為0.125us,
- //******************************
- #define red_on PA6=0;
- #define red_off PA6=1;
- #define green_on PA0=0;
- #define green_off PA0=1;
- //*****************************//
- #define shutled {red_off;green_off;} //所有的燈熄滅
- #define openled {red_on;green_on;} //所有燈打開(kāi)
- #define TMR0_VALUE_INIT 5;//1個(gè)timer0中斷是(255-5)*0.125us*256=8ms,1250個(gè)timer0中斷是1s
- #define delay_num 500; //大量延時(shí)用的默認(rèn)毫秒值
- unsigned int interrupt_time=0; //timer0中斷次數(shù)
- float adc_avg_num=0, VCC=0.0; //ADC轉(zhuǎn)換出來(lái)的數(shù)值
- /*-------------------模糊自適應(yīng)pid-------------------*/
- #define IS_Kp 1
- #define IS_Ki 2
- #define IS_Kd 3
- #define NL -3
- #define NM -2
- #define NS -1
- #define ZE 0
- #define PS 1
- #define PM 2
- #define PL 3
- static const float fuzzyRuleKp[7][7]={
- PL, PL, PM, PM, PS, PS, ZE,
- PL, PL, PM, PM, PS, ZE, ZE,
- PM, PM, PM, PS, ZE, NS, NM,
- PM, PS, PS, ZE, NS, NM, NM,
- PS, PS, ZE, NS, NS, NM, NM,
- ZE, ZE, NS, NM, NM, NM, NL,
- ZE, NS, NS, NM, NM, NL, NL
- };
- static const float fuzzyRuleKi[7][7]={
- NL, NL, NL, NM, NM, ZE, ZE,
- NL, NL, NM, NM, NS, ZE, ZE,
- NM, NM, NS, NS, ZE, PS, PS,
- NM, NS, NS, ZE, PS, PS, PM,
- NS, NS, ZE, PS, PS, PM, PM,
- ZE, ZE, PS, PM, PM, PL, PL,
- ZE, ZE, PS, PM, PL, PL, PL
- };
- static const float fuzzyRuleKd[7][7]={
- PS, PS, ZE, ZE, ZE, PL, PL,
- NS, NS, NS, NS, ZE, NS, PM,
- NL, NL, NM, NS, ZE, PS, PM,
- NL, NM, NM, NS, ZE, PS, PM,
- NL, NM, NS, NS, ZE, PS, PS,
- NM, NS, NS, NS, ZE, PS, PS,
- PS, ZE, ZE, ZE, ZE, PL, PL
- };
- typedef struct{
- float Kp;
- float Ki;
- float Kd;
- }PID;
- PID fuzzy(float e,float ec);//e 誤差,ec誤差變化率
- float speed_pid(float s_tar,float s_cur);//在目標(biāo)值會(huì)多次改變的情況下,建議在函數(shù)內(nèi)部初始pid參數(shù)而不是作為形參
- /*-------------------------------模糊自適應(yīng)pid結(jié)束---------------------------------*/
- void Delay_xms(unsigned int);//不精準(zhǔn)的毫秒延時(shí)
- void Delay_xus(unsigned int);//不精準(zhǔn)的微妙延時(shí)
- void colck_init(void);//所有時(shí)鐘初始化
- void interrupt_init(void);//中斷模塊初始化
- void io_init(void);//所有io初始化
- void adc_init(void);//adc模塊初始化
- void pwm_colck_init(void);//pwm初始化,IO端口將在set_pwm中設(shè)置,本函數(shù)主要初始化的是相關(guān)時(shí)鐘配置
- void shutdown_pwmall(void);//關(guān)閉所有PWM,同時(shí)使P1C和P1B引腳狀態(tài)固定pc4=1;pc3=0;
- unsigned int start_tran_adc(void);//官方給定的ADC程序
- unsigned int start_tran_adc2(void);//專門用來(lái)測(cè)量電流值大小
- unsigned int start_tran_adc_vcc(void);//用來(lái)測(cè)量VCC電壓
- void set_pwm_frequence(unsigned int, float);//設(shè)置PA6端口周期和占空比,調(diào)用_delay_us()函數(shù)完成,如果用定時(shí)器中斷,則基準(zhǔn)頻率可以最高提升到8M,
- void set_duty(float duty);//占空比操作封裝
- void set_cycle(unsigned int cycle);//周期操作封裝
- void set_pwm1(float duty,unsigned int cycle);//系統(tǒng)pc3引腳的pwm設(shè)置,//PWM周期 Tpwm=(PR2+1)*4*Tsys*預(yù)分頻
- void set_pwm2(float duty,unsigned int cycle);//系統(tǒng)pc4引腳的pwm設(shè)置
- void set_half_bridge(float duty,unsigned int cycle);//系統(tǒng)pc3,pc3組成的半橋pwm設(shè)置
- void controlpwm_calc(float expecte_volate,float expecte_current);//輸入預(yù)期電流電壓,該程序自動(dòng)調(diào)控,設(shè)置占空比和頻率
- void controlpwm_fruzy_pid(float expecte_voltage,float expecte_current);//自適應(yīng)模糊PID控制算法
- float avg_adc_value(unsigned int frequency);//平均多次采樣數(shù)據(jù),采樣次數(shù)由frequency 確定
- void act_led(float);//根據(jù)adc轉(zhuǎn)化的值處理燈閃爍狀態(tài)
- void breathe_led(unsigned int,float);//呼吸燈程序
- void system_check(void);//系統(tǒng)閃燈自檢
- /*********************************************main*****************************************************************************/
- int main()
- {
- colck_init();
- interrupt_init();
- io_init();
- adc_init();
- system_check();
- INTCON=0B10000000;
- // while(ADC_time>1250)//1250*8ms=10秒,每10秒啟動(dòng)一次檢測(cè)
- while (1)
- {
- {
- VCC=12*start_tran_adc_vcc()/1023;
- act_led(VCC);
- interrupt_time=0;
- }
- }
- }
- /*********************************************************************************************************************/
- void colck_init(void)//中心頻率16M,2T模式,看門狗禁用,定時(shí)器0中斷
- {
- //UCFG0
- ////////關(guān)鍵數(shù)據(jù)//////////////
- OPTION=0B00000111;//time0定時(shí)器,PORTA上拉使能,下降沿中斷,內(nèi)部時(shí)鐘,預(yù)分頻器給timer0使用,timer0 256分頻
- OSCCON=0B11110101;//低頻,256Khz,timer0每一指令周期加1,16M系統(tǒng)時(shí)鐘focs
- WDTCON=0B00010110;//看門狗時(shí)鐘源32khz,65565分頻,溢出周期為32khz/65535,軟件關(guān)閉看門狗(sbit<0>)
- ADCON1=0B01010000;//ADC選擇內(nèi)部時(shí)鐘16分頻(ADC采樣頻率為16M/16=1M)
- ////////關(guān)鍵數(shù)據(jù)//////////////*
- //T1CON=0B10000101;//time1定時(shí)器,高電平有效,1:1分頻,內(nèi)部時(shí)鐘
- //T2CON&=0B11111000;//time2定時(shí)器關(guān)閉
- /*全局中斷及time1中斷在此設(shè)置*/
- }
- void interrupt_init(void)//關(guān)閉比較器中斷,禁止ADC中斷
- {
- INTCON=0B10100000;//全局中斷使能,TIMER0中斷使能
- PIE1 = 0b00000000;
- PIE2 = 0b00000000;
- PIR1 = 0b00000000;
- PIR2 = 0b00000000;
- IOCA=0B00000000;
- }
- void io_init(void)
- {
- //GPIO 設(shè)置為輸出時(shí),弱上拉會(huì)自動(dòng)關(guān)斷,此點(diǎn)可作為測(cè)試關(guān)鍵點(diǎn)
- CMCON0=0B00000111;//關(guān)閉PA6引腳的比較模式
- MSCKCON=0B00000000;
- TRISA=0B00000000;//全部porta引腳配置為輸出
- TRISC=0B00000100;//除PC2配置為AN6輸入外,其他全部配置為輸出
- PORTA=0B00000000;//初始化porta端口,全部低電平
- PORTC=0B00000000;//初始化portc端口,全部低電平
- ANSEL=0B01000000;//除AN6通道為模擬輸入外,其他全部設(shè)置為數(shù)字IO
- WPUA=0B00000000;//全部弱上拉使能
- WPUC=0B00000000;//全部允許弱上拉
- WPD=0B00000000;//pc2,PA4,PC1,PC3都設(shè)置為下拉
- }
- void adc_init(void)
- {
- // ADCON0=0B10111000;//右對(duì)齊,內(nèi)部2v參考電壓,AN6模擬通道,轉(zhuǎn)換完成,禁止ADC開(kāi)始(sbit<0>)
- ADCON0=0B11011000;//參考3v,sbit<6:5> 10 3v,01 2v,00 VCC
- ADCON1=0B01100000;//ADC采樣時(shí)間初始化,初始化為系統(tǒng)時(shí)鐘的64分頻
- //ADCON0=0B10111100;//1/4VDD an7
- }
- float avg_adc_value(unsigned int frequency)//平均多次轉(zhuǎn)換結(jié)果,使轉(zhuǎn)換結(jié)果更穩(wěn)定
- {
- float temp=0,Value_tmp=0,Value_min=1000000,Value_max=-100000;
- if(0==frequency){frequency=1;}
- else if(frequency<10)
- {
- for(int T=0;T<frequency;T++)
- {
- temp=(6*(start_tran_adc())/1023);//3V參考電壓
- Value_tmp+=temp;
- }
- Value_tmp=(Value_tmp/frequency);
- }
- else
- {for(int T=0;T<frequency;T++)
- {
- temp=(6*(start_tran_adc())/1023);
- Value_tmp+=temp;
- if(temp>Value_max) {Value_max=temp;}
- if(temp<Value_min) {Value_min=temp;}
- }
- Value_tmp=Value_tmp-(Value_max+Value_min);
- Value_tmp=(Value_tmp/(frequency-2));
- }
- return Value_tmp;
- }
- void act_led(float tmp)//根據(jù)adc轉(zhuǎn)換的平均結(jié)果控制閃燈
- {
- if(tmp>=4.16)//大于4.16v充滿,綠燈常亮
- {
- red_off;
- green_on;
- }
- else if(tmp>4.0&&tmp<4.08)//3.0v~4.08v補(bǔ)足充電,綠燈閃
- {
- red_off;
- green_on;__delay_ms(200);green_off;__delay_ms(200);
- }
- else if(tmp=4.0){breathe_led();}
- else if(tmp>=3.0&&tmp<4.0)//3.0v~4.0v快充階段,紅綠燈交替閃
- {
- red_off;green_on;
- __delay_ms(100);
- red_on;green_off;
- __delay_ms(100);
- }
- else if(tmp>=0.5&&tmp<3.0)//預(yù)充階段,紅燈閃爍
- {
- green_off;
- red_on;__delay_ms(200);
- red_off;__delay_ms(200);
- }
- else //故障,紅燈常亮
- {
- green_off;
- red_on;
- }
- }
- void interrupt ISR(void){
- if(T0IE&&T0IF){
- T0IF=0;
- TMR0=TMR0_VALUE_INIT;//賦初值
- interrupt_time++;//系統(tǒng)指令周期0.125us*256分頻* TMR0是8位256=0.125*256*256=8.192ms
- }
- }
- /////////////////////////////////////////////
- void set_pwm_frequence(unsigned int circle,float Duty)//分辨率1us,輸入單位100us
- {
- PA6=1;
- Delay_xus(circle*Duty*100);
- PA6=0;
- Delay_xus(circle*(1-Duty)*100);
- }
- void breathe_led(unsigned int freqs,float dutys)
- {
- freqs=100000/freqs;
- green_on;red_off;
- Delay_xus((freqs*dutys));
- green_off;red_on;
- Delay_xus((freqs*(1-dutys)));
- }
- ///////////////////////////////////////////////breathe of led test function ///////////
- void Delay_xms(unsigned int integerA){
- for(unsigned int i=0;i<integerA;i++){
- __delay_ms(1);
- }
- }
- void Delay_xus(unsigned int integerB){
- for (unsigned int x=0;x<integerB;x++){
- __delay_us(1);
- }
- }
- PID fuzzy(float e,float ec)//e 誤差,ec誤差變化率
- {
- float etemp,ectemp;
- float eLefttemp,ecLefttemp;
- float eRighttemp ,ecRighttemp;
- int eLeftIndex,ecLeftIndex;
- int eRightIndex,ecRightIndex;
- PID fuzzy_PID;
- etemp = e > 3.0 ? 0.0 : (e < - 3.0 ? 0.0 : (e >= 0.0 ? (e >= 2.0 ? 2.5: (e >= 1.0 ? 1.5 : 0.5)) : (e >= -1.0 ? -0.5 : (e >= -2.0 ? -1.5 : (e >= -3.0 ? -2.5 : 0.0) ))));
- eLeftIndex = (int)e;
- eRightIndex = eLeftIndex;
- eLeftIndex = (int)((etemp-0.5) + 3); //[-3,3] -> [0,6]
- eRightIndex = (int)((etemp+0.5) + 3);
- eLefttemp =etemp == 0.0 ? 0.0:((etemp+0.5)-e);
- eRighttemp=etemp == 0.0 ? 0.0:( e-(etemp-0.5));
- ectemp = ec > 3.0 ? 0.0 : (ec < - 3.0 ? 0.0 : (ec >= 0.0 ? (ec >= 2.0 ? 2.5: (ec >= 1.0 ? 1.5 : 0.5)) : (ec >= -1.0 ? -0.5 : (ec >= -2.0 ? -1.5 : (ec >= -3.0 ? -2.5 : 0.0) ))));
- ecLeftIndex = (int)((ectemp-0.5) + 3); //[-3,3] -> [0,6]
- ecRightIndex = (int)((ectemp+0.5) + 3);
- ecLefttemp =ectemp == 0.0 ? 0.0:((ectemp+0.5)-ec);
- ecRighttemp=ectemp == 0.0 ? 0.0:( ec-(ectemp-0.5));
- ///////*************************************反模糊*************************************//////
- fuzzy_PID.Kp = (eLefttemp * ecLefttemp * fuzzyRuleKp[ecLeftIndex][eLeftIndex]
- + eLefttemp * ecRighttemp * fuzzyRuleKp[ecRightIndex][eLeftIndex]
- + eRighttemp * ecLefttemp * fuzzyRuleKp[ecLeftIndex][eRightIndex]
- + eRighttemp * ecRighttemp * fuzzyRuleKp[ecRightIndex][eRightIndex]);
- fuzzy_PID.Ki = (eLefttemp * ecLefttemp * fuzzyRuleKi[ecLeftIndex][eLeftIndex]
- + eLefttemp * ecRighttemp * fuzzyRuleKi[ecRightIndex][eLeftIndex]
- + eRighttemp * ecLefttemp * fuzzyRuleKi[ecLeftIndex][eRightIndex]
- + eRighttemp * ecRighttemp * fuzzyRuleKi[ecRightIndex][eRightIndex]);
- fuzzy_PID.Kd = (eLefttemp * ecLefttemp * fuzzyRuleKd[ecLeftIndex][eLeftIndex]
- + eLefttemp * ecRighttemp * fuzzyRuleKd[ecRightIndex][eLeftIndex]
- + eRighttemp * ecLefttemp * fuzzyRuleKd[ecLeftIndex][eRightIndex]
- + eRighttemp * ecRighttemp * fuzzyRuleKd[ecRightIndex][eRightIndex]);
- return fuzzy_PID;
- }
- float speed_pid(float s_tar,float s_cur)//在目標(biāo)值會(huì)多次改變的情況下,建議在函數(shù)內(nèi)部初始pid參數(shù)而不是作為形參
- {
- float tar = 0,cur = 0; //目標(biāo)值 , 實(shí)際值
- tar=s_tar;s_cur=cur;
- static PID pid= {1, 0, 0}; //賦予初值kp,ki,kd
- static float sumE = 0; //累加偏差
- static float lastE = 0;
- PID OUT = {0, 0, 0};
- float e = -1,ec = -2.6;
- e = tar - cur; //目標(biāo)值 - 實(shí)際值
- ec = e - lastE; //誤差變化率
- sumE += e;
- lastE = e;
- OUT = fuzzy(e, ec); //模糊控制調(diào)整 kp,ki,kd
- return (pid.Kp+OUT.Kp)*e + (pid.Kd+OUT.Kd)*ec + (pid.Ki+OUT.Ki)*sumE;
- }
- /////////////////////////////
- void pwm_colck_init(void)
- {
- TMR2IF = 0; //中斷標(biāo)志位清零
- T2CON = 0B00000100;//timer2預(yù)分頻比<1,0> 00 1:1 與系統(tǒng)時(shí)鐘1:1
- }
- void shutdown_pwmall(void)
- {
- ECCPAS=0B00000001;
- }
- unsigned int start_tran_adc(void)
- {
- unsigned int TempADCBuffer=0;
- TRISC=0B00000100;//除PC2配置為AN6輸入外,其他全部配置為輸出
- ANSEL=0B01000000;//配置an6信號(hào)為模擬信號(hào)
- ADCON0 = 0b11011001; //右對(duì)齊,ADC使能,AN6通道,3V參考電壓
- __delay_us(20);
- GO_DONE=1; //開(kāi)始轉(zhuǎn)換
- while(GO_DONE==1) ; //等待轉(zhuǎn)換完成
- TempADCBuffer = ADRESH;
- TempADCBuffer = (TempADCBuffer<<8)|ADRESL;
- asm("nop");
- ADON = 0;
- ANSEL=0B00000000;//關(guān)閉模擬輸入an6
- TRISC=0B00000000;//關(guān)閉AN6,防止影響另一路adc
- return(TempADCBuffer);
- }
- unsigned int start_tran_adc2(void)
- {
- unsigned int TempADCBuffer=0;
- TRISA=0B00000010;//除PA1配置為AN1輸入外,其他全部配置為輸出
- ANSEL=0B00000010;
- ADCON0= 0b10100101; //右對(duì)齊,ADC使能,選擇AN1通道,2V參考電壓,AN1通道,pa1口
- __delay_us(20);
- GO_DONE=1; //開(kāi)始轉(zhuǎn)換
- while(GO_DONE==1) ; //等待轉(zhuǎn)換完成
- TempADCBuffer = ADRESH;
- TempADCBuffer = (TempADCBuffer<<8)|ADRESL;
- asm("nop");
- ADON = 0;
- ANSEL=0B00000000;//關(guān)閉模擬輸入an1
- TRISA=0B00000000;//關(guān)閉AN1,防止影響另一路adc
- return(TempADCBuffer);
- }
- unsigned int start_tran_adc_vcc(void)
- {
- unsigned int TempADCBuffer=0;
- TRISA=0B00000000;//全部配置為輸出
- ANSEL=0B10000000;//配置an7信號(hào)為模擬信號(hào)
- //ADCON0= 0b10111101; //右對(duì)齊,ADC使能,采集內(nèi)部1/4VCC通道信號(hào),2V參考
- ADCON0= 0b11011101; //3v參考
- __delay_us(20);
- GO_DONE=1; //開(kāi)始轉(zhuǎn)換
- while(GO_DONE==1) ; //等待轉(zhuǎn)換完成
- TempADCBuffer = ADRESH;
- TempADCBuffer = (TempADCBuffer<<8)|ADRESL;
- asm("nop");
- ADON = 0;
- ANSEL=0B00000000;//關(guān)閉模擬輸入an1
- TRISA=0B00000000;//關(guān)閉AN1,防止影響另一路adc
- return(TempADCBuffer);
- }
- void set_duty(float duty)
- {
- unsigned int t_duty=0;
- /********************************占空比操作******************************/
- t_duty=duty*(4*(PR2+1));//占空比是10位,高8位放在CCR1L,低2位被放在CCP1CON 的DC1B<1:0>中,所以為了封裝進(jìn)行了以下一系列操作
- CCPR1L=t_duty>>2;
- t_duty=t_duty-CCPR1L;
- t_duty=t_duty<<4;
- CCP1CON=t_duty|CCP1CON;
- /********************************占空比操作完畢******************************/
- }
- void set_cycle(unsigned int cycle)
- { unsigned int timer2_prescaler=0;
- /********************************PWM周期操作******************************/
- timer2_prescaler=T2CON;
- timer2_prescaler&=0b01111000;
- timer2_prescaler=timer2_prescaler>>3;
- PR2 = (cycle*4/timer2_prescaler-1);
- /********************************PWM周期操作完畢******************************/
- }
- void set_pwm1(float duty,unsigned int cycle) //Tpwm=(PR2+1)*4*Tsys*預(yù)分頻 ,pwm周期cycle單位是1us
- {
- TRISC3 = 1;
- set_duty(duty);set_cycle(cycle);
- CCP1CON|=0B00001101;
- CCP1CON&=0b00111101; //PWM模式,單輸出,P1C高電平有效,P1B低電平有效
- TMR2IF = 0;
- T2CON = 0B00000100;
- while(TMR2IF==0) ;
- TRISC3 = 0;
- }
- void set_pwm2(float duty,unsigned int cycle) //Tpwm=(PR2+1)*4*Tsys*預(yù)分頻 ,pwm周期cycle單位是1us
- {
- TRISC4 = 1;
- set_duty(duty);set_cycle(cycle);
- CCP1CON|=0B00001101;
- CCP1CON&=0b00111101; //PWM模式,單輸出,P1C高電平有效,P1B低電平有效
- TMR2IF = 0;
- T2CON = 0B00000100;
- while(TMR2IF==0) ;
- TRISC4= 0;
- }
- void set_half_bridge(float duty,unsigned int cycle)
- {
- TRISC4 = 1;TRISC3=1;
- set_duty(duty);set_cycle(cycle);
- CCP1CON|=0B00001101;
- CCP1CON&=0b00111101; //PWM模式,單輸出,P1C高電平有效,P1B低電平有效
- TMR2IF = 0;
- T2CON = 0B00000100;
- while(TMR2IF==0) ;
- TRISC4= 0;TRISC3=0;
- }
- /*以下程序?yàn)榭刂扑惴?///控制算法1
- void controlpwm_calc(float expecte_voltage,float expecte_current)//根據(jù)公式計(jì)算的算法1
- {
- float duty_temp=0.0 , cycle_temp=0.0;
- VCC=8*start_tran_adc_vcc()*1023;//10bit adc,2V參考電壓,采集1/4 vcc電壓
- duty_temp=expecte_voltage/VCC;
- cycle_temp=1.2*expecte_current/(VCC*(1-duty_temp));
- set_half_bridge(duty_temp,cycle_temp);
- }
- void controlpwm_fruzy_pid(float expecte_voltage,float expecte_current)//模糊自適應(yīng)pid
- {
- float duty_temp=0.0 ,pid_temp=0.0, s_cur=0,s_vol=0;
- unsigned int cycle_temp=0;
- VCC=8*start_tran_adc_vcc()/1023;
- s_vol=9*start_tran_adc()/1023;
- s_cur=4*start_tran_adc2()/5115;
- pid_temp=speed_pid(expecte_voltage,s_vol);
- duty_temp=pid_temp/VCC;//s_vol 是調(diào)整輸出電壓的數(shù)據(jù)
- cycle_temp=1.2*VCC*speed_pid( expecte_current,s_cur)/(1-duty_temp);//s_cur 是調(diào)整電流的數(shù)據(jù),由另一路ADC完成
- set_half_bridge(duty_temp,cycle_temp);
- }
- void system_check(void)
- {
- for(char i=0;i<3;i++)
- {
- red_on;green_on;Delay_xms(i*100);
- green_off;red_off;Delay_xms((4-i)*100);
- }
- }
復(fù)制代碼
|
評(píng)分
-
查看全部評(píng)分
|