Swordfish Guide - PICKit 2 and UART

swordfish

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.

PIC20to20PICKit20220to20PC1


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

PICKit20220Select20UART20Tool1

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

PICKit20220Select20Baud20Speed1

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.

PICKit20220Select20Echo20off1

 

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

Echo Program
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 utilising the built in 8Mhz oscillator to minimise 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;

img3621

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;

PICKit20220PIC20Micro20Pinout20UART1

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.

 

Video Guides

Simulation

What are ASCII Characters

Forum Activity

  • No posts to display.

Member Access