Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

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
 

PIC2PIC

New Member
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
 

Pommie

Well-Known Member
Most Helpful Member
The only thing I can see wrong is the lack of any config information.

Mike.
 

Pommie

Well-Known Member
Most Helpful Member
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:

PIC2PIC

New Member
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 ?
 

Pommie

Well-Known Member
Most Helpful Member
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.
 

PIC2PIC

New Member
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?
 

birdman0_o

Active Member
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 :)
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top