PIC单片机对步进电机的控制设计

pic16f874步进电机程序
listp=16f874
include
;--==*[constants]*==--
#definecommand_2’b’
#definecommand_3’c’
#definecommand_4’d’
;--==*[variables]*==--
cmdnumequ0x20;commandnumber.0xffforinvalidcommand
recbyteequ0x21;receivedbyte
motorlequ0x22;motordata-lowbyte
motorhequ0x23;motordata-highbyte
bytecntequ0x24;internalcounterforbytecountingduringreception
ticklequ0x25;ticks:lowbyte
tickhequ0x26;ticks:highbyte
result1lequ0x27;result:thisoneisneededforstorageofticks
result1hequ0x28
winkellequ0x29;winkel:tickcounterwithoutoverflowchecketc.
winkelhequ0x30
quadequ0x31;quadraturesignal,fordirectionstorage
oldbequ0x32;oldvalueofportb
newbequ0x33;newvalueofportb
tmr0o0equ0x34;tmr0overflowcounter0
tmr0o1equ0x35;tmr0overflowcounter1
wtemp1equ0x60;storageforthewregister(rbif)
wtemp2equ0x61;storageforthewregister(ccp2if)
wtemp3equ0x62;storageforthewregister(rcif)
wtemp4equ0x63;storageforthewregister(usarterrorroutines)
org0x00;--==*[resetvector]*==--
bmain
org0x04;--==*[intvector]*==--
bint_handler
org0x05;--==*[mainprogram]*==--
main:
;--==*[variables-initialize]*==--
movlw0xff
movwfcmdnum
clrfmotorl
clrfmotorh
clrftickl
clrftickh
clrfresult1l
clrfresult1h
clrfwinkell
clrfwinkelh
clrfquad
clrfoldb
clrfnewb
clrftmr0o0
clrftmr0o1
;--==*[ports-setupportb]*==--
clrfportb
bsfstatus,rp0;pin4and5asinput(photo-interruptersignals)
movlwb’00110000’;pin1hastobeanoutput(directionsignal)
movwftrisb;allotherpinsareoutputs,too(default)
bcfstatus,rp0
;--==*[ports-setupportc]*==--
clrfportc
bsfstatus,rp0
movlwb’11111011’;pin2asoutput(pwmsignal)
movwftrisc;pins6and7havetobeinputs(default)
bcfstatus,rp0;allotherpinsareinputs,too(default)
;--==*[ccp1-setuppwmmodule]*==--
bsfstatus,rp0
movlwd’249’;pwmperiod(calculatedwithformula)
movwfpr2
bcfstatus,rp0;dutycycle=0
clrfccpr1l;ccpr1lisupper8(bit:9.。.2)bitsofdutycycle
movlwb’00000101’;enabletimer2(bit:2)andsetprescaleof1:4(bit:0-1)
movwft2con;timer2isimportantforpwmoperation!
movlwb’00001111’;selectpwmmodeforccp1module(bit:0-3)
movwfccp1con;andresetlowertwobitsofdutycycle(bit:4-5)
;--==*[usart-setup]*==--
bsfstatus,rp0
movlw0x40;configurebaudgeneratorregister(calculatedwithformula)
movwfspbrg;with:9600baud,noparity,8databits,nohandshake
movlwb’00100100’;enabletrasmit(bit:5)andhighspeedbaudrate(bit:2)
movwftxsta
bcfstatus,rp0
movlwb’10010000’;enableserialport(bit:7)andcontinuousreception(bit:4)
movwfrcsta
clrw;w=0
movwfrcreg;resetuartreceiverandfifo
movwfrcreg;sowecanavoidreceive/framing/overrunerrorsatthebeginning
movwfrcreg
movwftxreg;justincase:thetxifflagisnowvalid(=1;avoidsinfiniteloopsinsendbyte)
;--==*[timer0-setup]*==--
bsfstatus,rp0;thisistricky;prescalerhastobeassignedtothewdt,
;incaseyouwanttoachieve1:1prescale
bcfoption_reg,ps0;first,setprescalerto1:2
bcfoption_reg,ps1
bcfoption_reg,ps2
bsfoption_reg,psa;then,assignprescalertowdt;nowwehavea1:1prescalefortimer0:-)
bcfoption_reg,t0se
bcfoption_reg,t0cs
bcfstatus,rp0
;--==*[interrupts-setup]*==--
bsfstatus,rp0
clrfpie1
bsfpie1,rcie;enable“receivebyte”interrupt
bcfstatus,rp0
clrfintcon;resetallinterruptflags
bsfintcon,rbie;enable“interruptonchange”interrupt
bsfintcon,t0ie;enable“timer0overflow”interrupt
bsfintcon,peie;enableperipheralinterrupts
bsfintcon,gie;enableglobalinterrupts
;--==*[mainloop]*==--
loop:
bloop
;--==*[sendbyte-routine]*==--
sendbyte:;sendbyte(whichisstoredinw)
sendbyte_l0:;waituntilnewdataarrivedintxreg
btfsspir1,txif;(indicatedviatransmitinterruptflagbit:txif)
bsendbyte_l0
sendbyte_l1:
movwftxreg;sendnewdata
return
;--==*[interrupthandlingroutine]*==--
int_handler:
btfscrcsta,oerr;overflowerroroccured,handleit
berr_overflow
btfscrcsta,ferr;framingerroroccured,handleit
berr_frame
btfscpir1,rcif;receiveinterrupt:rcif
bint_usart_receive
btfscintcon,rbif;pininterrupt:rbif
bint_rb_change
btfscintcon,t0if;tmr0interrupt:t0if
bint_timer0_reset
retfie
int_rb_change:
incftickl,1;incrementticks(lowbyte)
btfscstatus,z
incftickh,1;incrementticksonoverflow(highbyte)
incfwinkell,1;sameastick,butwillnotbereset(eichungswert)
btfscstatus,z
incfwinkelh,1
movwfwtemp1;savew
movfwportb
movwfnewb;newb=portb
movlwb’00110000’;andmask
andwfoldb,1;resetallbitsexcept4and5
andwfnewb,1;resetallbitsexcept4and5
clrfquad;resetquadvalue
clrw;oldb==00?
subwfoldb,w
bzo00
movlwb’00010000’;oldb==01?
subwfoldb,w
bzo01
movlwb’00100000’;oldb==10?
subwfoldb,w
bzo10
bo11;else,oldb==11
o00:
movlwb’00010000’;newb==01?
subwfnewb,w
bnzquit
bsfquad,7;left
bquit
o01:
movlwb’00110000’;newb==11?
subwfnewb,w
bnzquit
bsfquad,7;left
bquit
o10:
clrw;newb==00?
subwfnewb,w
bnzquit
bsfquad,7;left
bquit
o11:
movlwb’00100000’;newb==10?
subwfnewb,w
bnzquit
bsfquad,7;left
quit:
movfwportb
movwfoldb;oldb=portb
movfwwtemp1;restorew
bcfintcon,rbif;resetinterrupt(important)
retfie
int_timer0_reset:
btfsctmr0o0,7;wait128overflows
gotoa1
incftmr0o0,1
gotoa0
a1:
btfsctmr0o1,6;wait64overflows
gotoa4
incftmr0o1,1
gotoa0
a4:
btfscportb,7;ashorthack,sowecanmeasuretheimpulses
gotonext1;oftimer0withanoscillograph
bsfportb,7;signalisonbit7ofportb
gotomainl
next1:
bcfportb,7
mainl:
movwfwtemp2;savew
movfwtickl;storeticksinresult1
movwfresult1l
movfwtickh
movwfresult1h
movfwquad;andblendthedirectionbitonmsbofresult1
iorwfresult1h,1
clrftickh;cleartickcounter
clrftickl
clrftmr0o0;cleartimer0overflowcounters
clrftmr0o1
movfwwtemp2;restorew
a0:
bcfintcon,t0if;resetinterrupt(important)
retfie
int_usart_receive:
movwfwtemp3;savew
movlwcommand_3;command3active?
subwfcmdnum,w
bzgetdata_command3;yes,handleit
movfwrcreg;storereceivedbyte
clrfrcreg;it’sagoodideatoflushthebuffer
clrfrcreg;afterreceivingabyte,soit’s
clrfrcreg;forcedthatwehaveanewbyteinthebufferinthenextstep
movwfrecbyte
movlwcommand_2;-executecommand2?
subwfrecbyte,w
bzcommand2;yes,doit
movlwcommand_3;-executecommand3?
subwfrecbyte,w
bzcommand3;yes,doit
movlwcommand_4;-executecommand4?
subwfrecbyte,w
bzcommand4;yes,doit
commandunknown:;else,receivedbyteisunknown
bsfportb,0;showerroronleds
movfwwtemp3;restorew
retfie
;--==*[commandexec-transmitmotordataindebugmode]*==--
command2:
movfwresult1h
callsendbyte
movfwresult1l
callsendbyte
movfwwinkelh
callsendbyte
movfwwinkell
callsendbyte
movfwwtemp3;restorew
retfie
;--==*[commandinit-setupforreceivemotordata(part1/2)]*==--
command3:;command3
movlwcommand_3
movwfcmdnum;cmdnumcontainsnowthecurrentcommandvalue
movlw.2
movwfbytecnt;wewantexactly2bytesfromthepc
movfwwtemp3;restorew
retfie
;--==*[commandexec-receivemotordata(part2/2)]*==--
getdata_command3:
decfbytecnt,1;handlebytecounter
bzc3_b2;ifbytecounteris0thenitisthe2ndbyte
movfwrcreg;else,1stbytereceive
movwfmotorl;storeinmotorl
bouthere;andexit
c3_b2:
movfwrcreg;2ndbytereceive
movwfmotorh;storeinmotorh
movlw0xff;resetcmdnumtoundefinedvalue(0xff)
movwfcmdnum
;reconfigurepwm
movfwmotorh
movwfccpr1l;storehighbyte(8;bits9-2)
bcfccp1con,ccp1y
btfscmotorl,0;storelowbyte(2;bits0)
bsfccp1con,ccp1y
bcfccp1con,ccp1x
btfscmotorl,1;storelowbyte(2;bits1)
bsfccp1con,ccp1x
btfssmotorl,2;motorl《2》bitissignificantformotordirection
bturn_left
bsfportb,1;turnmotorright
bouthere
turn_left:
bcfportb,1;turnmotorleft
outhere:
movfwwtemp3;restorew
retfie
;--==*[commandexec-transmitmotordata]*==--
command4:
movfwresult1h;transmithighbyte
callsendbyte
movfwresult1l;transmitlowbyte
callsendbyte
movfwwtemp3;restorew
retfie
;--==*[errorhandling-fortheserialcommunication]*==--
err_overflow:;handleoverflowerror
movwfwtemp4;savew
bsfportb,7;showerroronleds(10)
bcfportb,6
bcfrcsta,cren;disablecontinuousreception
movfrcreg,w;flushreceivefifobuffer(3bytesdeep)
movfrcreg,w
movfrcreg,w
bsfrcsta,cren;reenablecontinuousreception
movfwwtemp4;restorew
retfie
err_frame:;handleframeerror
movwfwtemp4;savew
bcfportb,7;showerroronleds(01)
bsfportb,6
movfrcreg,w;flushreceivefifobuffer(3bytesdeep)
movfrcreg,w
movfrcreg,w
movfwwtemp4;restorew
retfie
end

微软似乎还计划将Cortana并入Office
广和通创新探索5G+AI领域,助力工业智能机器人“聪明”上线
物联网和大数据将如何革新我们的智慧城市
WQX2超声波风速风向仪的简单介绍
SMT生产线的未来已来:全自动智能智造设备一网打尽!
PIC单片机对步进电机的控制设计
浅谈嵌入式开发中测试用例的调试和测试方法
APP时代域名到底还重不重要
IEEE1394接口
Linux系统中O_APPEND和O_TRUNC标志的使用方法
物联网所谓的万物互联,需要哪些技术支撑
高压螺杆式压缩机振动的原因是什么
中国宣布已攻克可重构5G射频芯片
Canyon-SQL: 新的 ORM
VC曝出了RTX 3090 HOF的PCB样板,26相供电
华为解禁谈判重启 G20特朗普为何“松口”?
谷歌向手机厂商发出警告 修改Android内核代码将暴露更多的安全Bug
郑州正方科技户外储能电站太阳能板充电实测
空调智能控制是通过是什么实现的?
预期2020年笔电出货量将达2亿台,2021年比今年再激增四成