;Begin main program ; ;Software initialization ; ; LIST P=PIC16C66, R=DEC LIST P=PIC16F876, R=DEC include "C:\Progra~1\MPLAB\P16f876.INC" ; ; ;Register file map ;Bank 0 registers indf equ 0x0 ;indirect memory addressing tmr0 equ 0x1 ;timer0 module's register pcl equ 0x2 ;program counter LSB status equ 0x3 ;bank select bit fsr equ 0x4 ;indirect memory addressing porta equ 0x5 ;porta data latch portb equ 0x6 ;portb data latch rpc equ 0x6 ;RPC uses 8 byte wide portb portc equ 0x7 ;portc data latch intcon equ 0xB ;tmr0 overflow and interrupt bits pir1 equ 0xC ;flags for peripheral interrupts rcsta equ 0x18 ;receive status and control register txreg equ 0x19 ;transmit register rcreg equ 0x1a ;receive register ;Bank 1 registers trisa equ 0x85 ;porta direction register: 1-input, 0-output trisb equ 0x86 ;portb direction register rpc_ddr equ 0x86 ;Data direction for RPC trisc equ 0x87 ;portc direction register pie1 equ 0x8C ;enable bits for peripheral interrupts txsta equ 0x98 ;transmit status and control register spbrg equ 0x99 ;baud rate generator ;User defined bytes reg1 equ 0x40 ;register settings reg2 equ 0x41 ;read from RPC reg3 equ 0x42 reg4 equ 0x43 reg5 equ 0x44 reg6 equ 0x45 reg7 equ 0x46 reg8 equ 0x47 setting equ 0x73 ;correct RPC setting counter_r equ 0x74 ;# byte received from RPC delay1 equ 0x75 delay2 equ 0x76 delay3 equ 0x77 counter_s equ 0x78 ;# byte sent to RPC ;Pseduonyms for bits ;RPC Bit txa equ 3 ;input- transfer accepted txr equ 2 ;output- tranfer request rxa equ 1 ;output- receive accept rxr equ 0 ;input- receive requested ;Status bit rp0 equ 5 ;bank select bit z equ 2 ;zero bit ;Txsta csrc equ 7 tx9 equ 6 txen equ 5 sync equ 4 brgh equ 2 trmt equ 1 tx9d equ 0 ;rcsta spen equ 7 sren equ 5 cren equ 4 ferr equ 2 oerr equ 1 ;Pie1 txie equ 4 rcie equ 5 ;Pir1 txif equ 4 rcif equ 5 ;intcon gie equ 7 ;Global interrupt enable peie equ 6 ;Peripheral interrupt org 0 start goto init org 20 init call pt_set ;required port setups call syncset ;required async mode setups call reset ;reset RPC call pause ;carter had it before call read_1 ;get register1 info call check_1 ;check register1 setting call read_2 ;get register2 info call check_2 ;check register2 setting call read_3 ;get register3 info call check_3 ;check register3 setting call read_4 call check_4 call read_5 call check_5 call read_6 call check_6 call read_7 call check_7 call read_8 ;get register8 info call check_8 ;check register8 setting endless nop goto endless pt_set clrf portc bsf status, rp0 clrf trisc ;all output except bsf trisc, 7 ;must be set in order to bsf trisc, 6 ;properly set UART pg. 9-95 clrf trisa ;for trigger movlw b'11111001' ;data bus to input movwf rpc_ddr bcf status, rp0 return syncset bsf status, rp0 clrf txsta ;async mode set bsf txsta, brgh movlw d'12' ;initialize baud rate generator movwf spbrg bcf status, rp0 clrf rcsta bsf rcsta, spen ;enable pins bsf status, rp0 ;bank 1 bsf txsta, txen ;Start transmission bcf status, rp0 ;bank 0 bsf rcsta, cren ;enable reception (asynchronous) return reset bsf rpc, txr ;txr should be held high during reset bcf porta, 1 ;reset rpc nop ; nop ;1 us delay necessary nop nop bsf porta, 1 ;return reset high- need 1 ms delay here movlw d'20' movwf delay1 movlw d'64' movwf delay2 loop nop decfsz delay1, f goto loop movlw d'20' movwf delay1 decfsz delay2, f goto loop return txrpc bcf rpc, txr ;request transmit bsf rpc, rxa ;do not accept receive ;call send outbyte bcf rpc, txr bsf rpc, rxa waccept btfsc rpc, txa ;check for transmit accept goto waccept ;loop back until accepted bsf status, rp0 ;set port b to transmit to rpc movlw b'00001001' movwf rpc_ddr bcf status, rp0 swapf indf, f ;prepare nibble for port b movf indf, w iorlw b'00000010' ;place data on port movwf rpc bsf rpc, txr ;remove request wdun btfss rpc, txa ;check if data is been read goto wdun ;wait until data read is confirmed incf fsr, f decfsz counter_s, f goto outbyte ;loop until all bytes sent bsf status, rp0 ;return port b to receive from rpc movlw b'11111001' movwf rpc_ddr bcf status, rp0 return pause movlw d'10' movwf delay3 ;this allows for 10 ms wait movlw d'20' movwf delay1 movlw d'64' movwf delay2 loop10 nop decfsz delay1, f goto loop10 movlw d'20' movwf delay1 decfsz delay2, f goto loop10 movlw d'64' movwf delay2 decfsz delay3, f goto loop10 return read_1 movlw 0x20 movwf fsr movlw b'00000001' ;this is lsn of control byte in bits 0-3 movwf indf incf fsr, f movlw b'00001000' ;this is msn of control byte in bits 0-3 movwf indf movlw d'2' movwf counter_s ;send 2 nibbles to rpc movlw 0x20 movwf fsr call txrpc ;send 'read' control byte to rpc movlw d'4' movwf counter_r ;receive 4 nibbles from rpc (control + read) movlw 0x30 movwf fsr call in_byte movlw 0x32 ;0x32 stores LSN of the register value movwf fsr movf indf, w movwf reg1 ;write LSN to Reg1 incf fsr, f swapf indf, w addwf reg1, f ;write MSN to Reg1 return check_1 movlw 0x40 ;change to reg1 movwf fsr ;pointer movlw b'01100100' ;correct setting for reg1 movwf setting movf indf, w subwf setting, w ;check for errors btfss status, z call rescue_1 ;rewrite reg1 return rescue_1 movlw 0x20 ;location 0x20 movwf fsr movlw b'00000000' ;lsn of write to switches, required for each write movwf indf incf fsr, f movlw b'00001100' ;msn of write to switches, required for each write movwf indf incf fsr, f movlw b'00001000' ;lsn of enable WE in switches, required for each write movwf indf incf fsr, f movlw b'00000000' ;msn of enable WE in switches, required for each write movwf indf incf fsr, f movlw b'00000001' ;lsn of command to write to 0x01 in bits 0-3 movwf indf incf fsr, f movlw b'00001100' ;msn of command to write to 0x01 in bits 0-3 movwf indf incf fsr, f movlw b'00000100' ;lsn of command for location 0x01 (E) movwf indf incf fsr, f movlw b'00000110' ;msn of command for location 0x01 (G) movwf indf movlw 0x20 movwf fsr movlw d'8' movwf counter_s call txrpc call pause ;takes 10 msec to write a register return read_2 movlw 0x20 movwf fsr movlw b'00000010' ;this is lsn of control byte in bits 0-3 movwf indf incf fsr, f movlw b'00001000' ;this is msn of control byte in bits 0-3 movwf indf movlw d'2' movwf counter_s ;send 2 nibbles to rpc movlw 0x20 movwf fsr call txrpc ;send 'read' control byte to rpc movlw d'4' movwf counter_r ;receive 4 nibbles from rpc (control + read) movlw 0x30 movwf fsr call in_byte movlw 0x32 ;0x32 stores LSN of the register value movwf fsr movf indf, w movwf reg2 ;write LSN to Reg2 incf fsr, f swapf indf, w addwf reg2, f ;write MSN to Reg2 return check_2 movlw 0x41 ;change to reg2 movwf fsr ;pointer movlw b'11111111' ;correct setting for reg2 movwf setting movf indf, w subwf setting, w ;check for errors btfss status, z call rescue_2 ;rewrite reg2 return rescue_2 movlw 0x20 ;location 0x20 movwf fsr movlw b'00000000' ;lsn of write to switches, required for each write movwf indf incf fsr, f movlw b'00001100' ;msn of write to switches, required for each write movwf indf incf fsr, f movlw b'00001000' ;lsn of enable WE in switches, required for each write movwf indf incf fsr, f movlw b'00000000' ;msn of enable WE in switches, required for each write movwf indf incf fsr, f movlw b'00000010' ;lsn of command to write to 0x02 in bits 0-3 movwf indf incf fsr, f movlw b'00001100' ;msn of command to write to 0x02 in bits 0-3 movwf indf incf fsr, f movlw b'00001111' ;lsn of command for location 0x02 (P) movwf indf incf fsr, f movlw b'00001111' ;msn of command for location 0x02 (P) movwf indf movlw 0x20 movwf fsr movlw d'8' movwf counter_s call txrpc call pause ;take 10 msec to write a register return read_3 movlw 0x20 movwf fsr movlw b'00000011' ;this is lsn of control byte in bits 0-3 movwf indf incf fsr, f movlw b'00001000' ;this is msn of control byte in bits 0-3 movwf indf movlw d'2' movwf counter_s ;send 2 nibbles to rpc movlw 0x20 movwf fsr call txrpc ;send 'read' control byte to rpc movlw d'4' movwf counter_r ;receive 4 nibbles from rpc (control + read) movlw 0x30 movwf fsr call in_byte movlw 0x32 ;0x32 stores LSN of the register value movwf fsr movf indf, w movwf reg3 ;write LSN to Reg3 incf fsr, f swapf indf, w addwf reg3, f ;write MSN to Reg3 return check_3 movlw 0x42 ;change to reg3 movwf fsr ;pointer movlw b'00000101' ;correct setting for reg3 movwf setting movf indf, w subwf setting, w ;check for errors btfss status, z call rescue_3 ;rewrite reg3 return rescue_3 movlw 0x20 ;location 0x20 movwf fsr movlw b'00000000' ;lsn of write to switches, required for each write movwf indf incf fsr, f movlw b'00001100' ;msn of write to switches, required for each write movwf indf incf fsr, f movlw b'00001000' ;lsn of enable WE in switches, required for each write movwf indf incf fsr, f movlw b'00000000' ;msn of enable WE in switches, required for each write movwf indf incf fsr, f movlw b'00000011' ;lsn of command to write to 0x03 in bits 0-3 movwf indf incf fsr, f movlw b'00001100' ;msn of command to write to 0x03 in bits 0-3 movwf indf incf fsr, f movlw b'00000101' ;lsn of command for location 0x03 (F) movwf indf incf fsr, f movlw b'00000000' ;msn of command for location 0x03 (A) movwf indf movlw 0x20 movwf fsr movlw d'8' movwf counter_s call txrpc call pause ;takes 10 msec to write a register return read_4 movlw 0x20 movwf fsr movlw b'00000100' ;this is lsn of control byte in bits 0-3 movwf indf incf fsr, f movlw b'00001000' ;this is msn of control byte in bits 0-3 movwf indf movlw d'2' movwf counter_s ;send 2 nibbles to rpc movlw 0x20 movwf fsr call txrpc ;send 'read' control byte to rpc movlw d'4' movwf counter_r ;receive 4 nibbles from rpc (control + read) movlw 0x30 movwf fsr call in_byte movlw 0x32 ;0x32 stores LSN of the register value movwf fsr movf indf, w movwf reg4 ;write LSN to Reg4 incf fsr, f swapf indf, w addwf reg4, f ;write MSN to Reg4 return check_4 movlw 0x43 ;change to reg4 movwf fsr ;pointer movlw b'00011110' ;correct setting for reg4 movwf setting movf indf, w subwf setting, w ;check for errors btfss status, z call rescue_4 ;rewrite reg4 return rescue_4 movlw 0x20 ;location 0x20 movwf fsr movlw b'00000000' ;lsn of write to switches, required for each write movwf indf incf fsr, f movlw b'00001100' ;msn of write to switches, required for each write movwf indf incf fsr, f movlw b'00001000' ;lsn of enable WE in switches, required for each write movwf indf incf fsr, f movlw b'00000000' ;msn of enable WE in switches, required for each write movwf indf incf fsr, f movlw b'00000100' ;lsn of command to write to 0x04 in bits 0-3 movwf indf incf fsr, f movlw b'00001100' ;msn of command to write to 0x04 in bits 0-3 movwf indf incf fsr, f movlw b'00001110' ;lsn of command for location 0x01 (O) movwf indf incf fsr, f movlw b'00000001' ;msn of command for location 0x01 (B) movwf indf movlw 0x20 movwf fsr movlw d'8' movwf counter_s call txrpc call pause ;takes 10 msec to write a register return read_5 movlw 0x20 movwf fsr movlw b'00000101' ;this is lsn of control byte in bits 0-3 movwf indf incf fsr, f movlw b'00001000' ;this is msn of control byte in bits 0-3 movwf indf movlw d'2' movwf counter_s ;send 2 nibbles to rpc movlw 0x20 movwf fsr call txrpc ;send 'read' control byte to rpc movlw d'4' movwf counter_r ;receive 4 nibbles from rpc (control + read) movlw 0x30 movwf fsr call in_byte movlw 0x32 ;0x32 stores LSN of the register value movwf fsr movf indf, w movwf reg5 ;write LSN to Reg5 incf fsr, f swapf indf, w addwf reg5, f ;write MSN to Reg5 return check_5 movlw 0x44 ;change to reg5 movwf fsr ;pointer movlw b'00011110' ;correct setting for reg5 movwf setting movf indf, w subwf setting, w ;check for errors btfss status, z call rescue_5 ;rewrite reg5 return rescue_5 movlw 0x20 ;location 0x20 movwf fsr movlw b'00000000' ;lsn of write to switches, required for each write movwf indf incf fsr, f movlw b'00001100' ;msn of write to switches, required for each write movwf indf incf fsr, f movlw b'00001000' ;lsn of enable WE in switches, required for each write movwf indf incf fsr, f movlw b'00000000' ;msn of enable WE in switches, required for each write movwf indf incf fsr, f movlw b'00000101' ;lsn of command to write to 0x05 in bits 0-3 movwf indf incf fsr, f movlw b'00001100' ;msn of command to write to 0x05 in bits 0-3 movwf indf incf fsr, f movlw b'00001110' ;lsn of command for location 0x05 (O) movwf indf incf fsr, f movlw b'00000001' ;msn of command for location 0x05 (B) movwf indf movlw 0x20 movwf fsr movlw d'8' movwf counter_s call txrpc call pause ;takes 10 msec to write a register return read_6 movlw 0x20 movwf fsr movlw b'00000110' ;this is lsn of control byte in bits 0-3 movwf indf incf fsr, f movlw b'00001000' ;this is msn of control byte in bits 0-3 movwf indf movlw d'2' movwf counter_s ;send 2 nibbles to rpc movlw 0x20 movwf fsr call txrpc ;send 'read' control byte to rpc movlw d'4' movwf counter_r ;receive 4 nibbles from rpc (control + read) movlw 0x30 movwf fsr call in_byte movlw 0x32 ;0x32 stores LSN of the register value movwf fsr movf indf, w movwf reg6 ;write LSN to Reg6 incf fsr, f swapf indf, w addwf reg6, f ;write MSN to Reg6 return check_6 movlw 0x45 ;change to reg6 movwf fsr ;pointer movlw b'00000011' ;correct setting for reg6 movwf setting movf indf, w subwf setting, w ;check for errors btfss status, z call rescue_6 ;rewrite reg6 return rescue_6 movlw 0x20 ;location 0x20 movwf fsr movlw b'00000000' ;lsn of write to switches, required for each write movwf indf incf fsr, f movlw b'00001100' ;msn of write to switches, required for each write movwf indf incf fsr, f movlw b'00001000' ;lsn of enable WE in switches, required for each write movwf indf incf fsr, f movlw b'00000000' ;msn of enable WE in switches, required for each write movwf indf incf fsr, f movlw b'00000110' ;lsn of command to write to 0x06 in bits 0-3 movwf indf incf fsr, f movlw b'00001100' ;msn of command to write to 0x06 in bits 0-3 movwf indf incf fsr, f movlw b'00000011' ;lsn of command for location 0x06 (D) movwf indf incf fsr, f movlw b'00000000' ;msn of command for location 0x06 (A) movwf indf movlw 0x20 movwf fsr movlw d'8' movwf counter_s call txrpc call pause ;takes 10 msec to write a register return read_7 movlw 0x20 movwf fsr movlw b'00000111' ;this is lsn of control byte in bits 0-3 movwf indf incf fsr, f movlw b'00001000' ;this is msn of control byte in bits 0-3 movwf indf movlw d'2' movwf counter_s ;send 2 nibbles to rpc movlw 0x20 movwf fsr call txrpc ;send 'read' control byte to rpc movlw d'4' movwf counter_r ;receive 4 nibbles from rpc (control + read) movlw 0x30 movwf fsr call in_byte movlw 0x32 ;0x32 stores LSN of the register value movwf fsr movf indf, w movwf reg7 ;write LSN to Reg7 incf fsr, f swapf indf, w addwf reg7, f ;write MSN to Reg7 return check_7 movlw 0x46 ;change to reg7 movwf fsr ;pointer movlw b'00000001' ;correct setting for reg7 movwf setting movf indf, w subwf setting, w ;check for errors btfss status, z call rescue_7 ;rewrite reg7 return rescue_7 movlw 0x20 ;location 0x20 movwf fsr movlw b'00000000' ;lsn of write to switches, required for each write movwf indf incf fsr, f movlw b'00001100' ;msn of write to switches, required for each write movwf indf incf fsr, f movlw b'00001000' ;lsn of enable WE in switches, required for each write movwf indf incf fsr, f movlw b'00000000' ;msn of enable WE in switches, required for each write movwf indf incf fsr, f movlw b'00000111' ;lsn of command to write to 0x07 in bits 0-3 movwf indf incf fsr, f movlw b'00001100' ;msn of command to write to 0x07 in bits 0-3 movwf indf incf fsr, f movlw b'00000001' ;lsn of command for location 0x07 (B) movwf indf incf fsr, f movlw b'00000000' ;msn of command for location 0x07 (A) movwf indf movlw 0x20 movwf fsr movlw d'8' movwf counter_s call txrpc call pause ;takes 10 msec to write a register return read_8 movlw 0x20 movwf fsr movlw b'00001000' ;this is lsn of control byte in bits 0-3 movwf indf incf fsr, f movlw b'00001000' ;this is msn of control byte in bits 0-3 movwf indf movlw d'2' movwf counter_s ;send 2 nibbles to rpc movlw 0x20 movwf fsr call txrpc ;send 'read' control byte to rpc movlw d'4' movwf counter_r ;receive 4 nibbles from rpc (control + read) movlw 0x30 movwf fsr call in_byte movlw 0x32 ;0x32 stores LSN of the register value movwf fsr movf indf, w movwf reg8 ;write LSN to Reg8 incf fsr, f swapf indf, w addwf reg8, f ;write MSN to Reg8 return check_8 movlw 0x47 ;change to reg8 movwf fsr ;pointer movlw b'00000010' ;correct setting for reg8 movwf setting movf indf, w subwf setting, w ;check for errors btfss status, z call rescue_8 ;rewrite reg8 return rescue_8 movlw 0x20 ;location 0x20 movwf fsr movlw b'00000000' ;lsn of write to switches, required for each write movwf indf incf fsr, f movlw b'00001100' ;msn of write to switches, required for each write movwf indf incf fsr, f movlw b'00001000' ;lsn of enable WE in switches, required for each write movwf indf incf fsr, f movlw b'00000000' ;msn of enable WE in switches, required for each write movwf indf incf fsr, f movlw b'00001000' ;lsn of command to write to 0x08 in bits 0-3 movwf indf incf fsr, f movlw b'00001100' ;msn of command to write to 0x08 in bits 0-3 movwf indf incf fsr, f movlw b'00000010' ;lsn of command for location 0x08 (C) movwf indf incf fsr, f movlw b'00000000' ;msn of command for location 0x08 (A) movwf indf movlw 0x20 movwf fsr movlw d'8' movwf counter_s call txrpc call pause ;takes 10 msec to write a register return in_byte btfsc rpc, rxr ;check for receive request (clear=yes) goto in_byte ;if no, loop back bcf rpc, rxa ;Accept the request- low=accept d_wait btfss rpc, rxr ;see if request okayed- data present goto d_wait ;loop back until present nop ;time delay to stablize data movf rpc, w ;read least sig. nibble bsf rpc, rxa ;message- "nibble received" andlw b'11110000' ;just want data movwf indf ;save nibble in indirect register swapf indf, f ;move nibble into proper location incf fsr, f ;next memory location decfsz counter_r, f goto in_byte return end