1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

PICKit 2 USART example

Discussion in 'Microcontrollers' started by gramo, May 10, 2008.

  1. gramo

    gramo New Member

    Joined:
    Oct 2, 2006
    Messages:
    1,221
    Likes:
    23
    Location:
    Newcastle, Australia
    Hi guys,

    I get asked a lot how to communicate either PIC to PC and vise versa, so I put together a guide for people on how to interface with their PICKit 2 via USART. Almost all PIC's come with some sort of USART onboard, and if not, then every compiler has a software equivalent ready to bit bang data out.

    I use this method extensivly for debugging and interfacing with programs as its only a couple of wires and you can send/receive data from your PIC via a number of different routines found in the USART.bas library in Swordfish


    Anyway, here it is, hope it helps someone out :)

    18F PICKit 2 UART

    Communication between you PIC micro and PC could not be easier then with your PICKit 2. Forget the hassles of wiring up your DB9 connectors and worrying about MAX232's or DS275's. don't even worry about connecting an LCD to get real time data from your program... This is the fast, effective and simple solution for real time debugging and user interfacing on your projects.


    Ok, so I hear what you are saying - the PICKit 2 will program almost every PIC known to man kind from 12Fs/16F's/18F's/dsPIC's/24F PIC's, but how do you use it for UART/USART communication with your PC? Well start off by opening the PICKit 2 programming software, and from the Tools menu, select UART Tool...

    [​IMG]
    The PICKit UART tool will open, and the first thing you should do is set the baud that your using...

    [​IMG]



    Your now set to go! One other thing before I cover a simple UART program, the "Echo On" option box enable/disables the transmitted data to be appended to the UART Tool screen, eg, if you typed "Hello World" and then clicked "Send", if enabled it would display in the UART Tool screen.

    [​IMG]

    Let's have a look at a simple program to transmit data from your PIC to your PC;

    Code (text):
    Device = 18F4550
    Clock = 8
    Config FOSC = INTOSCIO_EC
     
    // import usart module...
    Include "usart.bas"

    // setup the internal OSC for 8Mhz
    OSCCON = %01110110

    // read in characters and echo to screen...
    USART.SetBaudrate(br9600)
    While true
       USART.WriteByte(USART.ReadByte)
    Wend
     
    I am using an 18F4550 as it is my PIC of choice at the moment, and also utilizing the built in 8Mhz oscillator to minimize external components. You can use any PIC micro that Swordfish supports, there are plenty of other examples with setting up other PIC's on the site.

    From there, I include the USART.bas library, as it contains all of the routines to handle Hardware UART on PIC's. OSCCON = %01110110 is part of setting up the 18F4550's internal oscillator, the next important part is USART.SetBaudrate(br9600), as it tells the compiler what speed your UART will be operating at, in this case its 9600 baud.

    Essentially the main program is a UART Echo routine - that is, whatever is received is then transmitted. The program will wait for ever if need be until USART.ReadByte returns something, so that USART.WriteByte can then send something.

    Connecting your PIC to the PICKit 2 couldn't be easier, it is even shown on the PICKit 2 UART tool, but here it is again;

    [​IMG]

    You do not need to connect Pin 5 of the PICKit 2 if your just using the UART tool for displaying data from the PIC, leaving you with only three wires to connect. Locating the USART pins on a PIC micro can be found in the datasheet, in either the USART section or the Pinout diagram. The USART pins are usually identified by RX and TX as shown below;

    [​IMG]

    This is extremely handy for thousands of uses, in particular, I use this method extensively as a real time display of what is happening in my program, eg, place a line of code that sends crucial program status information via UART so I can tell exactly what my program is doing, handy.

    UART with your PC via the PICKit 2

    Video Tutorials:

    • UART with your PC via the PICKit 2
    • Raw basics on how to setup and use your PICKit 2 and the UART tool
    • I start from building the circuit and then run through setting up the PICKit 2 software

     
  2. gramo

    gramo New Member

    Joined:
    Oct 2, 2006
    Messages:
    1,221
    Likes:
    23
    Location:
    Newcastle, Australia
    Constructive comments are more then welcome :)
     
  3. astronomerroyal

    astronomerroyal New Member

    Joined:
    Apr 26, 2008
    Messages:
    116
    Likes:
    2
    A million thanks. Several months ago I started learning about uCs from scratch. All i knew was that they were the things that controlled fridges. 'Embedded sytems' was not a phrase I had encountered. At some point I came across your video tutorials which were a great help - very clear and uncomplicated. At the time i think I was confused about the basic of headers and connectors. Today I'm a little more sophisticated but was unsure about how to use USART. Once again I found your tutorial indispensable. I had no idea Pickit 2 software could do this. Reading Microchip's external EEPROM chips is another useful thing it does.

    If you're still making entry-level tutorials it would really very useful for beginners if you had a very basic overview of what uCs are all about; what they are, what they can do, where you might find one, how they differ form a desktop computer, their variety, popular manufacturers, cost, languages etc. Most importantly give the viewer an impression of what is involved in their practical use, assuming no prior knowledge - Grandmother level. Lots of analogies. I found that even beginner books tended to assume that the reader knew what a programmer was.

    Another suggestion - a tutorial about how to use a character LCD, in the style of Julyan Ilett's brilliant 'How to use intelligent LCDs' article. No uC needed, amazing when you consider the LCDs complicated datasheet.

    Anyway, with your pedagogical style the tutorials could be a very valuable resource. I'm amazed no-one has commented on this thread. Please keep up the good work.
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. silvarblade

    silvarblade Member

    Joined:
    Feb 20, 2007
    Messages:
    264
    Likes:
    1
    Location:
    The Motor city,USA

    Hi Gramo,nice to see that you made an other tutorial.I would suggest that you post the links of your video tutorials on your site,that would make it easy for others to learn from your experience since there are only four of your tutorials on the site.
     
  6. gramo

    gramo New Member

    Joined:
    Oct 2, 2006
    Messages:
    1,221
    Likes:
    23
    Location:
    Newcastle, Australia
    I've got the signature link for people to follow, but for a direct link for this guide in particular, click here Spency's digital-diy 18F PIC micro Tutorial - PICKit 2 & UART, USART
     
  7. gramo

    gramo New Member

    Joined:
    Oct 2, 2006
    Messages:
    1,221
    Likes:
    23
    Location:
    Newcastle, Australia
    You know what would be really great - edit access on old threads :rolleyes:

    This content has moved as well, where to? A better place: Swordfish Tutorial - PICKit 2 and UART


    Note the video tutorial links in the first thread will not be active after May 09. The videos are of course available from the new link above
     

Share This Page