gramo
New Member
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 **broken link removed**
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...
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;
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:
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 **broken link removed**
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...
**broken link removed**
The PICKit UART tool will open, and the first thing you should do is set the baud that your using...
**broken link removed**
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.
**broken link removed**
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;
**broken link removed**
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;
**broken link removed**
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
- ASCII - What are ASCII Characters, and how do you use them with UART?
- ASCII characters are very handy with UART, for example, carriage return to indicate a new line