Continue to Site

# 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

goto Main

banksel PIR1
btfss PIR1,RCIF
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 Trans

goto Main

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

Trans
banksel PORTB
movf PORTA,W
movwf TXREG

banksel TXSTA

WtHere btfss TXSTA,TRMT
goto WtHere
return

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

end

#### PIC2PIC

##### New Member
What ? Nobody ? Ok.

#### birdman0_o

##### Active Member
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:

#### birdman0_o

##### Active Member
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

The above is from the datasheet, it appears you only set bit 7

#### Pommie

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

Mike.

#### Pommie

##### Well-Known Member
The above is from the datasheet, it appears you only set bit 7

I read that as both C6 and C7 have to be input.

Mike.

#### birdman0_o

##### Active Member
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?

#### birdman0_o

##### Active Member
I read that as both C6 and C7 have to be input.

Mike.

What I meant is that he only had set bit 7, and from what I posted both bit 6 and 7 need to be set. Sorry for the ambiguity.

#### Pommie

##### Well-Known 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 !
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
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
goto	Main

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

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

#### PIC2PIC

##### New Member
@birdman0_o

Not quite understand ? Please explain.

#### birdman0_o

##### Active Member
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.

Replies
7
Views
992
Replies
4
Views
829
Replies
9
Views
1K
Replies
12
Views
2K
Replies
15
Views
2K