UART between 6f877 and 16f877

Status
Not open for further replies.

PIC2PIC

New Member
I am trying to communicate two 16f877 .
It works like this:
PIC1: It reads PORTB and sends that data to PIC2, then it reads RCREG to see if other PIC2 has sent some data, and sends that data to PORTA.
PIC2: It reads RCREG to see if other PIC2 has sent some data and then reads PORTA and sends that data to PIC1.

But is not working. What is wrong with this code. Can't get it.


The code is :


;---------------------------------------------------------------------------
list p=16f877A ; list directive to define processor
#include <p16f877A.inc> ; processor specific variable definitions
errorlevel -302, -207
;---------------------------------------------------------------------------


;---------------------------------------------------------------------------
banksel TRISB

movlw 0ffh
movwf TRISB
movlw 00h
movwf TRISA
;---------------------------------------------------------------------------


;---------------------------------------------------------------------------

banksel SPBRG
movlw 0x0C
movwf SPBRG

movlw b'00100100'
movwf TXSTA


bcf PIE1,TXIE
bcf PIE1,RCIE

banksel RCSTA
movlw b'10010000'
movwf RCSTA

;---------------------------------------------------------------------------





Main
call Trans
call Receive

goto Main

Receive
banksel PIR1
btfss PIR1,RCIF
goto Receive
movf RCREG,W
movwf PORTA
return

Trans
banksel PORTB
movf PORTB,W
movwf TXREG

banksel TXSTA

WtHere btfss TXSTA,TRMT
goto WtHere
return

;---------------------------------------------------------------------------





end
 
Actualy this up code is for "Transmitter" i.e. PIC1.

The code for "receiver" i.e. PIC2 is rather same:

;---------------------------------------------------------------------------
list p=16f877A ; list directive to define processor
#include <p16f877A.inc> ; processor specific variable definitions
errorlevel -302, -207
;---------------------------------------------------------------------------


;---------------------------------------------------------------------------
banksel TRISB

movlw 0ffh
movwf TRISA
movlw 00h
movwf TRISB
;---------------------------------------------------------------------------


;---------------------------------------------------------------------------

banksel SPBRG
movlw 0x0C
movwf SPBRG

movlw b'00100100'
movwf TXSTA


bcf PIE1,TXIE
bcf PIE1,RCIE

banksel RCSTA
movlw b'10010000'
movwf RCSTA

;---------------------------------------------------------------------------





Main

call Receive
call Trans

goto Main

Receive
banksel PIR1
btfss PIR1,RCIF
goto Receive
movf RCREG,W
movwf PORTB
return

Trans
banksel PORTB
movf PORTA,W
movwf TXREG

banksel TXSTA

WtHere btfss TXSTA,TRMT
goto WtHere
return

;---------------------------------------------------------------------------





end
 
This might be part of the problem : When setting up USART in a PIC the TX and RX pin need to be configured as input.
 
Last edited:
Bit SPEN (RCSTA<7>) and bits TRISC<7:6> have to
be set in order to configure pins RC6/TX/CK and
RC7/RX/DT as the Universal Synchronous Asynchronous
Receiver Transmitter.

The above is from the datasheet, it appears you only set bit 7
 
The only thing I can see wrong is the lack of any config information.

Mike.
 
Mike, would it still compile, he did not say weither it did or not? Does MPLAB have build in default configs for each chip in the database?
 
It would compile but I think it defaults to RC oscillator and WDT enabled. I suspect the OP has a 4MHz crystal.

Edit, I see what you meant now about bit 7.

Mike.
 
Last edited:
Hi !
Thank you for your assistance.
I set TRISC to be all input. But still is not working.
The reason I don't set config information in the assembly program is that I set fusese in WinPicProg 1.91. and that setting overrides anything I set in assembly program. I set "POWER UP" and "Brown out" as allways and XT as I use crystal, other options are clear.

Is it possible that the problem might be in waiting PICs each others response ?
 
Hi !
Is it possible that the problem might be in waiting PICs each others response ?

That is most likely your problem.

Try,
Code:
Main			 
		call	Trans 
		call	Receive 
		goto	Main 

Receive			 
		banksel	PIR1
		btfss	PIR1,RCIF 
		return
		movf	RCREG,W
		movwf	PORTA
		return	 

Trans			 
		banksel	TXSTA 
		btfss	TXSTA,TRMT 
		return
		banksel	PORTB
		movf	PORTB,W
		movwf	TXREG
		return

Mike.
 
I solve the problem. My code i OK.

But :

1. To use PORTA as digital you must set ADCON1 register, i.e.

movlw b'00000110' ; all analog pins = digital
movwf ADCON1


2. Allso, two wires in my hardware were disconnected - TX and RX ) . What a idiot I am ) Can't help my self in this stupid mistakes ever )

Now it works.
But very important question arises:
I know that is not posible for transmiter to know if receiving side has got the signal , but is there a way for receiving side to know if connection TX-RX is broken, i.e. the transmitting wire of PIC1 TX is not connected to RX of PIC2?
 
Assuming you are not transmitting only 0xFF or 0x00. You could always check if there is a change on the RX and TX pin every now and then with a simple "btfss PORTX,BitX" kind of thing
 
Maybe I'm off with my thinking but, before setting up the USART on the receiving PIC you could poll whether there is a signal on the RX pin.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…