Usart tutorial/code

Discussion in 'Programmer's Corner' started by KCHARROIS, Nov 20, 2012.


    Thread Starter Active Member

    Jun 29, 2012
    Hello everyone,

    Here is my goal:

    Press a button that goes to a pic and transmit that data to another pic to turn on an LED using USART.

    So in the microcontroller world I've been able to teach myself some basic stuff looking through the datasheet such as turning and led on and off, basic interrupts, a/d conversion, etc. The pic I've been using since I started is the pic18F4520. Does anybody have some code theyd like to share or have found a tutorial online that has sample code using usart? The language I've used has always been assembly language.

    Thanks in advance
  2. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    If using assembler it is pretty simple. Please get familiar with the USART chapter of the datasheet, I know it looks complicated at first glance but it is not that hard and is worth reading a couple of times.

    General procedure;
    1. setup USART as asynchronous serial (datasheet has some code)
    2. setup the baudrate
    the above involves writing values to 3 or 4 registers.

    Then detecting an incoming byte is automatic, the USART sets a flag bit when a byte ie received;
    1. if the flag is set we received a byte
    2. read the byte from register RXREG
    3. if that byte = blah then turn light ON
    4. if that byte = bleh then turn light OFF

    Both processes ie setting up the USART and reading a byte are only a handful of assember instructions each so even with someone elses source code you still need to have a good read of the USART chapter of the datasheet, as you will still need to set your own baudrate and options etc for your own project. :)