Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
Thread Tools Display Modes
Old 10th May 2008, 07:51 AM   (permalink)
Default PICKit 2 USART example

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


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




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.


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

Code:
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;


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;


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
__________________
Spency.

PIC Micro's - Your mind is the limit

PIC's and interfacing with other devices - a PIC Basic Guide @ digital-diy.net
gramo is offline   Reply With Quote
Old 10th May 2008, 07:52 AM   (permalink)
Default

Constructive comments are more then welcome
__________________
Spency.

PIC Micro's - Your mind is the limit

PIC's and interfacing with other devices - a PIC Basic Guide @ digital-diy.net
gramo is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Latest
Drive 33 Servos With One PIC + USART wschroeder Micro Controllers 40 14th July 2008 02:22 PM
PIC18F252 USART problem mikesmixes777 Micro Controllers 8 18th December 2007 02:27 PM
RF receive using PIC USART col_implant Electronic Projects Design/Ideas/Reviews 11 15th November 2007 04:05 PM
Pic 18452 Usart the dude of dudes Micro Controllers 5 14th July 2006 02:51 AM
Usart to Usart? Eclipsed General Electronics Chat 7 23rd October 2003 04:07 PM



All times are GMT. The time now is 06:08 AM.


Electronic Circuits  |  Electronics Wiki
Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.