include "p16F877a.inc"
org 0x00
errorlevel -302
errorlevel -305
Temp_W equ 0x20
Temp_STATUS equ 0x21
MS_TIMER equ 0x22
Time1 equ 0x23
Time2 equ 0x24
TimeDiff equ 0x25
HexValue equ 0x26
ASCIIValue1 equ 0x27
ASCIIValue2 equ 0x28
Nibble equ 0x29
goto Setup
org 0x04
call ISR
org 0x10
Setup
bcf STATUS, RP1
bsf STATUS, RP0 ;bank select 1
movlw h'00'
movwf TRISB ;sets PORTB to all output
movlw h'FF' ;sets PORTD to all input
movwf TRISD
TMR0Setup
bcf STATUS, RP1
bcf STATUS, RP0
movlw b'00000111' ;sets prescalar (1:256)
movwf OPTION_REG
bcf INTCON, TMR0IF
bsf INTCON, 7
bsf INTCON, 5 ;enables Gloabal and timer0 interruputs
movlw d'252'
movwf TMR0 ;puts decimal 252 into timer register
bcf INTCON, TMR0IF ;clears the interrupt flag
UARTInitialize
bsf STATUS, RP0
movlw b'00011001'
movwf SPBRG ;sets 9600 baud rate and 4MHz clock
movlw b'00100110'
movwf TXSTA ;inititalizes the transmit register
bcf STATUS, RP0
movlw b'10010000'
movwf RCSTA ;intitializes the reciever resgister
SPIInitialize
bcf STATUS, RP1
bsf STATUS, RP0
movlw b'01000000'
movwf SSPSTAT ;sets up PIC1 SPI as master
movlw b'10010000'
movwf TRISC ;sets up PORTC for SPI use(*master)
bcf STATUS, RP1
bcf STATUS, RP0
movlw b'00110000'
movwf SSPCON
CheckBeamBreak
btfsc PORTD, 0 ;checks to see if the copper wire breaks beam
goto CheckBeamBreak
movfw MS_TIMER
movwf Time1 ;records time of breaking beam
CheckBeamRestore
btfss PORTD, 0 ;checks to see if copper wire has crossed beam
goto CheckBeamRestore
movfw MS_TIMER
movwf Time2 ;stoers this time
call CalcDiff
call DisplayTimes
goto CheckBeamBreak
ISR
bcf INTCON, GIE ;disables all interupts
comf PORTB
SaveSettings
movwf Temp_W ;saves W register
swapf STATUS, W ;moves STATUS into W register
movwf Temp_STATUS ;saves Temp_STATUS
incf MS_TIMER ;incremments MS_TIMER
ResetTimer0
movlw d'252'
movwf TMR0 ;puts decimal 252 into timer register
bcf INTCON, TMR0IF ;clears the interrupt flag
bsf INTCON, GIE ;enables interupts
RestoreSettings
swapf Temp_STATUS, W ;Puts Temp_STATUS into W
movwf STATUS ;restores STATUS register
swapf Temp_W, F ;swaps nibbles of temp_w
swapf Temp_W, W ;restores old value of the W register
retfie