Basic Gun Page 5: Putting it all together
This is all the code that is necessary for the laser gun. However, it would be nice to make it a little bit more complete. The major flaw with the current code is that the PIC simply emmits a constant stream of the waveform. We would like it to only make the stream when a user asks it to (i.e. pulls the trigger). To make this slightly more complicated, we have to make sure that each time the user pulls the trigger the PIC outputs enough of the waveform to register a hit. (This takes 35 repitions of the waveform.) So with that in mind, we head back to coding, and this is the result:
start btfsc porta,1 ;Check to see if the trigger is depressed
goto start ;If it isn't, keep looping until it is.
movlw d'35' ;The number of times to send the waveform
movwf repeats ;Move the number to a file register
movlw d'76' ;Number of times to repeat carrier loop
movwf carrier ;Move the number to a file register
fire bsf portb,1 ;Turn on LED,13
nop ;1
nop ;2
nop ;3
nop ;4
nop ;5
nop ;6
nop ;7
nop ;8
nop ;9
nop ;10
nop ;11
nop ;12
bcf portb,1 ;Turn off LED, 13
nop ;1
nop ;2
nop ;3
nop ;4
nop ;5
nop ;6
nop ;7
nop ;8
nop ;9
decfsz carrier,f ;Decrease the carrier register,
;skip the next instruction if zero, 10, or 1,2 if carrier=0
goto fire ;11,12
movlw d'76' ;Get ready for the next time carrier loop, 3
movwf carrier ;Move the number into the appropriate file regeister, 4
call delay1 ;Delay for 775 instuctions (5-780)
call delay1 ;Delay for another 775 instructions (781-1556) 419 instructions left
call delay2 ;Delay for another 415 instructions (1557-1972)
nop ;1973
decfsz repeats ;1974
goto fire ;Return to the carrier routine if repeats doesn't equal zero, 1975,1976
wait1 btfss porta,1 ;Wait until trigger is released
goto wait1
goto start ;Return to waiting for user input
delay1 movlw 0x00 ;Loop for maximum duration (775 instructions)
movwf counta ;File register to be decremented
dec1 decfsz counta,f ;Decrease file register
goto dec1 ;Not equal to zero, repeat
return ;Equal to zero, return
delay2 movlw d'136' ;Loop for maximum duration (415 instructions)
movwf counta ;File register to be decremented
dec2 decfsz counta,f ;Decrease file register
goto dec1 ;Not equal to zero, repeat
return ;Equal to zero, return
This code, plus the small amount of extra code to transform it into a complete program is available, along with the compiled hex file, HERE. The code is written for the 16F628A.
