Menu

Hardware USART lesson / direction needed

Help
graindart
2011-03-21
2013-05-30
  • graindart

    graindart - 2011-03-21

    I've been messing around GCBasic for the past week or two and have figured out most things that I needed, however can't figure out the steps to be able to utilize the hardware usart on my PIC.  I've tried searching the forums and reading up on the help files, but can't seem to find enough information to get things working properly.

    If someone could help shove me in the right direction it would be highly appreciated.  I'm just trying to send a single digit number to the PIC from a computer terminal, have the PIC receive it and then send it back to the terminal so that I know the communication is working.  I'm sure I've got the code all messed up, but here's what I've got so far:

    ;Chip Settings
    #chip 16F690,4
    ;Defines (Constants)
    #define USART_BAUD_RATE 9600
    ;Variables
    Dim testdata As integer
    'Variables
    'USART settings
    'Set USART receive pin to input
    Dir PORTB.5 In
    'Set USART transmit pin to output
    Dir PORTB.7 Out
    InitUSART
    'Main loop
    Do Forever
        HSerReceive testdata
        HSerPrint testdata
        'Short delay for receiver to process message
        Wait 10 ms
    Loop
    
     
  • Mauried

    Mauried - 2011-03-21

    You are on the right track but if you have a hardware uart, its better to use the interrupt capability
    that the uart provides, so that you only check for a received character when there is actually one available.

    For example.
    #define USART_BAUD_RATE 9600
    On Interrupt UsartRx1Ready Call Rx 
    Initusart

    Main
    do
    loop

    Sub Rx
    Hsereceive TestData
    Hsersend TestData
    end sub

    In this case, the code only checks for a received character when there is one
    in the uart FIFO, and the echoes the char back.
    This allows you to put whatever other code you need in the main loop to do whatever you need.
    With code like this , you need to be aware of how long it takes for the interrupt routine to run and for a simple application
    when the data is coming in on a char by char basis it will work.
    But if a big data stream comes in then the above code wont work as the time it takes to send the character back will
    start to overlap with the time it takes to receive the next one.
    To fix this is a bit more complex , and it requires a dedicated rx interrupt routine to do the receiving and a separate one
    to do the transmitting.

     
  • graindart

    graindart - 2011-03-22

    Thanks for the reply about using the interrupt capability.  Unfortunately I must still be missing something somewhere. 

    I copied your example code directly into Great Cow Graphical Basic and tried compiling it.  The Compiler states error in Main at 2: Syntax Error and Error in Rx at 1: Syntax Error.

    Am I supposed to add some of my previous code to the code you listed to get it to work or should your example code compile correctly as is?

     
  • graindart

    graindart - 2011-03-22

    I got your example code to compile by removing the word "Main" and adding a 2nd letter "R" to HserReceive.

     
  • Mauried

    Mauried - 2011-03-22

    Yes, srry, a few typos when I typed in the example.
    The Main is just a comment to show where the program sits when no serial data is coming in
    and I learn to spell HserReceive properly.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.