;---------------------------------------------------------------
;PIC16C54
;---------------------------------------------------------------
INDF EQU 0 ;F0
TMR0 EQU 1 ;F1
PC EQU 2 ; Program counter
STATUS EQU 3 ; PIC status byte
FSR EQU 4 ; File Select Register
RA EQU 5 ;
RB EQU 6 ;
RC EQU 7 ;
;
TXBUF EQU 08 ; TX BUFFER
RXBUF EQU 09 ; RX buffer
POFFBUF EQU 0B ; Bit buffer
COUNT EQU 0CH ; Bit counter
OOFFBUF EQU 0DH
ADDR EQU 0EH
RBBUFNEW EQU 0FH
RBBUFOLD EQU 10H
VOICEBUF EQU 11H
DELAYBUF1 EQU 12H
UPDOWNBIT EQU 14H
ONEVOICEBIT EQU 15H
CHARGEVBIT EQU 16H
INCDECBIT EQU 17H
RBIT EQU 18H
XRABUF EQU 19H
COUNT1 EQU 1AH
SENDBUF EQU 1BH
RABUFNEW EQU 1CH
RABUFOLD EQU 1DH
MODEBIT EQU 1EH
XRBBUF EQU 1FH
;
;----------------------------------------------------------------------------
C EQU 0 ;DEF BIT OF F3
DC EQU 1
Z EQU 2
PD EQU 3
TO EQU 4
RP0 EQU 5
RP1 EQU 6
RP2 EQU 7
;-----------------------------------------------------------------------------
; Bit Assignments
;-----------------------------------------------------------------
;RB PORT
DOWNKEY EQU 0 ;RB0
AUXKEY EQU 2 ;RB2
FXPKEY EQU 1 ;RB1
FZDKEY EQU 3 ;RB3
TX_CON EQU 4 ;RB4
VIOCEMIC EQU 5 ;RB5
SCL EQU 6 ;RB6, serial clock
SDA EQU 7 ;RB7, data in/out
;-----------------------------------------------------------------
;RA PORT
MICKEY EQU 0 ;RA0
UPKEY EQU 1 ;RA1
VOICEBIT EQU 2 ;RA2
MICRADIOBIT EQU 3 ;RA3
;-----------------------------------------------------------------
SENDKJDQBIT EQU 0
SENUPKEY EQU 2
SENDOWNKEY EQU 1
SENZHJDQBIT EQU 3
SENAUXONOFF EQU 7
;-----------------------------------------------------------------
ORG 1FFH
GOTO MAIN
;------------------------------------------------------------------
ORG 0
;------------------------------------------------------------------
SETRARBRC
MOVLW B'11111111'
TRIS RA
MOVLW B'11001111'
TRIS RB
RETLW 0
;------------------------------------------------------------------
READRARB
MOVF RBBUFNEW,W
MOVWF RBBUFOLD
MOVF RB,W
MOVWF RBBUFNEW
MOVLW 0FH
ANDWF RBBUFNEW,1
MOVF RABUFNEW,W
MOVWF RABUFOLD
MOVF RA,W
MOVWF RABUFNEW
MOVLW 0FH
ANDWF RABUFNEW,1
RETLW 0
;------------------------------------------------------------------
DELAY
MOVF DELAYBUF1,W
MOVWF 1FH
DLY1 NOP
DECFSZ 1FH,1
GOTO DLY1
RETLW 0
;------------------------------------------------------------------------
KEYSCAN
RETLW 0
;--------------------------------------------------------------------------
BSTART
MOVLW B'00001111' ; Put SCL, SDA line in output state
TRIS RB
NOP
BSF RB,SDA ;make sure sda is high
BSF RB,SCL ; Set clock high
NOP
NOP
BCF RB,SDA ; SDA goes low during SCL high
NOP ; Timing adjustment
NOP
BCF RB,SCL ; Start clock train
RETLW 0
;-----------------------------------------------------------------------------
BSTOP
MOVLW B'00001111' ; Put SCL, SDA line in output state
TRIS RB
NOP
BCF RB,SDA ; Return SDA to low
BSF RB,SCL ; Set SCL high
NOP
NOP
BSF RB,SDA ; SDA goes from low to high during SCL high
NOP
NOP
BCF RB,SCL
RETLW 0
;-----------------------------------------------------------------------------
; Serial data send from PIC to serial EEPROM, bit-by-bit subroutine
;-----------------------------------------------------------------------------
RX MOVLW 8H
MOVWF COUNT
CLRF RXBUF
MOVLW B'10001111'
TRIS RB
NOP
RXLP
BSF RB,SCL
BCF STATUS,C
NOP
NOP
NOP
BTFSC RB,SDA
BSF STATUS,C
RLF RXBUF,1
BCF RB,SCL
DECFSZ COUNT,1
GOTO RXLP
RETLW 0
;-----------------------------------------------------------------------------
; TRANSMIT DATA subroutine
;-----------------------------------------------------------------------------
TX MOVLW 8H
MOVWF COUNT
TXLP MOVLW B'00001111'
TRIS RB
NOP
BTFSS TXBUF,7
GOTO TX0
BSF RB,SDA
GOTO TX1
TX0 BCF RB,SDA
TX1 NOP
NOP
BSF RB,SCL
NOP
NOP
NOP
BCF RB,SCL
RLF TXBUF,1
DECFSZ COUNT,1
GOTO TXLP
MOVLW B'10111111'
TRIS RB
NOP
ACK NOP
BCF RB,SCL
NOP
NOP
BSF RB,SCL
NOP
NOP
BTFSC RB,SDA
GOTO ACK
BCF RB,SCL
RETLW 0
;-----------------------------------------------------------------------------
; BYTE-WRITE, write one byte to EEPROM device
;-----------------------------------------------------------------------------
WRBYTE
MOVLW 0A0H
MOVWF TXBUF
CALL BSTART
CALL TX
MOVF ADDR,W
MOVWF TXBUF
CALL TX
MOVF POFFBUF,W
MOVWF TXBUF
CALL TX
CALL BSTOP
RETLW 0
;-----------------------------------------------------------------------------
; BYTE-READ, read one byte from serial EEPROM device
;-----------------------------------------------------------------------------
RDBYTE
MOVLW 0A0H
MOVWF TXBUF
CALL BSTART
CALL TX
MOVF ADDR,W
MOVWF TXBUF
CALL TX
CALL BSTART
MOVLW 0A1H
MOVWF TXBUF
CALL TX
CALL RX
MOVLW B'00001111'
TRIS RB
NOP
BSF RB,SDA
BCF RB,SCL
NOP
BSF RB,SCL
CALL BSTOP
MOVF RXBUF,W
MOVWF OOFFBUF
RETLW 0
;----------------------------------------------------------------------------
INCDEC
MOVF INCDECBIT,W
BTFSS STATUS,Z
GOTO ID1
INCF VOICEBUF,1
MOVLW 4
XORWF VOICEBUF,W
BTFSS STATUS,Z
GOTO ID2
MOVLW 0
MOVWF VOICEBUF
GOTO ID2
ID1 DECF VOICEBUF,1
MOVLW 0FFH
XORWF VOICEBUF,W
BTFSS STATUS,Z
GOTO ID2
MOVLW 3
MOVWF VOICEBUF
ID2 MOVF VOICEBUF,W
MOVWF POFFBUF
RETLW 0
;------------------------------------------------------
TX_SER_T0
BSF RB,TX_CON
MOVLW 10H
MOVWF DELAYBUF1
CALL DELAY
BCF RB,TX_CON
MOVLW 060H
MOVWF DELAYBUF1
CALL DELAY
RETLW 0
;------------------------------------------------------
TX_SER_T1
BSF RB,TX_CON
MOVLW 060H
MOVWF DELAYBUF1
CALL DELAY
BCF RB,TX_CON
MOVLW 12H
MOVWF DELAYBUF1
CALL DELAY
RETLW 0
;------------------------------------------------------
MAIN
CLRW ;SET I/O DIRECTION
CLRWDT
MOVLW B'00001100'
OPTION
CLRF TMR0
CLRWDT
MOVLW B'00001101'
OPTION
CALL SETRARBRC
BSF RB,VIOCEMIC
CLRF ADDR
MAINA CLRF CHARGEVBIT
CLRF ONEVOICEBIT
CLRF INCDECBIT
CALL RDBYTE ;read back data
MOVF OOFFBUF,W
MOVLW 4
SUBWF OOFFBUF,W
BTFSC STATUS,Z
GOTO MAIN3
BTFSS STATUS,C
GOTO MAIN1
MAIN3 CLRF POFFBUF
CLRF VOICEBUF
CALL WRBYTE
GOTO MAIN2
MAIN1 MOVF OOFFBUF,W
MOVWF VOICEBUF
MAIN2 CALL READRARB
;---------------------------------------------------
START CALL SETRARBRC
CALL READRARB
CLRF SENDBUF
CLRF TMR0
CLRWDT
MOVLW B'00001101'
OPTION
;---------------------------------------------------
MODEPD BTFSC RABUFNEW,MICKEY
GOTO ONEVOICE
BCF RB,VIOCEMIC
BCF RB,TX_CON
GOTO START
ONEVOICE BTFSC RABUFNEW,MICRADIOBIT
GOTO MRSPAKE
BSF RB,VIOCEMIC
MOVLW B'00110000'
MOVWF SENDBUF
BTFSS RABUFNEW,UPKEY
BSF SENDBUF,SENUPKEY
BTFSS RBBUFNEW,DOWNKEY
BSF SENDBUF,SENDOWNKEY
M3B BTFSC RBBUFNEW,AUXKEY
BSF SENDBUF,SENAUXONOFF
BTFSC RBBUFNEW,FXPKEY
BSF SENDBUF,SENZHJDQBIT
BTFSS RBBUFNEW,FZDKEY
BSF SENDBUF,SENDKJDQBIT
GOTO TX_SER
;---------------------------------------------------------------------------
MRSPAKE BTFSS RABUFNEW,VOICEBIT
GOTO VOICE
MOVLW B'00010000'
MOVWF SENDBUF
M1B BSF RB,VIOCEMIC
BTFSS RBBUFNEW,DOWNKEY
BSF SENDBUF,SENDOWNKEY
M1A BSF RB,VIOCEMIC
BTFSS RABUFNEW,UPKEY
BSF SENDBUF,SENUPKEY
GOTO M3B
;---------------------------------------------------
VOICE BSF RB,VIOCEMIC
MOVF RABUFOLD,W
XORWF RABUFNEW,W
MOVWF XRABUF
MOVLW 02H
ANDWF XRABUF,1
BTFSC STATUS,Z
GOTO VOICEC
BTFSC RABUFNEW,UPKEY
GOTO VOICEB
CLRF INCDECBIT
CALL INCDEC
CALL WRBYTE
GOTO VOICEB
VOICEC MOVF RBBUFOLD,W
XORWF RBBUFNEW,W
MOVWF XRBBUF
MOVLW 01H
ANDWF XRBBUF,1
BTFSC STATUS,Z
GOTO VOICEB
VOICEA BTFSC RBBUFNEW,DOWNKEY
GOTO VOICEB
MOVLW 55H
MOVWF INCDECBIT
CALL INCDEC
CALL WRBYTE
VOICEB MOVLW 0
XORWF VOICEBUF,W
BTFSS STATUS,Z
GOTO VOICE1
MOVLW B'00100000'
MOVWF SENDBUF
GOTO M3B
VOICE1 MOVLW 1
XORWF VOICEBUF,W
BTFSS STATUS,Z
GOTO VOICE2
MOVLW B'01000000'
MOVWF SENDBUF
GOTO M3B
VOICE2 MOVLW 2
XORWF VOICEBUF,W
BTFSS STATUS,Z
GOTO VOICE3
MOVLW B'01010000'
MOVWF SENDBUF
GOTO M3B
XORWF VOICEBUF,W
BTFSS STATUS,Z
GOTO START
MOVLW B'01100000'
MOVWF SENDBUF
GOTO M3B
;------------------------------------------------------------------------
TX_SER
MOVLW 8
MOVWF COUNT
TXMTR BCF RA,TX_CON
CALL TX_SER_T0
CALL TX_SER_T0
TXNEXT RRF SENDBUF,1
BTFSC STATUS,C
GOTO TX_SER1
CALL TX_SER_T0
CALL TX_SER_T0
GOTO TX_SER2
CALL TX_SER_T1
TX_SER2 DECFSZ COUNT,1
GOTO TXNEXT
TXSTOP BCF RA,TX_CON
MOVLW 0EFH
MOVWF DELAYBUF1
CALL DELAY
CALL DELAY
CALL DELAY
GOTO START
;--------------------------------------------------------------------------
END