PDA

View Full Version : UART questions



Christopher4187
- 16th February 2013, 14:15
I'm using an 18F4550. I have a SPI IC and a UART IC. Most of you who have used a 4550 know that the RX USART pin is shared with the SDO pin (Pin 1). I'm using the MSSP for the SPI communication.

The best solution that I've found on here is to reconfigure the pin for the function that you need. I'd like to try it to see how much it will slow down the transfer of data but I have some questions.

1. If I'm running the SPI and want to switch to the UART, would I do something like:

SSPEN=0
SPEN=1

And then do the reverse when I want to use the SPI again?


2. Can I use other pins for the UART communication? It's not critical data and I only need to check the UART once every 3-4 seconds.

3. Are there any other options available? I know switching PIC's seems like an easy solution, and in the end I may switch to a 18F46J50, but if I can make this work I'd prefer to stick with it.

4. The external UART IC runs at 3.3V. I have the 4550 running at 5V. If I want to interface the UART pins, do I put a current limiting resistor in series?

HenrikOlsson
- 16th February 2013, 14:42
Hi Christopher,
1) In theory yes, I believe that's what you need to do, plus swtiching between input and output. In practice I'm not sure it's going to work since the pin, in "SPI mode", is a driven output which then connects to whatever drives the pin when it's in "UART mode". Ie there's a potential short circuit happening there. Perhaps putting a resistor between the pin and the device connected to it "in UART mode" is enough.

2) No. The on the 4550 the UART pins are RC6 and RC7 and can't be moved. If you can use SEROUT/SERIN you can use (almost) any pin you like.

3) You already know my view on this ;-)

4) Hard to say without looking at the datasheet for the UART chip. Some have 5V tolerant I/O some don't.

/Henrik.

Christopher4187
- 16th February 2013, 15:14
Thanks for the reply Henrik.

In reference to #2, that's what I meant. I know SERIN/OUT is slower, but it won't matter for my application. Is it as simple as just running the UART I/O to the SERIN/OUT and then when I want to send something, I just put "SEROUT pin, mode, blah, blah, blah....." Also, can I or should I use SEROUT2? If I understand it correctly, the only difference is flow control?

In reference to #4, the external UART IC is only 3.3V. It states that it's an 8mA drive, but nothing on the input. When I'm using SERIN, the 4550 should be floating and only accepting the UART data, so 3.3V should be the max and I can connect the input directly. When I'm using SEROUT, the 4550 is driving the data at 5V and I would need a resistor, perhaps 1k?. I've never done this before, is my understanding correct?

prstein
- 16th February 2013, 21:56
Hello Christopher,

I'd be very interested to know which UART chip you are using.

A different approach could be to utilize the UART for the serial communications and use the software SPI commands (SHIFTIN and SHIFTOUT). I would be lost without the PIC's two-byte buffer and interrupt...8v)

Best Regards,
Paul

Christopher4187
- 16th February 2013, 22:30
It's the RN-171. I'm using this code and I can see the data coming out of the RN-171 and into the 4550 but the 4550 isn't doing anything. Am I at least in the ballpark?




DEFINE OSC 48

@ __CONFIG _CONFIG1L, _PLLDIV_5_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
@ __CONFIG _CONFIG1H, _FOSC_HSPLL_HS_1H & _FCMEN_OFF_1H & _IESO_OFF_1H
@ __CONFIG _CONFIG2L, _PWRT_OFF_2L & _BOR_OFF_2L & _BORV_3_2L & _VREGEN_ON_2L
@ __CONFIG _CONFIG2H, _WDT_OFF_2H & _WDTPS_32768_2H
@ __CONFIG _CONFIG3H, _CCP2MX_ON_3H & _PBADEN_OFF_3H & _LPT1OSC_OFF_3H & _MCLRE_ON_3H
@ __CONFIG _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & _ICPRT_ON_4L & _XINST_OFF_4L

CMCON = 7
PORTD.1=0 'diagnostic LED
PORTD.0=0 'diagnostic LED


DEFINE HSER_BAUD 9600
DEFINE HSER_RCSTA 90h
DEFINE HSER_TXSTA 20h
DEFINE HSER_CLROERR 1

in1 var byte

HIGH PORTD.0
HIGH PORTD.1


mainloop:

IF RCSTA.1 = 1 THEN 'if USART overrun error flag OERR is set...
RCSTA.4 = 0 'clear and then set CREN bit to recover
RCSTA.4 = 1
in1 = RCREG 'read data to clear out RCREG
IF PIR1.5 = 1 THEN in1 = RCREG 'clear possible 2nd byte in RCREG
ENDIF


WHILE PIR1.5 = 0 'stay here until USART receives data


WEND
LOW portD.0 'data in RCREG; turn on diagnostic LED <----my code won't go past this point even though I can see data being sent with an oscope.

goto mainloop

Christopher4187
- 17th February 2013, 02:00
Nevermind. Apparently I either need glasses or sleep.

prstein
- 17th February 2013, 02:02
Hello Christopher,

I see a couple of things. To make the USART work on the '4550 the manual says that both the RX and TX pins need to be configured as inputs, so add

TRISC.7 = 1
TRISC.6 = 1

If you're having trouble, try remming out the first six lines under mainloop, from the if to the endif, just to confirm that the port is working asynchronously.

For synchronous operations, TRISC.7 needs to be set to 0.

Best Regards,
Paul

Christopher4187
- 17th February 2013, 21:47
I have everything working for the most part. It's a little buggy but hopefully I can sort that out over time. I have a question about sorting the data. The data comes to me in this form:


*DATA* 84 324 2343 211 2345 234 DONE
ID: 24

I want to take the six numbers on the first line and assign variables to them and then do stuff with them. How can I do that? Also, if I was to push out 100 variables in one shot, would that work or is it considered normal practice?

Christopher4187
- 18th February 2013, 16:49
One other question. When I'm using SPI, my settings are as follows:
SSPSTAT = %01100100
SSPCON1 = %00100001
BAUDCON = %00001111
When I switch to UART, I do the following:
SELECT case TIMER
CASE 255
SSPSTAT = 0
SSPCON1 = 0
BAUDCON = 0
PAUSE 1000
for x=1 to 10
HSEROUT ["FAN: ",DEC FAN]
pause 10
next x
SSPSTAT = %01100100
SSPCON1 = %00100001
BAUDCON = %00001111
pause 100
END SELECT
Using one or the other (initial programming), it works correctly. When I use the code above, the data goes out but it's garbled. Instead of the fan information, I get stuff like "* "

Any ideas?