Hello !
I try to make simple serial communications between PIC 16f876 as transmiter and PIC 16f877 as receiver. But it won't work. I am new to this UART communications so I don't understand where the problem in my code is. Please tell me where the problem is.
The code simply reads PORTB on 16f876 ,takes that data and send it over UART to 16f877 witch turns on and off LED on PORTD in the same order.
The code for transmiter (i.e. 16f876) is :
bank0 macro
bcf 03h,5
bcf 03h,6 ; ram page 0
endm
bank1 macro
bsf 03h,5
bcf 03h,6 ; ram page 1
endm
;---------------------------------------------------------------------------
;bit spen (rcsta<7>) and bits trisc<7:6> have to be set
bank0
bsf 18h,7 ;bit spen (rcsta<7>) set
bsf 07h,6 ; Initialize port C
bank1
movlw 0ffh
movwf 86h
bsf 87h,7
;---------------------------------------------------------------------------
;1. initialize the spbrg register for the appropriate
;baud rate. if a high speed baud rate is desired,
;set bit brgh (section 10.1).
bank1
bsf 98h,2
movlw 0ch ;baud rate = 19,2 k
movwf 99h ;4 Mhz Quartz
;---------------------------------------------------------------------------
;2. enable the asynchronous serial port by clearing
;bit sync and setting bit spen.
bank1
bcf 98h,4
bank0
bsf 18h,7
;---------------------------------------------------------------------------
;3. if interrupts are desired, then set enable bit
;txie .
bank1
bcf 8ch,4
;---------------------------------------------------------------------------
;4. if 9-bit transmission is desired, then set transmit
;bit tx9 .
bcf 98h,6
;---------------------------------------------------------------------------
;5. enable the transmission by setting bit txen
;which will also set bit txif.
bank1
bsf 98h,5
;---------------------------------------------------------------------------
;6. if 9-bit transmission is selected, the ninth bit
;should be loaded in bit tx9d.
;---------------------------------------------------------------------------
;7. load data to the txreg register (starts transmission).
circle
bank0
movf 06h,0 ;byte that I take from PORTB and send it to
movwf 19h ;txreg
bank1
wait_a_moment btfss 98h,1
goto wait_a_moment
goto circle
;---------------------------------------------------------------------------
;8. if using interrupts, ensure that gie and peie
;(bits 7 and 6) of the intcon register are set.
end
;---------------------------------------------------------------------------------
The code for receiver (i.e. 16f877) is:
bank0 macro
bcf 03h,5
bcf 03h,6 ; ram page 0
endm
bank1 macro
bsf 03h,5
bcf 03h,6 ; ram page 1
endm
;---------------------------------------------------------------------------
;bit spen (rcsta<7>) and bits trisc<7:6> have tobe set
bank0
bsf 18h,7
bsf 07h,6
bank1
movlw 00h
movwf 88h
bsf 87h,7
;---------------------------------------------------------------------------
;1. initialize the spbrg register for the appropriate
;baud rate. if a high speed baud rate is desired,
;set bit brgh (section 10.1).
bank1
bsf 98h,2
movlw 0ch ;baud rate = 19,2 k
movwf 99h ;4 mhz
;---------------------------------------------------------------------------
;2. enable the asynchronous serial port by clearing
;bit sync and setting bit spen.
bank1
bcf 98h,4
bank0
bsf 18h,7
;---------------------------------------------------------------------------
;3. If interrupts are desired, then set enable bit
;RCIE..
bank1
bcf 8ch,5
;---------------------------------------------------------------------------
;4. 4. If 9-bit reception is desired, then set bit RX9.
bank0
bcf 18h,6
;---------------------------------------------------------------------------
; 5. Enable the reception by setting bit CREN.
bank0
bsf 18h,4
;---------------------------------------------------------------------------
;6. Flag bit RCIF will be set when reception is complete
;and an interrupt will be generated if enable
;bit RCIE is set.
;---------------------------------------------------------------------------
;7. Read the RCSTA register to get the ninth bit (if
;enabled) and determine if any error occurred
;during reception.
;---------------------------------------------------------------------------
;8. Read the 8-bit received data by reading the
;RCREG register.
vrti
bank0
movf 1ah,0 ;byte that I send to LED on PORTD
movwf 08h ;txreg
bank1
cekaj btfss 0ch,5
goto cekaj
goto vrti
;---------------------------------------------------------------------------
;8. if using interrupts, ensure that gie and peie
;(bits 7 and 6) of the intcon register are set.
end
I try to make simple serial communications between PIC 16f876 as transmiter and PIC 16f877 as receiver. But it won't work. I am new to this UART communications so I don't understand where the problem in my code is. Please tell me where the problem is.
The code simply reads PORTB on 16f876 ,takes that data and send it over UART to 16f877 witch turns on and off LED on PORTD in the same order.
The code for transmiter (i.e. 16f876) is :
bank0 macro
bcf 03h,5
bcf 03h,6 ; ram page 0
endm
bank1 macro
bsf 03h,5
bcf 03h,6 ; ram page 1
endm
;---------------------------------------------------------------------------
;bit spen (rcsta<7>) and bits trisc<7:6> have to be set
bank0
bsf 18h,7 ;bit spen (rcsta<7>) set
bsf 07h,6 ; Initialize port C
bank1
movlw 0ffh
movwf 86h
bsf 87h,7
;---------------------------------------------------------------------------
;1. initialize the spbrg register for the appropriate
;baud rate. if a high speed baud rate is desired,
;set bit brgh (section 10.1).
bank1
bsf 98h,2
movlw 0ch ;baud rate = 19,2 k
movwf 99h ;4 Mhz Quartz
;---------------------------------------------------------------------------
;2. enable the asynchronous serial port by clearing
;bit sync and setting bit spen.
bank1
bcf 98h,4
bank0
bsf 18h,7
;---------------------------------------------------------------------------
;3. if interrupts are desired, then set enable bit
;txie .
bank1
bcf 8ch,4
;---------------------------------------------------------------------------
;4. if 9-bit transmission is desired, then set transmit
;bit tx9 .
bcf 98h,6
;---------------------------------------------------------------------------
;5. enable the transmission by setting bit txen
;which will also set bit txif.
bank1
bsf 98h,5
;---------------------------------------------------------------------------
;6. if 9-bit transmission is selected, the ninth bit
;should be loaded in bit tx9d.
;---------------------------------------------------------------------------
;7. load data to the txreg register (starts transmission).
circle
bank0
movf 06h,0 ;byte that I take from PORTB and send it to
movwf 19h ;txreg
bank1
wait_a_moment btfss 98h,1
goto wait_a_moment
goto circle
;---------------------------------------------------------------------------
;8. if using interrupts, ensure that gie and peie
;(bits 7 and 6) of the intcon register are set.
end
;---------------------------------------------------------------------------------
The code for receiver (i.e. 16f877) is:
bank0 macro
bcf 03h,5
bcf 03h,6 ; ram page 0
endm
bank1 macro
bsf 03h,5
bcf 03h,6 ; ram page 1
endm
;---------------------------------------------------------------------------
;bit spen (rcsta<7>) and bits trisc<7:6> have tobe set
bank0
bsf 18h,7
bsf 07h,6
bank1
movlw 00h
movwf 88h
bsf 87h,7
;---------------------------------------------------------------------------
;1. initialize the spbrg register for the appropriate
;baud rate. if a high speed baud rate is desired,
;set bit brgh (section 10.1).
bank1
bsf 98h,2
movlw 0ch ;baud rate = 19,2 k
movwf 99h ;4 mhz
;---------------------------------------------------------------------------
;2. enable the asynchronous serial port by clearing
;bit sync and setting bit spen.
bank1
bcf 98h,4
bank0
bsf 18h,7
;---------------------------------------------------------------------------
;3. If interrupts are desired, then set enable bit
;RCIE..
bank1
bcf 8ch,5
;---------------------------------------------------------------------------
;4. 4. If 9-bit reception is desired, then set bit RX9.
bank0
bcf 18h,6
;---------------------------------------------------------------------------
; 5. Enable the reception by setting bit CREN.
bank0
bsf 18h,4
;---------------------------------------------------------------------------
;6. Flag bit RCIF will be set when reception is complete
;and an interrupt will be generated if enable
;bit RCIE is set.
;---------------------------------------------------------------------------
;7. Read the RCSTA register to get the ninth bit (if
;enabled) and determine if any error occurred
;during reception.
;---------------------------------------------------------------------------
;8. Read the 8-bit received data by reading the
;RCREG register.
vrti
bank0
movf 1ah,0 ;byte that I send to LED on PORTD
movwf 08h ;txreg
bank1
cekaj btfss 0ch,5
goto cekaj
goto vrti
;---------------------------------------------------------------------------
;8. if using interrupts, ensure that gie and peie
;(bits 7 and 6) of the intcon register are set.
end