這個程序NB在哪兒呢,在用了自適應模糊PID算法,不過主函數中我把整個結構去除了,因為做的項目屬于公司的,但是保留了所有功能函數,可以直接使用功能。另外要說一句,最好用14引腳的同款芯片來實現功能,因為復用編程器引腳會經常在寫入程序時報錯。
單片機源程序:
- /* ----------------
- * VDD----------|1(VDD) (GND)8|------------GND
- * RED--------- |2(RA6) (PA0)7|------------GREEN
- * PWM3--------|3(PC3) (PA1)6|------------AD2
- * AD6-----------|4(PC2) (PC4)5|------------PWM4
- * ----------------
- */
- /*程序說明:ADC轉換,通過ADC測量實現燈的轉變,通過ADC測量,對輸出mcu引腳頻率以及占空比進行控制,從而達到控制輸出電流電壓*/
- /*timer0 用的是1:256分頻,timer0增1周期為32us,中斷一次耗時32us*255=8.16ms*/
- /*自設的pwm~pa6采用系統時鐘的80分頻,即一個pwm周期為0.125us*80=10us,由軟件方法實現設定,PA6的震動頻率是80的整倍數,占空比自調(必須由中斷計時)*/
- /*為降低定時器中斷產生的難度,暫時將pwm周期設置為20us,由__delay_us()函數來完成。*/
- /* 注意:本程序所有的占空比是小數表示,不是用百分數表示*/
- #include "syscfg.h"
- #include "MS83Fxx02.h"
- #include<stdio.h>
- #define _XTAL_FREQ 32000000 //內部晶振頻率為16M
- //時鐘周期與機器周期為1:1,2T模式下,2個機器周期為一個指令周期,當前系統指令周期為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;} //所有燈打開
- #define TMR0_VALUE_INIT 5;//1個timer0中斷是(255-5)*0.125us*256=8ms,1250個timer0中斷是1s
- #define delay_num 500; //大量延時用的默認毫秒值
- unsigned int interrupt_time=0; //timer0中斷次數
- float adc_avg_num=0, VCC=0.0; //ADC轉換出來的數值
- /*-------------------模糊自適應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);//在目標值會多次改變的情況下,建議在函數內部初始pid參數而不是作為形參
- /*-------------------------------模糊自適應pid結束---------------------------------*/
- void Delay_xms(unsigned int);//不精準的毫秒延時
- void Delay_xus(unsigned int);//不精準的微妙延時
- void colck_init(void);//所有時鐘初始化
- void interrupt_init(void);//中斷模塊初始化
- void io_init(void);//所有io初始化
- void adc_init(void);//adc模塊初始化
- void pwm_colck_init(void);//pwm初始化,IO端口將在set_pwm中設置,本函數主要初始化的是相關時鐘配置
- void shutdown_pwmall(void);//關閉所有PWM,同時使P1C和P1B引腳狀態固定pc4=1;pc3=0;
- unsigned int start_tran_adc(void);//官方給定的ADC程序
- unsigned int start_tran_adc2(void);//專門用來測量電流值大小
- unsigned int start_tran_adc_vcc(void);//用來測量VCC電壓
- void set_pwm_frequence(unsigned int, float);//設置PA6端口周期和占空比,調用_delay_us()函數完成,如果用定時器中斷,則基準頻率可以最高提升到8M,
- void set_duty(float duty);//占空比操作封裝
- void set_cycle(unsigned int cycle);//周期操作封裝
- void set_pwm1(float duty,unsigned int cycle);//系統pc3引腳的pwm設置,//PWM周期 Tpwm=(PR2+1)*4*Tsys*預分頻
- void set_pwm2(float duty,unsigned int cycle);//系統pc4引腳的pwm設置
- void set_half_bridge(float duty,unsigned int cycle);//系統pc3,pc3組成的半橋pwm設置
- void controlpwm_calc(float expecte_volate,float expecte_current);//輸入預期電流電壓,該程序自動調控,設置占空比和頻率
- void controlpwm_fruzy_pid(float expecte_voltage,float expecte_current);//自適應模糊PID控制算法
- float avg_adc_value(unsigned int frequency);//平均多次采樣數據,采樣次數由frequency 確定
- void act_led(float);//根據adc轉化的值處理燈閃爍狀態
- void breathe_led(unsigned int,float);//呼吸燈程序
- void system_check(void);//系統閃燈自檢
- /*********************************************main*****************************************************************************/
- int main()
- {
- colck_init();
- interrupt_init();
- io_init();
- adc_init();
- system_check();
- INTCON=0B10000000;
- // while(ADC_time>1250)//1250*8ms=10秒,每10秒啟動一次檢測
- while (1)
- {
- {
- VCC=12*start_tran_adc_vcc()/1023;
- act_led(VCC);
- interrupt_time=0;
- }
- }
- }
- /*********************************************************************************************************************/
- void colck_init(void)//中心頻率16M,2T模式,看門狗禁用,定時器0中斷
- {
- //UCFG0
- ////////關鍵數據//////////////
- OPTION=0B00000111;//time0定時器,PORTA上拉使能,下降沿中斷,內部時鐘,預分頻器給timer0使用,timer0 256分頻
- OSCCON=0B11110101;//低頻,256Khz,timer0每一指令周期加1,16M系統時鐘focs
- WDTCON=0B00010110;//看門狗時鐘源32khz,65565分頻,溢出周期為32khz/65535,軟件關閉看門狗(sbit<0>)
- ADCON1=0B01010000;//ADC選擇內部時鐘16分頻(ADC采樣頻率為16M/16=1M)
- ////////關鍵數據//////////////*
- //T1CON=0B10000101;//time1定時器,高電平有效,1:1分頻,內部時鐘
- //T2CON&=0B11111000;//time2定時器關閉
- /*全局中斷及time1中斷在此設置*/
- }
- void interrupt_init(void)//關閉比較器中斷,禁止ADC中斷
- {
- INTCON=0B10100000;//全局中斷使能,TIMER0中斷使能
- PIE1 = 0b00000000;
- PIE2 = 0b00000000;
- PIR1 = 0b00000000;
- PIR2 = 0b00000000;
- IOCA=0B00000000;
- }
- void io_init(void)
- {
- //GPIO 設置為輸出時,弱上拉會自動關斷,此點可作為測試關鍵點
- CMCON0=0B00000111;//關閉PA6引腳的比較模式
- MSCKCON=0B00000000;
- TRISA=0B00000000;//全部porta引腳配置為輸出
- TRISC=0B00000100;//除PC2配置為AN6輸入外,其他全部配置為輸出
- PORTA=0B00000000;//初始化porta端口,全部低電平
- PORTC=0B00000000;//初始化portc端口,全部低電平
- ANSEL=0B01000000;//除AN6通道為模擬輸入外,其他全部設置為數字IO
- WPUA=0B00000000;//全部弱上拉使能
- WPUC=0B00000000;//全部允許弱上拉
- WPD=0B00000000;//pc2,PA4,PC1,PC3都設置為下拉
- }
- void adc_init(void)
- {
- // ADCON0=0B10111000;//右對齊,內部2v參考電壓,AN6模擬通道,轉換完成,禁止ADC開始(sbit<0>)
- ADCON0=0B11011000;//參考3v,sbit<6:5> 10 3v,01 2v,00 VCC
- ADCON1=0B01100000;//ADC采樣時間初始化,初始化為系統時鐘的64分頻
- //ADCON0=0B10111100;//1/4VDD an7
- }
- float avg_adc_value(unsigned int frequency)//平均多次轉換結果,使轉換結果更穩定
- {
- 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)//根據adc轉換的平均結果控制閃燈
- {
- if(tmp>=4.16)//大于4.16v充滿,綠燈常亮
- {
- red_off;
- green_on;
- }
- else if(tmp>4.0&&tmp<4.08)//3.0v~4.08v補足充電,綠燈閃
- {
- 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)//預充階段,紅燈閃爍
- {
- 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++;//系統指令周期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)//在目標值會多次改變的情況下,建議在函數內部初始pid參數而不是作為形參
- {
- float tar = 0,cur = 0; //目標值 , 實際值
- 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; //目標值 - 實際值
- ec = e - lastE; //誤差變化率
- sumE += e;
- lastE = e;
- OUT = fuzzy(e, ec); //模糊控制調整 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; //中斷標志位清零
- T2CON = 0B00000100;//timer2預分頻比<1,0> 00 1:1 與系統時鐘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信號為模擬信號
- ADCON0 = 0b11011001; //右對齊,ADC使能,AN6通道,3V參考電壓
- __delay_us(20);
- GO_DONE=1; //開始轉換
- while(GO_DONE==1) ; //等待轉換完成
- TempADCBuffer = ADRESH;
- TempADCBuffer = (TempADCBuffer<<8)|ADRESL;
- asm("nop");
- ADON = 0;
- ANSEL=0B00000000;//關閉模擬輸入an6
- TRISC=0B00000000;//關閉AN6,防止影響另一路adc
- return(TempADCBuffer);
- }
- unsigned int start_tran_adc2(void)
- {
- unsigned int TempADCBuffer=0;
- TRISA=0B00000010;//除PA1配置為AN1輸入外,其他全部配置為輸出
- ANSEL=0B00000010;
- ADCON0= 0b10100101; //右對齊,ADC使能,選擇AN1通道,2V參考電壓,AN1通道,pa1口
- __delay_us(20);
- GO_DONE=1; //開始轉換
- while(GO_DONE==1) ; //等待轉換完成
- TempADCBuffer = ADRESH;
- TempADCBuffer = (TempADCBuffer<<8)|ADRESL;
- asm("nop");
- ADON = 0;
- ANSEL=0B00000000;//關閉模擬輸入an1
- TRISA=0B00000000;//關閉AN1,防止影響另一路adc
- return(TempADCBuffer);
- }
- unsigned int start_tran_adc_vcc(void)
- {
- unsigned int TempADCBuffer=0;
- TRISA=0B00000000;//全部配置為輸出
- ANSEL=0B10000000;//配置an7信號為模擬信號
- //ADCON0= 0b10111101; //右對齊,ADC使能,采集內部1/4VCC通道信號,2V參考
- ADCON0= 0b11011101; //3v參考
- __delay_us(20);
- GO_DONE=1; //開始轉換
- while(GO_DONE==1) ; //等待轉換完成
- TempADCBuffer = ADRESH;
- TempADCBuffer = (TempADCBuffer<<8)|ADRESL;
- asm("nop");
- ADON = 0;
- ANSEL=0B00000000;//關閉模擬輸入an1
- TRISA=0B00000000;//關閉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>中,所以為了封裝進行了以下一系列操作
- 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*預分頻 ,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*預分頻 ,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;
- }
- /*以下程序為控制算法*///控制算法1
- void controlpwm_calc(float expecte_voltage,float expecte_current)//根據公式計算的算法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)//模糊自適應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 是調整輸出電壓的數據
- cycle_temp=1.2*VCC*speed_pid( expecte_current,s_cur)/(1-duty_temp);//s_cur 是調整電流的數據,由另一路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);
- }
- }
復制代碼
|