PDA

View Full Version : PIC uart basics



Michael
- 21st September 2008, 18:26
Some of the PICs have an onboard UART and I'm not familiar with implementing it.

So, some very basic questions to save some time...

This uart will work in the background continuously while my code is executing?

What are the commands I should be looking at?.....is the HSERIN and HSEROUT? (So, if I use HSEROUT, it starts the uart and it will keep running?).

Any other special things I should know about setting up a register or something?

THANKS.

I've always used the software RS232 on my PIC projects but time to move up and do it the smart way.

ronjodu
- 21st September 2008, 23:56
I just went thru this last weekend. Here's the thread. Hope it helps.
Set-up the usart in the Defines.


http://www.picbasic.co.uk/forum/showthread.php?t=9567

Michael
- 22nd September 2008, 16:35
Love that George Carlin quote.

I replied to that thread like a moron.

let's try again --



From what I see it's mostly a matter of setting up the defines and running getbytes?

Is this the barebones of what I need to get data in/out of serialdata?

As long as data is at portc.7 it continuously runs in the background as an input, loading serialdata while any other code is executing?

I can then sample serialdata when I need to in my code?


DEFINE LOADER_USED 1
DEFINE OSC 4
DEFINE HSER_RCSTA 90h ' enable serial port,
define HSER_TXSTA 24h ' enable transmit,
define HSER_SPBRG 25 ' set baudrate to 9600
DEFINE HSER_CLOERR 1 ' automatic clear overrun error

TRISC = %10000000 ' PORTC.7 is the RX input, PORTC.6 is the TX output


' Serial communication definition


Getbytes:

While RCIF = 1 ' clear the buffer
HSERIN 100,error,[Serialdata] ' take it
hserout [serialdata] ' send it
Wend

ronjodu
- 22nd September 2008, 21:17
The way I wrote the program it is interrupt driven. A byte is received at the usart and a receive interrupt is generated. The program jumps to Getbytes: to service the interrupt for as long as there's bytes coming in then goes back to the original program.

For what little testing I've done with it so far it works great. Eventually it will become part of a larger program, that's why I went with the interrupt option.

Be careful with the DEFINEs also, I think they should be all caps.
Hope this helps some.

Michael
- 23rd September 2008, 13:38
I saw that it was interrupt driven but I want something that just runs in the background continuously and I'll just sample the serildata when I need to.

I'm just trying to get a head start before I sit down and play with it, but the code as shown, will get me close? (or do the job).

I left out all the references to the interrupt....looks like that's pretty much it?

Bruce
- 24th September 2008, 16:13
The hardware UART will buffer up to 2 incoming bytes automatically. Just set it up, enable
it, and read data from the buffer before the 3rd byte is received.

Michael
- 27th September 2008, 17:30
So it does or does not work in the background?

What I mean is once you write a HSERIN command it will always write to the register you specify?....then just sample the register when you want?

Or will it only write to the registers once and wait till you do another HSERIN? (or Hserout, whatever the case may be).

I was under the impression the hardware Uart works like that....independently.

?

Thanks.

ronjodu
- 27th September 2008, 19:11
I also have been toying with the USART and I think had a misconception of what "in the background" meant. I've come to believe it means the USART will suck in two bytes at any time they arrive as long as the buffer is empty. This gives you some time to get there and read them, thus emptying the buffer for another two bytes to arrive. I use the Receive interrupt because I'm receiving 5 bytes at a time. If your program is fast enough I would think you could just poll the RCIF and grab a byte at a time.

I could be way off here but I'm still learning too.