# Serial Comms between 2 PIC18F14K22

Status
Not open for further replies.

#### Hintza

##### New Member
I used a Real Ice system to set one PIC18F14K22 to continuously transmit an 'A' at 1 second intervals. This worked as I can capture the character on my scope. The problem arises when trying to receive this on the second PIC. Both use the same baud rate code and are running at 5V with a common earth. My code is:

;Receive byte at 1200 baud

CLRF SPBRGH
MOVLW D'51'
MOVWF SPBRG ;Baud=1200, 4MHz crystal

BCF TXSTA,BRGH
BCF BAUDCON,BRG16
BCF TXSTA,SYNC
;now have SYNC=0, BRGH=0,BRG16=0

BSF RCSTA,SPEN ;enable serial port
BSF TRISB,RX ;make RX pin an input
BCF RCSTA,CREN
BSF RCSTA,CREN ;enable reception
Loop1
BTFSS PIR1,RCIF ;poll RCIF for high
BRA Loop1

MOVF RCREG,W
NOP
END

I took the PIC that was transmitting the 'A' character and connected it to a Maximite which uses a PIC32MX170 which has a Basic interpreter on board. This received the 'A' . This convinces me that the problem is at the receiving end.

What am I doing wrong when trying to receive a character?

#### jpanhalt

##### Well-Known Member
Most Helpful Member
Welcome to ETO.

I do not use the 18F series, but in the 16F enhanced series the EUSART seems similar. Are you seeing any errors in the receiver? Can you monitor the IF flag in simulation?

Finally, for the 16F1xxx deivces, one needs to read RCREG twice to be sure to clear it. Of course banking is different for the 18F series.

For example (PIC 16F1519):
Code:
CReset
BANKSEL   RCSTA               ;not needed?                            |B3
movf      RCREG,w             ;clear 3rd byte, if present --          |B3
movf      RCREG,w             ;                                       |B3
bcf       RCSTA,SPEN          ;                                       |B3
bsf       RCSTA,SPEN          ;                                       |B3
RETFIE

The snippet is from a draft version. Ignore the comments.

John

#### Hintza

##### New Member
Thanks for the suggestion John. I tried reading RCREG twice but I still cannot receive a byte.
I don't see any errors in the receiver because nothing is received.
The mystery continues.

#### jpanhalt

##### Well-Known Member
Most Helpful Member
From your comment, is it safe to assume that RCIF is not being set? You could light an LED when or if RCIF is set to confirm it.

#### granddad

##### Well-Known Member
I've always had serial problems with PIC18 polling the RXIF , and have resorted to an ISR to read the RCREG and clear the flag. and had to do same with PIC24 , try a faster internal fosc .....

#### MaxHeadRoom78

##### Well-Known Member
Most Helpful Member
Have you read the Picmicro AN774? It has a full explanation for the USART and also links to several examples of code for the 18F.
Max.

Status
Not open for further replies.

Replies
25
Views
3K
Replies
3
Views
1K
Replies
15
Views
4K
Replies
4
Views
4K
Replies
1
Views
1K