+ Reply to Thread
Results 1 to 6 of 6

Thread: Need help with PIC USART programming

  1. #1
    davepusey Newbie
    Join Date
    May 2004
    Location
    United Kingdom
    Posts
    140

    Need help with PIC USART programming

    Hi

    I've got my circuit wired up like this so far...

    The red wire acts as a loopback (for testing) so that anything i send out the serial port on my computer, i get straight back again.

    This is working perfectly.



    The plan is to remove the red link, and connect the TTL level signals to the USART of a PIC16F627 like this...



    The question is... How do I program the PIC to send and receive data?
    Dave Pusey.
    PIC Programming Tutorials: http://www.mstracey.btinternet.co.uk...al/picmain.htm
    Brilliant PIC Simulator: http://www.oshonsoft.com/pic.html


  2. #2
    Super Moderator Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent
    Join Date
    Nov 2003
    Location
    Derbyshire, UK
    Posts
    29,372

    Check my PIC RS232 tutorials, most use a software UART (so it can be used on any pin), but the last one shows how to use the hardware UART.
    PIC programmer software, and PIC Tutorials at:
    http://www.winpicprog.co.uk

  3. #3
    davepusey Newbie
    Join Date
    May 2004
    Location
    United Kingdom
    Posts
    140

    Ok - a couple of questions...

    1) What is the purpose of the test/loop in this code block?

    Code:
    XMIT_RS232  	btfss   PIR1,	TXIF 	      ;xmit buffer empty?
         		GOTO	XMIT_RS232            ;no, wait
         		MOVWF   TXREG		      ;now send
                    RETURN
    
    2) Is there a way to receive data via an interrupt?

    I dont really want to keep polling the usart when it wont be used that often.
    Dave Pusey.
    PIC Programming Tutorials: http://www.mstracey.btinternet.co.uk...al/picmain.htm
    Brilliant PIC Simulator: http://www.oshonsoft.com/pic.html

  4. #4
    Super Moderator Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent
    Join Date
    Nov 2003
    Location
    Derbyshire, UK
    Posts
    29,372

    Quote Originally Posted by davepusey View Post
    Ok - a couple of questions...

    1) What is the purpose of the test/loop in this code block?

    Code:
    XMIT_RS232  	btfss   PIR1,	TXIF 	      ;xmit buffer empty?
         		GOTO	XMIT_RS232            ;no, wait
         		MOVWF   TXREG		      ;now send
                    RETURN
    
    As the comments say, it's waiting until the transmit buffer is empty - it's the standard way of using the UART - you don't want to overwrite the buffer while it's still been used.


    2) Is there a way to receive data via an interrupt?

    I dont really want to keep polling the usart when it wont be used that often.
    Yes, you can set the hardware UART to generate an interrupt when data is received, or you can occasionally poll the interupt flag to see if data is received.
    PIC programmer software, and PIC Tutorials at:
    http://www.winpicprog.co.uk

  5. #5
    davepusey Newbie
    Join Date
    May 2004
    Location
    United Kingdom
    Posts
    140

    Quote Originally Posted by Nigel Goodwin View Post
    Yes, you can set the hardware UART to generate an interrupt when data is received
    Do you have an example of this please?
    Dave Pusey.
    PIC Programming Tutorials: http://www.mstracey.btinternet.co.uk...al/picmain.htm
    Brilliant PIC Simulator: http://www.oshonsoft.com/pic.html

  6. #6
    chuddleston Newbie
    Join Date
    Jul 2009
    Posts
    17

    Fr. Tom McGahee

    Check out the PICUART.zip example. Enable the UART interrupt and configure the TRIS register correctly for the RX and TX pins, then when a byte is received the code will automatically jump to ORG h'0004'.

+ Reply to Thread

Similar Threads

  1. usart with pic
    By neelam29 in forum Micro Controllers
    Replies: 19
    Latest: 18th July 2009, 09:39 AM
  2. USART with PS/2
    By andonur in forum Micro Controllers
    Replies: 1
    Latest: 11th March 2009, 12:40 AM
  3. Usart + Adc
    By Overclocked in forum Micro Controllers
    Replies: 5
    Latest: 28th January 2009, 06:35 PM
  4. PIC USART: Help Please
    By mortuzahasan in forum Micro Controllers
    Replies: 2
    Latest: 11th February 2007, 07:28 PM
  5. HELP!!! nothing going through my USART!!!
    By Guyver in forum Micro Controllers
    Replies: 8
    Latest: 6th May 2004, 09:11 AM

Tags for this Thread