这个程序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);
- }
- }
复制代码
|