《直流无刷电机控制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)=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