PDA

View Full Version : PIC16F1779 settings for EUSART



Ioannis
- 21st May 2023, 11:04
I have a hard time understanding how to set port output and input for the Tx/Rx of the USART peripheral.

Tried the MCC also but the C code is not easy to understand also.

Any help please on setting the PPS registers? What the heck is xy in the xyPPS?

Ioannis

richard
- 21st May 2023, 11:24
What the heck is xy in the xyPPS?

whatever it needs to be
eg on your chip

if its an input then xy= the input to the module
ie.
RXPPS = 0x09; //RB1->EUSART:RX;
makes rx on portb.1

if its an output then xy= the output port/pin for the module
ie.
RB0PPS = 0x24; //RB0->EUSART:TX;
makes tx on portb.0

tumbleweed
- 21st May 2023, 11:42
See the datasheet sect 12.8 (this can be different for each device)

To map the input use the register shown in 12-1 'xxxPPS peripheral input selection' along with Table 12-1
To map the EUSART RX, xxxPPS=RXPPS... that's the register you need to set.
Look back at 12-1 to get the PORT and PIN setting.
For example, to select PORTB pin 4 the setting would be '--001100', so
RXPPS = $0C

To map the output use the register shown in 12-2 'RxyPPS output source selection register'
along with Table 12-2. 'x' is the PORTx (ie 'A','B', 'C') and 'y' is the pin '0'-'7' (ie 'RB5')
To map the EUSART TX, the output signal in Table 12-2 is TX/CK which shows RxyPPS = 100100, so
RB5PPS = $24

tumbleweed
- 21st May 2023, 11:58
Also, this is all enabled by the PPSLOCKED bit in the PPSLOCK register, which defaults to 0 (unlocked).
To set/clear PPSLOCKED you have to follow the sequence shown in sect 12-4 exactly.
There is also a CONFIG PPS1WAY setting in CONFIG2 which, if set, only allows you to change it once.

I'd recommend setting CONFIG PPS1WAY = OFF and not setting the PPSLOCKED bit. That way you don't have to bother with it.

Ioannis
- 21st May 2023, 19:15
Thank you Richard and tumbleweed.

That clears things.

The versatility of this system is great and helps very much in the PCB layout.

Ioannis

Ioannis
- 24th May 2023, 22:02
Seems that I did not quite understood that PPS thing...

I set the registers as:

RD2PPS=%00011010 for RD2 as UART output
and
RXPPS=%00011011 for RD3 as UART RX input

Hserout does not spiting anything out. Code follows:



#CONFIG
__config _CONFIG1, _FOSC_INTOSC & _WDTE_ON & _PWRTE_ON & _MCLRE_OFF & _CP_OFF & _BOREN_ON & _CLKOUTEN_OFF & _IESO_ON & _FCMEN_ON
__config _CONFIG2, _WRT_OFF & _PPS1WAY_OFF & _ZCD_OFF & _PLLEN_ON & _STVREN_ON & _BORV_LO & _LPBOR_OFF & _LVP_OFF
#ENDCONFIG


OSCCON=%11110000

DEFINE OSC 32

LATA = %00000000
LATB = %00000000
LATC = %00000000
LATD = %00000000
LATE = %00000000

TRISA = %00100011
TRISB = %00010011
TRISC = %00000000
TRISD = %00001000
TRISE = %00000000

;************************************************* ********************
; ADC Settings
;************************************************* ********************
ansela=3
wpua=0
anseld=0
WPUD=0
ODCOND=0
ADCON0=%00000001
ADCON1=%10110000
ADCON2=%00000000

cm1con0=0
cm2con0=0
cm3con0=0
cm4con0=0
cm5con0=0
cm6con0=0
cm7con0=0
cm8con0=0

cm1con1=0
cm2con1=0
cm3con1=0
cm4con1=0
cm5con1=0
cm6con1=0
cm7con1=0
cm8con1=0

cmout=0


;************************************************* ********************

' Set LCD Data port
DEFINE LCD_DREG PORTD
' Set starting Data bit (0 or 4) if 4-bit bus
DEFINE LCD_DBIT 4
' Set LCD Register Select port
DEFINE LCD_RSREG PORTD
' Set LCD Register Select bit
DEFINE LCD_RSBIT 0
' Set LCD Enable port
DEFINE LCD_EREG PORTD
' Set LCD Enable bit
DEFINE LCD_EBIT 1
' Set LCD bus size (4 or 8 bits)
DEFINE LCD_BITS 4
' Set number of lines on LCD
DEFINE LCD_LINES 2
' Set command delay time in us
'DEFINE LCD_COMMANDUS 1500
' Set data delay time in us
'DEFINE LCD_DATAUS 250

DEFINE ADC_BITS 10 ' Set number of bits in result
DEFINE ADC_SAMPLEUS 50 ' Set sampling time in uS
'DEFINE ADC_CLOCK 3

'************************************************* ***********
' UART Setup
'
DEFINE HSER_RC1STA 90h
DEFINE HSER_TX1STA 24h '9600 baud rate, BRGH=1
DEFINE HSER_BAUD 9600
define HSER_SPBRG 51
DEFINE HSER_CLROERR 1

pause 100
t1con=0

wADC1 var word

DAC1CON0=%10100000 'Enable,right just.,Output enable,Vref
DAC2CON0=%10100000 'Enable,right just.,Output enable,Vref

DAC1REFH=0
DAC1REFL=0
DAC2REFH=0
DAC2REFL=0
DACLD=%00000011

'************* RD2 = UART TX out
RD2PPS=%00011010

'************* RD3 = UART RX input
RXPPS=%00011011


lcdout $fe,$1,"Test"
pause 1500

clear


while 1

adcin 0,wADC1
lcdout $fe,$80,#wADC1," "
toggle latb.5 'LED hertbeat
DAC1REFL=wADC1.lowbyte
DAC1REFH=wADC1.highbyte
DAC2REFL=wADC2.lowbyte
DAC2REFH=wADC2.highbyte
DACLD=%00000011
hserout ["DAC1:",#wADC1,13,10]
pause 500

wend

end


The RD2 pin is the output to the RS-232 driver and RD3 the input from driver. The LED is flashing at the proper rate, so program is looping. But no serial output.

Ioannis

tumbleweed
- 24th May 2023, 22:34
If you look at Table 12-1 Port Selection and Table 12-2 Available Ports For Output you'll find that the EUSART can't be mapped to PORTD, only PORTB and PORTC.

There are restrictions like this in many of the devices... the PPS matrix isn't a full x-y mux and you usually only get a choice of two ports to pick from.

Ioannis
- 25th May 2023, 09:19
Ooops...

Thanks,
Ioannis

mpgmike
- 25th May 2023, 14:45
If I may make a suggestion; I noticed when you were trying to use PORTD for UART, you initialized your TX Pin low (LATD = 0). I initialize my UART TX Pin high, as that is the idle state. Secondly, your WPUD didn't take advantage of the ability to bias your RX Pin high as well. You may get a false Start of Signal reading when your companion device initializes. Using a Weak Pull-Up on RX reduces that chance.

Ioannis
- 25th May 2023, 19:33
Thank you Mike for the suggestions. Indeed this is the proper way to initialize the ports. At the moment that was the least of the problems.

The important thing is to read carefully the datasheet and as tumbleweed noted, I was setting UART on wrong ports.

Now that I paid more attention and am really awake(!), it works as it should and all is fine.

The people in this forum are one of a kind. Thank you all.

Ioannis