ORG 0x0000
bra Initialize
ORG 0x0008
bra HighInt ; If using a high interrupt if not then 0x0018
Initialize
bsf STATUS,RP0 ; bank 1
bcf TRISC,1 ; Output: RC1
bcf TRISC,2 ; Output: RC2
movlw b'00001111' ; Outputs: RD2,RD4-RD7
movwf TRISD
movlw b'01110001' ; 8Mhz
movwf OSCCON
movlw b'00001110' ; enable AN0
movwf ADCON1
movwf b'00000101' ; Fosc/16
movwf ADCON2
movlw 0x3F ; 31.2 kHz PWM
movwf PR2
movlw b'00000000' ; 1:2 prescaler for TMR0
movwf T0CON
bcf STATUS,RP0 ; bank 0
clrf PORTD ; turn off windings
clrf PORTC
movlw b'00000001' ; left justified, AN0 selected, module on
movwf ADCON0
movlw 0x07
movwf CMCON ; turn off comparators
clrf CCPR1L
clrf CCPR2L
movlw b'00001100' ; pwm mode
movwf CCP1CON
movwf CCP2CON ; turn on PWM
bsf T2CON,TMR2ON ; turn on Timer 1
bsf T1CON,TMR1ON ; turn on Timer 1
[COLOR="Red"] bsf T0CON,TMR0ON ; turn on Timer 0
movlw 0xE0
movwf INTCON ;Turn on Timer0 Interrupt .
bsf INTCON2,TMR0IP ;Make timer 0 high priority. [/COLOR]
clrf State ; initialize motor state pointer and duty cycle
clrf Index ; index pointer
clrf Mode
clrf ADRESH
movlw 1
movwf StepModeScaler
movwf Counter
clrf Speed
clrf ButtonState ; initialize button state
Main;[COLOR="Red"]
btfss myFlag,0 ; Wait 2.5ms
goto Main
bcf myFlag,0 ;Clear the flag for next time[/COLOR]
call ButtonPress ; Call the button press debounce routine
btfsc STATUS,C ; The carry flag is set if a button press just occurred
incf Mode,f ; Change to the next state when a button press occurs.
..............................................................
............................................................
...................................................
[COLOR="Red"]HighInt:
bcf INTCON,TMR0IF ;
bsf myFlag,0 ; Create your own flag
retfie FAST[/COLOR]
END