Basic Gun Page 4: Coding the Signal

Now we have code for the carrier frequency. All that remains in making the proper waveform is to superimpose this waveform on the signal waveform. The signal waveform is aproximately 250 Hz, so this translates into 76 times through the carrier frequency instructions (done on the previous page), followed by 1976 instructions of nothing. To do this, we will need a loop that cycles through the carrier frequency 76 times, and then a delay routine that gives around 1976 instructions of delay. The loop is achieved by putting the number 76 into a file register and decrementing it every time that the code is executed. By using the decfsz command, we can exit the loop when the file register gets to zero. Below is the code to do this. As before, the numbers in the comments tell the instruction number for timing purposes.

       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
       nop                ;1974
       goto   fire        ;Return to the carrier routine, 1975,1976


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

So this code now creates the entire waveform. The additional code simply calls two different timing delay loops (to be explained in greater detail in the Microcontroller Programming Basics page that will be made at a later date). Basically, they just put a number into a file register, and then decrease the number again and again until the number equals zero. Then they return to the original program.