;********************************************************************************
;Set up the assembler options (Chip type, clock source, other bits and pieces)
LIST p=16F1827, r=DEC
#include <P16F1827.inc>
__CONFIG _CONFIG1, _BOREN_OFF & _MCLRE_ON & _PWRTE_ON & _WDTE_OFF & _FOSC_HS
__CONFIG _CONFIG2, _LVP_OFF & _PLLEN_OFF
;********************************************************************************
;Set aside memory locations for variables
DELAYTEMP EQU 112
DELAYTEMP2 EQU 113
SysWaitTempMS EQU 114
SysWaitTempMS_H EQU 115
COMMANDX EQU 579
COMMAND EQU 32
HSERRECEIVE EQU 33
LOOPS EQU 34
SERDATA EQU 35
SysTemp1 EQU 36
;********************************************************************************
;Alias variables
AFSR0 EQU FSR0L
AFSR0_H EQU FSR0H
;********************************************************************************
;Vectors
ORG 0
goto BASPROGRAMSTART
ORG 4
retfie
;********************************************************************************
;Start of program memory page 0
ORG 5
BASPROGRAMSTART
;Call initialisation routines
call INITSYS
call INITUSART
;Start of the main program
banksel TRISB
bcf TRISB,3
bcf TRISB,4
bcf TRISB,5
bcf TRISB,6
banksel LOOPS
clrf LOOPS
SysForLoop1
incf LOOPS,F
bsf PORTB,3
movlw 250
movwf SysWaitTempMS
clrf SysWaitTempMS_H
call Delay_MS
bcf PORTB,3
movlw 250
movwf SysWaitTempMS
clrf SysWaitTempMS_H
call Delay_MS
movlw 4
subwf LOOPS,W
btfss STATUS, C
goto SysForLoop1
SysForLoopEnd1
START
;FNSTART,HSERRECEIVE
call FN_HSERRECEIVE138
movlw low COMMANDX
addlw 1
movwf AFSR0
clrf SysTemp1
movlw high COMMANDX
addwfc SysTemp1,W
movwf AFSR0_H
movf HSERRECEIVE,W
movwf INDF0
movlw low COMMANDX
addlw 1
movwf AFSR0
clrf SysTemp1
movlw high COMMANDX
addwfc SysTemp1,W
movwf AFSR0_H
movf INDF0,W
movwf SERDATA
call HSERSEND
movlw low COMMANDX
addlw 1
movwf AFSR0
clrf SysTemp1
movlw high COMMANDX
addwfc SysTemp1,W
movwf AFSR0_H
movlw 48
subwf INDF0,W
movwf COMMAND
btfss COMMAND,0
goto ELSE2_1
bsf PORTB,3
goto ENDIF2
ELSE2_1
bcf PORTB,3
ENDIF2
btfss COMMAND,1
goto ELSE3_1
bsf PORTB,4
goto ENDIF3
ELSE3_1
bcf PORTB,4
ENDIF3
btfss COMMAND,2
goto ELSE4_1
bsf PORTB,5
goto ENDIF4
ELSE4_1
bcf PORTB,5
ENDIF4
btfss COMMAND,3
goto ELSE5_1
bsf PORTB,6
goto ENDIF5
ELSE5_1
bcf PORTB,6
ENDIF5
goto START
BASPROGRAMEND
sleep
goto BASPROGRAMEND
;********************************************************************************
Delay_MS
incf SysWaitTempMS_H, F
DMS_START
movlw 227
movwf DELAYTEMP2
DMS_OUTER
movlw 6
movwf DELAYTEMP
DMS_INNER
decfsz DELAYTEMP, F
goto DMS_INNER
decfsz DELAYTEMP2, F
goto DMS_OUTER
decfsz SysWaitTempMS, F
goto DMS_START
decfsz SysWaitTempMS_H, F
goto DMS_START
return
;********************************************************************************
;Overloaded signature:
FN_HSERRECEIVE138
call HSERRECEIVE139
movf SERDATA,W
movwf HSERRECEIVE
return
;********************************************************************************
;Overloaded signature: BYTE:
HSERRECEIVE139
SysWaitLoop2
btfss PIR1,RCIF
goto SysWaitLoop2
btfss PIR1,RCIF
goto ENDIF6
banksel RCREG
movf RCREG,W
banksel SERDATA
movwf SERDATA
ENDIF6
banksel RCSTA
btfss RCSTA,OERR
goto ENDIF7
bcf RCSTA,CREN
bsf RCSTA,CREN
ENDIF7
banksel STATUS
return
;********************************************************************************
HSERSEND
SysWaitLoop1
btfss PIR1,TXIF
goto SysWaitLoop1
movf SERDATA,W
banksel TXREG
movwf TXREG
banksel STATUS
return
;********************************************************************************
INITSYS
banksel ADCON0
bcf ADCON0,ADON
bcf ADCON1,ADFM
banksel ANSELA
clrf ANSELA
clrf ANSELB
banksel CM2CON0
bcf CM2CON0,C2ON
bcf CM1CON0,C1ON
banksel PORTA
clrf PORTA
clrf PORTB
return
;********************************************************************************
INITUSART
movlw 7
banksel SPBRG
movwf SPBRG
movlw 2
movwf SPBRGH
bsf BAUDCON,BRG16
bsf TXSTA,BRGH
bcf TXSTA,SYNC
bsf RCSTA,SPEN
bsf RCSTA,CREN
bsf TXSTA,TXEN
banksel STATUS
return
;********************************************************************************
;Start of program memory page 1
ORG 2048
END