; LIST p=16F628 ; PIC16F628 is the target processor Processor 16F628 Include "p16F628.inc" ; +---------------+ ; | | ; STEP_MODE 1-| RA2 RA1 |-18 HALFSTEPOUT ; | | ; HALFSTEPIN 2-| RA3 RA0 |-17 RESET ; | | ; CHIP_ENABLE 3-| RA4 OCS |-16--+------------+ n/c ; | | | Oscillator | ; n/c 4-| ~MCLR OCS |-15--+------------+ n/c ; | | ; 0V 5-| Vss Vdd |-14 +5V ; | | ; MOVE 6-| RB0 RB7 |-13 LSTOP ; | | ; RX 7-| RB1 RB6 |-12 RSTOP ; | | ; TX 8-| RB2 RB5 |-11 DIROUT ; | | ; CLOCK 9-| RB3 RB4 |-10 DIRIN ; +---------------+ ;==================================================================== ; Configuration Bits: ; Code Protection = OFF ; WatchDog Timer = OFF ; Power-up Timer = ON ; Oscillator Type = INTERNAL ; Internal RC Oscillator = ON ; Low Voltage Program = OFF ; Master Clear = OFF ; Brown Out Detect = OFF ;==================================================================== __Config _CP_OFF & _WDT_OFF & _PWRTE_ON & _INTRC_OSC_CLKOUT & _LVP_OFF & _MCLRE_OFF & _BODEN_OFF CBLOCK 0X20 ;start of internal memory COUNTER0, COUNTER1 ;define delay counters STEPCOUNT ENDC #define CHIP_ENABLE PORTA,4 ;define i/o pin labels #define MOVE PORTB,0 #define RX PORTB,1 #define TX PORTB,2 #define CLOCK PORTB,3 #define DIRIN PORTB,4 #define DIROUT PORTB,5 #define LSTOP PORTB,7 #define RSTOP PORTB,6 #define STEP_MODE PORTA,2 #define HALFSTEPIN PORTA,3 #define HALFSTEPOUT PORTA,1 #define RESET PORTA,0 _ResetVector set 0x00 ;reset vector org _ResetVector ;pic starts code execution from here goto START nop nop ; *** Start of program code *** START movlw 0x07 ;Turn comparators off and movwf CMCON ;enable pins for i/o functions bsf STATUS,RP0 ;select page 1 movlw B'00001101' ;i/o pin selection for port movwf TRISA ;set port A movlw B'11010011' ;i/o pin selection for port movwf TRISB ;set port B bcf OPTION_REG,7 ;switch on portB pull up resistors bcf STATUS,RP0 ;return to page 0 clrf PORTA ;clear port A clrf PORTB ;clear port B MAIN CLRWDT ;clear watchdog timer in case of latchup BTFSC RESET ;check reset pin goto RESET_MODE ;if 1 go to reset mode BTFSS MOVE ;check move pin goto MAIN ;if 0 go back to main BTFSC HALFSTEPIN ;check half step pin goto HALFSTEPMODE ;if 1 goto half step mode goto FULLSTEPMODE ;if 0 goto full step mode HALFSTEPMODE bsf HALFSTEPOUT ;if 1 set high for half step mode goto STEPMODE ;goto step mode FULLSTEPMODE bcf HALFSTEPOUT ;if 0 set low for full step mode goto STEPMODE ;goto step mode STEPMODE BTFSC STEP_MODE ;check step mode pin goto FREERUN ;if 1 go to free running mode goto ONE_STEP ;if 0 go to one step mode RESET_MODE BTFSS LSTOP ;if lstop high then skip next line call LEFT ;call left routine BTFSS LSTOP ;if lstop high then skip next line goto RESET_MODE ;go back to reset mode clrf STEPCOUNT ;clear stepcount counter register call RIGHT ;call right routine incf STEPCOUNT,f ;increment step counter BTFSS RSTOP ;if rstop high then skip next line goto $-3 ;go back 3 instructions bcf STATUS,C ;clear complimentary bit rrf STEPCOUNT,f ;move stepcount one bit right to divide by 2 call LEFT ;call left routine decfsz STEPCOUNT,f ;decrement stepcount by one and skip next instruction on 0 goto $-2 ;go back 2 instructions goto MAIN ;go back to main FREERUN BTFSC DIRIN ;if dirin pin low then skip next line goto TRYRIGHT ;go to tryright routine goto TRYLEFT ;go to tryleft routine ONE_STEP BTFSC DIRIN ;if dirin pin low then skip next line goto STEPRIGHT ;go to stepright routine goto STEPLEFT ;go to stepleft routine STEPLEFT BTFSS LSTOP ;if lstop high then skip next line call LEFT ;call left routine btfsc MOVE ;if move pin low then skip next line goto $-1 ;go back to last instruction goto MAIN ;go back to main STEPRIGHT BTFSS RSTOP ;if rstop high then skip next line call RIGHT ;call right routine btfsc MOVE ;if move pin low then skip next line goto $-1 ;go back one instruction goto MAIN ;go back to main TRYRIGHT BTFSS RSTOP ;if rstop high then skip next line call RIGHT ;call right routine goto MAIN ;go back to main TRYLEFT BTFSS LSTOP ;if lstop high then skip next line call LEFT ;call left routine goto MAIN ;go back to main RIGHT bcf CHIP_ENABLE ;set chip_enable pin low bcf DIROUT ;set direction out pin low to go right bsf CLOCK ;set clock pin high call DELAY ;go to delay routine bsf CHIP_ENABLE ;set chip enable pin high bcf CLOCK ;set clock pin low call DELAY ;call delay routine return ;return LEFT bcf CHIP_ENABLE ;set chip_enable pin low bsf DIROUT ;set direction out pin high to go left bsf CLOCK ;set clock pin high call DELAY ;go to delay routine bsf CHIP_ENABLE ;set chip enable pin high bcf CLOCK ;set clock pin low call DELAY ;call delay routine return ;return DELAY movlw .10 ;delay is equal to 10 milliseconds by moving .10 into w register movwf COUNTER0 ;move w register into COUNTER0 FLOOP nop ;no operation nop ;no operation movlw .248 ;inner part of delay loop equal to 1 millisecond by moving .248 into w register movwf COUNTER1 ;move w register into COUNTER1 FILOOP nop ;no operation decfsz COUNTER1,f ;decrement counter1 by 1 goto FILOOP ;goto filoop decfsz COUNTER0,F ;decrement counter 0 by 1 goto FLOOP ;goto floop return ;return END ;end of program ; *** End of program ***