Code:
' File...... 8722 Scan_ASM_Test
' Purpose... Drive two steppers to achieve diagonal moves
' *****************************************************************************
' * *
' * INCLUDE & DEFINES *
' * *
' *****************************************************************************
clear
define OSC 40 ' Use HSPLL during compilation
DEFINE INTHAND Tmr_ints
' *****************************************************************************
' * *
' * VARIABLES *
' * *
' *****************************************************************************
X_steps var word BankA System ' Steps to move, not reusable
Count_pulse var WORD BankA System ' Pulse duration = 3.n + 7
X_delay var WORD BankA System
Y_delay var WORD BankA System
goto Start ' Skip over ASM code
' *****************************************************************************
' * *
' * ASSEMBLER CODE *
' * *
' *****************************************************************************
ASM
;******************************************************************************
; Higher priority for Timer0 overflows, send out stepper
; drive pulse for X axis on PORTD.0
Tmr_ints
btfss INTCON,TMR0IF ; did Timer0 interrupt?
bra LowPri ; no. branch to Timer1 int
bsf PORTD,0 ; yes. High PORTD.0
call Pulse_out ; Call delay routine
bcf PORTD,0 ; Low PORTD.0
bcf INTCON,TMR0IF ; Clear Timer0 interrupt flag
movff X_delay+1,TMR0H ; Load timer0 with _X_delay high byte
movff X_delay,TMR0L ; Load timer0 with _X_delay low byte
decf X_steps,F ; Decrement X_steps low byte
; Lower priority for Timer1 overflows, send out stepper
; drive pulse for Y axis on PORTD.3
LowPri
btfss PIR1,TMR1IF ; Timer1 int pending?
bra IntExit ; no. exit
bsf PORTD,3 ; High PORTD.3
call Pulse_out ; Call delay routine
bcf PORTD,3 ; High PORTD.3
bcf PIR1,TMR1IF ; Clear Timer1 interrupt flag
movff Y_delay+1,TMR1H ; Load Timer1 with Y_delay high byte
movff Y_delay,TMR1L ; Load timer1 with Y_delay low byte
IntExit
retfie FAST ; return & restore WREG, STATUS, BSR
;****************************************************************************** *
; SUBROUTINES * *
;******************************************************************************
Pulse_out ; Delay = 3.n + 7 [0.1 microseconds]
movlw 0xff ; Load w with n
movwf Count_pulse ; Load Count with n
Pulse_loop
decfsz Count_pulse ; Decrament the file Count
goto Pulse_loop ; Loop if not zero
return
ENDASM
;****************************************************************************** *
; MAIN ASM * *
;******************************************************************************
ASM
_Main_ASM
; Set up interrupt conditions for Timer0 and Timer1
bcf RCON,7 ; Disable priority levels on interrupts,IPEN=0
bsf INTCON,7 ; Enable all interrupts
bsf INTCON,6 ; Enable all peripheral interrupts
bcf INTCON,3 ; Disable PORTB interrupts
bcf INTCON2,2 ; Make Timer0 overflow low priority
bcf IPR1,0 ; Make Timer1 overflow low priority
bsf INTCON,5 ; Enable Timer0 overflow int
bsf PIE1,0 ; Enable Timer1 overflow int
; Set up Timer0 conditions
bcf T0CON,6 ; Timer0 uses 16 bits
bcf T0CON,5 ; Timer0 uses internal osc
bcf T0CON,4 ; Timer0 count on rising edge og osc
bsf T0CON,3 ; Timer0 pre-scaler not assigned
clrf TMR0H ; Zero the clock
clrf TMR0L ; Zero the clock
bsf T0CON,7 ; Timer0 on
; Set up Timer1 conditions
bsf T1CON,7 ; Timer1 uses 16 bits
bcf T1CON,1 ; Timer1 uses internal osc
clrf TMR1H ; Zero the clock
clrf TMR1L ; Zero the clock
bsf T1CON,0 ; Timer1 on
; Temp code to simulate PBP input for the delays
; Removed - should come from PBP
; Check if X_steps_lb=0 (decremented in high priority interrupt)
; If so, check X_steps_hb and if zero, end
; If not, decrement X_steps_hb, set X_steps_lb and go again
Main_loop
movff X_steps+1,PORTJ ; Steps hi to PORTJ
movff X_steps,PORTB ; Steps lo to PORTB
tstfsz X_steps ; Test steps lo, skip next if zero
goto Main_loop ; More steps to do, so loop
tstfsz X_steps+1 ; Test steps hi, skip next if zero
bra Re_load_lb
goto All_done
Re_load_lb
decf X_steps+1,F ; Decrement hb and then carry to lb
setf X_steps ; Decremented hb, so load ff to lb
goto Main_loop
All_done
bcf INTCON,7 ; Clear GIE, disable ALL interrupts
clrwdt
return
ENDASM
' *****************************************************************************
' * *
' * INITIALISE *
' * *
' *****************************************************************************
Start:
ADCON1 = %00001101 ' A0, A1 analog, rest digital
CMCON = %00000111 ' Comparators off, this frees up PORTF
TRISA = %00000000
TRISB = %00000000
TRISC = %00000000
TRISD = %00000000 ' D.0 is X drive. D.3 is Y drive
TRISE = %00000000 ' LEDs to display X_steps lo byte
TRISF = %00000000
TRISG = %00000000
TRISH = %00000000
TRISJ = %00000000 ' LEDs to display X_steps hi byte
' *****************************************************************************
' * *
' * MAIN *
' * *
' *****************************************************************************
Main:
X_delay = $FFFF
Y_delay = $FFFF
X_steps = $FFFF
Count_pulse = $FFFF
CALL Main_ASM
pause 100
goto Main
end
Bookmarks