News:

Let's find out together what makes a PIC Tick!

Main Menu

PIC18F57Q43 and serial comms

Started by kcsl, Today at 10:38 AM

Previous topic - Next topic

kcsl

I'm trying to configure the 18F57Q43 for serial comms. Initially I tried and failed for UART1, but that's a complicated UART with lots of features and I only require basic serial, so I moved to UART2 and I just cannot get it to work either.

This is the code I have:

Declare HRSOut2_Pin = PORTE.1
Declare HRSIn2_Pin  = PORTE.0
Declare Hserial2_Baud = 9600

ANSELE.1 = 0
ODCONE.1 = 0
TRISE.1 = 0
RE1PPS = $23

loop:
    Toggle LED1
    HSerOut2["QWERTY "]
    DelayMS 500
    GoTo loop


I've tried without and with setting some of the configuration bits manually (as above) but that's not helped.
Can somebody spot what stupid mistake I've made please ?

Regards,
Joe
There's no room for optimism in software or hardware engineering.

okmn

#1
Hi,

The mistake is the pin choice, not the code.

CAUSE
On the 18F57Q43 the PPS cannot route a peripheral to any pin. On 28-pin and
larger devices each peripheral can only use pins on two specific ports
(datasheet DS40002147F, section 21.1, p.337). For the 48-pin device:

  UART2 TX  (RxyPPS = $23) -> PORTB or PORTD only  (Table 21-2, p.340)
  UART2 RX  (U2RXPPS)      -> PORTB or PORTD only  (Table 21-1, p.339)
  UART1 TX/RX              -> PORTC or PORTF only

PORTE is not available for UART1 or UART2, so "RE1PPS = $23" connects nothing.
That is also why UART1 failed. UART1 is not harder for basic serial: all five
UARTs on the Q43 are the same module; UART1 just adds LIN/DMX/DALI (p.4).

SOLUTION
Move UART2 to PORTD (or PORTB, but avoid RB6/RB7 as they are ICSP pins).
Clear ANSEL on the RX pin so its digital input buffer works (p.337).

U2RXPPS value: PORT bits = 011 (PORTD), PIN bits = 001 (RD1)
-> %011001 = $19 (register format, p.344)

Also note: "Loop" is a reserved word in Positron, so it cannot be used as a
label. Use Do...Loop or a different label name.

CODE (compiles without errors; please check on hardware)

Device = 18F57Q43
Declare Xtal = 64                    ' Set to your real clock speed

Declare HRSOut2_Pin  = PORTD.0      ' UART2 TX on RD0
Declare HRSIn2_Pin    = PORTD.1      ' UART2 RX on RD1
Declare Hserial2_Baud = 9600

Symbol LED1 = LATD.2                ' Change to your LED pin

ANSELD.0 = 0 : ANSELD.1 = 0          ' Digital mode for TX/RX pins
TRISD.2  = 0                        ' LED pin as output
RD0PPS  = $23                      ' RD0 <- UART2 TX
U2RXPPS  = $19                      ' UART2 RX <- RD1

Do
    Toggle LED1
    HRSOut2 "QWERTY "
    DelayMS 500
Loop

If you prefer UART1: use RC6 for TX (RC6PPS = $20). RX defaults to RC7
(U1RXPPS) at power-up, so it needs no PPS line.

Regards

kcsl

Hi okmn,

Thanks for that.
I was just about to reply to my own message - I'd missed it the first time but table 3-2 clearly shows that PORTE can't be used for UART operations.

This is what happens when you move to a new PIC in a hurry and start making assumptions :(

Ohhh well, out comes the scalpel and start cutting tracks and adding some jumper wires.

Regards,
Joe
There's no room for optimism in software or hardware engineering.

xvovanx

#3
Hello!

If you use software UART, but not hardware UART, you can use any pin in format
SerOut PORTC.0\PORTC.1,N9600,[N]
SerIn PORTD.0\PORTD.1,N9600,1,NO_TXRX,[N]

▲