《直流无刷电机控制PIC程序().docx》由会员分享,可在线阅读,更多相关《直流无刷电机控制PIC程序().docx(11页珍藏版)》请在课桌文档上搜索。
1、include#include/2008.4.24-14点00分includeintcharge_delayjm,lkk,trial,run_num,protect_num;charm,n,k,s,L,tri;charhall_num;/循环次数charhall_number;/1+3+2+10+8+9=33(一个循环和),hallless_protectQintpwm_hi;/pwm占空比上限intpwm_cycle;0wm周期intpwmo;/pwm占空比下限,由A/D结果确定intpwm_real;/pwm占空比实际intad_result;A/D转换结果bitghy,run_flag;
2、运行标志0,未运行,停顿bitflag_autohand;开场为自动voiddelay(delay_num)intdelay_num;(Intj;for(j=delay_num;j1;j-)(asm(,nop);)unionadresinty1;unsignedcharadre2;adresult;voidpara_chushihua()(protect_num=0;trial=0b10110000;tri=0x00;Ikk=10;halLnum=0x00;运行标志一0,未运行,停顿开场为-自动/ PWm周期/ PWm舌空比上限PWm占空比上限/实际PWm占空比为PWm周期上限设定下限制止中断
3、hall_number=0x00;run_flag=0;flag-autohand=1;PWm_cycle=240;/pwm_hi=pwm_cycle-5;pwm_hi=pwm_cycle-1;pwm_real=pwm_cycle;+PWmO=OXoA;voidnointerupt_chushihua()(INTCON=0X00;)voidIo_chushihua()/ ADCON l=0x8e;/RA0/AN0-VOLTAGE电压采样,右对齐1ADCON1=0x86;/设定RA0-RA6设定为I/O口;TRISA=0x09;/RAOPROTECT保护(电流保护和过温保护)输出,0不保护保护;
4、/RAl-PSWl,继电器,输出Q电源接通/RA2一一指示灯,输出0/RA3-按键,加,输入1/RA4-指示灯,输出0RA5-RSWI潍电器,输出0,软启充电PORTA=PoRTA&0x09;/RAl=O,一一输出低电平,PSWl,继电器断RBPU=O;TRISB=Oxfe;RA2=0,-输出低电平,指示灯灭RA4=0,-输出低电平,指示灯灭RA5=0,-输出低电平,RSWl,继电器断/RB端口内部弱上拉电路制止/RB7=1,空-输入/RB6=1-HA,输入/RB5=1-一HB,输入/RB4=1HG输入/RB3=1-停,输入,按钮/RB2=1一一启,输入,按钮/RBl=I-一百/卞选择,揄入,
5、开关/RBO=O-ST/STOP,输出,控制GALPORTB=PORTB&Oxfe;/RBO=O,-输出低电平,制止GAL输出/RC7A1+,PWMA1+,输出/RC6Al,输出RC5-B1+,输出/RC4BL输出/RC3-Cl+,输出/RC2CCPI(PWM)?输出,死区/RCl-减,输入,按钮TRISC=0x02;PORTC=0x06;)voidprotect()/RC0Cl,输出00000010GIE=0;pwm_real=pwm_hi;CCPR1L=pwm_real;ccprih=0x00;CCPICON=OxOc;PR2=pwm_cycle;TRISC=T句SC&Oxfb;T2CON
6、=OxOO;TMR2ON=1;PORTC=OxOO;PORTB=PORTBcfeOxfe;GAL输出while(l)过流和过温保护关全局中断/PWM_real-PWM实时值设置PWM输出为最大/CCPl选择为PWM工作方式设置PWM的周期设置RC2-PWM输出分频系数=OXoo启用TMR2/RBO=O,-输出低电平,制止PORTA=0b00010100;delay(15000);延时voidprotect_run()GIE=O;pwm_real=pwm_hi;CCPR1L=pwm_real;ccprih=0x00;CCPlCON=OxOc;PR2=pwm_cycle;TRlSC=TRISC&O
7、xfb;T2CON=OxOO;TMR2ON=1;PORTC=0x00;PORTB=PORTBcfeOxfe;GAL输出过流和过温保护关全局中断/PWM_real-PWM实时值设置PWM输出为最大/CCPl选择为PWM工作方式设置PWM的周期设置RC2PWM输出分频系数=OXOo启用TMR2/RBO=O,输出低电平,制止while(l)/PORTA=0b00010000;PORTA=ObOOOOOiOO;delay(15000);/RA4闪动/RA2闪动延时voidPWM_chushihua()/CCPRlL=OxaO;ccprih=0x00;run_num=0;pwm_real=pwm_hi;
8、CCPR1L=pwm_real;ccprih=0x00;CCPICON=OxOc;PR2=pwm_cycle;TRISC=TRISC&0xfb;T2CON=OxOO;TMR2ON=1;/PWM.real-PWM按时值/设置PWM输出为最关/CCPl选择为PWM工作方式/设置PWM的周期设置RC2PWM输出分频系数=OXOO启用TMR2voidopen_loopf()开环运行50次,启动k=ObOOO11000;s=PORTC&ObOOOOOHO;PORTC=ks;delay(8000);k=0b10010000;s=PORTC&ObOOOOOHO;PORTC=ks;delay(8000);k=
9、0b10000001;s=PORTC&ObOOOOOHO;PORTC=ks;delay(8000);k=ObOO100001;s=PORTC&ObOOOOOHO;PORTC=ks;delay(8000);/5/4/6/2/HA,HB,HC,/101,/延时/HA,HB,HC,/100,/延时/HA,HB,HC,/110,/延时/HA,HB,HC,/010,/延时B-C+A+B-C-A+B+C-k=0b01100000;/3/HA,HB,HC,s=PORTC&ObOOOOOHO;/011,A-B+PORTC=ks;/delay(8000);延时k=0b01OOl000;/1/HA,HB,HC,s
10、=PORTC&ObOOOOOHO;/001,C+A-PORTC=ks;/delay(8000);延时)voidinterruptRB4_int()(charth;run_num+;if(run_num10)run_num=10;m=PORTB;/RB7-HA,RB5-HB,RB4-HCm&=0xB0;m=0x90A=A,B=B,C=C/1011/11n=m4;switch(n)(/HA,HB,HC,/case1:k=b1001OOO;break;/001,C+A-/case3:k=0b01100000;break;/011,A-B+/case2:k=ObOO100001;break;/010,
11、B+C-/case10:k=Ob10000001;break;/110,C-A+/case8:k=0b1001OOOO;break;/100,A+B-/case9:k=0b0001100Oibreak:/101,B-C+/default:k=ObOOOOOOOO;无效casel:k=0b00011000;break;case3:k=b1001OOO;break;case2:k=0b01100000;break;caseIOik=ObOO100001;break;case 8: k=0b10000001;break;case 9: k=0b1001OOOOibreak:其它(Oo(MH),0x0
12、0,输出为/101,B-C+/001,C+A-/011,A-B+/010,B+C-/110,C-A+/100,A+B-效default=ObOOOOOOOO;其它(OO(Mll),0x00,输出为无s=PORTC&ObOOOOOHO;取RC1,RC2状态PORTC=ks;输出PORTCif(RB7=0)trial=ObOO110000;/RB7=0if(RB5=0)trial=0b10010000;/RB5=0if(RB4=0)trial&=Ob10100000;/RB4=0/ / / / / / /if(lkkl)Ikk-;每20次测试一次tri=n;if(tri&=0b00001000)=
13、0)trial&=ObOO110000;/RB7=0tri=n;if(tri(fe=ObOOOOOO10)=0)trial&=Ob10010000;/RB5=0tri=n;if(tri&=ObOOOOOOO1)=0)trial&=Ob10100000;/RB4=0if(lkk2)Ikk-;每20次测就一次elsewhile(trial!=ObOOOOOOOO)GIE=O; pwm_real=pwm_hi;CCPR1 L=pwm_real;Ccprih=OxOO;CCP ICON=OxOc;方式关全局中断/PWM_real-PWM 实时值设置PWM输出为最大/CCPl选择为PWM工作PR2=pw
14、m_cycle;TRISC=TRISC&Oxfb;T2CON=OxOO;TMR2ON=1;PORTC=0x00;PORTB=PORTBtfeOxfe;制止GAL输出/设置PWM的周期设置RC2PWM输出分频系数二OXOo/启用TMR2/RBO=O,-输出低电平,while(l)PORTA=ObOOO 10000;for (jm= 15000;jm 1 ;jm)/RA4闪动asm(,nop); )trial=0b 10110000;Ikk= 10;RBIF=O;void max_min_compare()if (pwm_realpwm_hi) pwm_real=pwm_hi;if (PWm_re
15、alpwmo) pwm_real=pwm_lo;main(void)para_chushihua(); nointerupt_chushihua(); Io_chushihua();PWM_chushihua();RAl=0;通电源RA5=0;电路充电延时RA2=0;RA4=0;delay(100);RBO=O;止GAL输出delay(100);RAl=I;通电源/参数初始化制止中断初始化/I/O初始化/PWM初始化/RAl-PSWl发送低电平,接/RA5-RSWl发送低电平,主/RA2,指示灯灭/RA4,指示好灭 主电路充电延时ST/(STOP)发送低电平,制/RAl-PSWl发送高电平,接
16、for(charge_delay=0;charge_delay50;charge_delay+)delay(15000);)RA5=1;/RA5-RSWl发送高电平,主电路充电延时delay(100);主电路充电延时PP:asm(no,r);/RBIF=O;制止RB4-7中断,清RBIF标志设定RBIE=O;定GIE=O;制止RB4-7中断,RBIE设/关全局中断RA2=0;/RA2,指空灯灭RA4=0;/RA4,指示疔灭runflag=O;运行标志一由RB2按钮设置while(runflag=O)/主程序/(RB2启动键,低电平有效if(RB2=0)run_flag=l;/运行标志一由RB2
17、按钮设置delay(100);1延时/运行,那么执行Jwhile(run_flag=l)intope;/设定开环运行次数为12次intopenloopn;/RBO=I;放GAL输出/ST(-STOP)发送高电平,开delay(100);RA4=1;/pwm_real=180;pwm_real=230;延时delay(500);/延时/ope=10;ope=3;设定开环运行次数为20次设定开环运行次数为20次for(OPen100Pn=0;OPenIooPn=ope;OPenIOoPn+)(0000000000000000CCPR1L=PWmjrea1;-130open_loopf();)一/0
18、00000000000000000delay(6000);RBIF=O;RBIE=I;GIE=I;RA2=1;pwm_real=pwm_hi;ghl:asm(nop);/00设置开环下PWM输出值开环运行20次,启动/延时/RB4-7中断,RBIF标志设定/RB4-7中断,RBIE设定力井荃扃中断/if(pwmreal110)&(run_num10)如果PW011140,但是run_num30)/如果30次保护,那么认为是真正保护(过温和过流源市软件谑菠)、protect();)过流和过温保护protect_num=0;if(RBl=l) flag-autohand= 1;else flag_
19、autohand=0;/自/手标志设定,标志设定 RBI=I-AUTO;自动RB 1=O-H AND;手动/if (flag_autohand=l)/111if (PWm_realpwmo) PWm_real=l; 自动 else pwm_real=pwm_lo;/11111111111111111111else( if(RA3=0)加(低有效),那么if (pwm_realpwm_lo) pwm_real-=l; else pwm_real=pwm_lo;max_min_compare();CCPR1 L=pwm_real; delay(4500); w 输入,hi!e(RA0=l)高电平保
20、护设置PWM输出调用延时子程序/RAO-过流和过温保护Il、protect();过流和过温保护/while(RB3=0)/RB3J亭机键,低电平有效run_flag=0;运行标志一由RB3按钮浚置if (run_flag=O)/停机,那么跳转至 (for(;pwm_realpwm_hi;pwm_real+)delay(2000);至上限否)/CCPR1 L=pwm_real; )RB0=0;止GAL输出para_chushihua(); nointerupt_chushihua(); PWM_chushihua();run_num=0;oto PP;else/设置PWM输出为最大/ST(-STOP)发送低电平,制参数初始化制止中断初始化/PWM初始化run_nUiTi=O;gotoghl;gotoPP;