PIC uart basics


Closed Thread
Results 1 to 8 of 8

Thread: PIC uart basics

  1. #1

    Default PIC uart basics

    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.

  2. #2
    Join Date
    Dec 2003
    Location
    Storrs, Ct.
    Posts
    91


    Did you find this post helpful? Yes | No

    Default Some help for you.

    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
    "It will never happen here!" just happened here.
    My thoughts and prayers for Sandy Hook victims and families.

  3. #3


    Did you find this post helpful? Yes | No

    Default

    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

  4. #4
    Join Date
    Dec 2003
    Location
    Storrs, Ct.
    Posts
    91


    Did you find this post helpful? Yes | No

    Default

    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.
    "It will never happen here!" just happened here.
    My thoughts and prayers for Sandy Hook victims and families.

  5. #5


    Did you find this post helpful? Yes | No

    Default

    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?

  6. #6
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    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.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  7. #7


    Did you find this post helpful? Yes | No

    Default

    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.

  8. #8
    Join Date
    Dec 2003
    Location
    Storrs, Ct.
    Posts
    91


    Did you find this post helpful? Yes | No

    Default

    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.
    "It will never happen here!" just happened here.
    My thoughts and prayers for Sandy Hook victims and families.

Similar Threads

  1. SMS via pic
    By kenandere in forum GSM
    Replies: 15
    Last Post: - 10th March 2010, 10:00
  2. Replies: 67
    Last Post: - 8th December 2009, 02:27
  3. HSERIN & Interupts (aka controlling PIC programs from a remote PC)
    By HankMcSpank in forum mel PIC BASIC Pro
    Replies: 16
    Last Post: - 17th June 2009, 14:46
  4. pic to pic ir link versus wired link : help please anyone
    By xnihilo in forum mel PIC BASIC Pro
    Replies: 13
    Last Post: - 30th May 2008, 21:01
  5. Serial Pic to Pic using HSER
    By Chadhammer in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 11th March 2005, 23:14

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts