Jump to content


View Other Content



Search Articles



Recent Comments


* * * * *

Using the Microchip PIC18 USB CDC Framework




Introduction

This tutorial explains in detail how to get the Microchip PIC18 USB CDC Framework up and running for a PIC18F4550 or PIC18F2550 (The PIC18F2550 is exactly the same as the PIC18F4550, except that it has fewer pins). However, following the steps, but inserting your devices configuration instead of the one given, you should be able to configure the framework for any PIC18 microcontroller.

If you have ever tried communicating between a PC and PIC microcontroller, you've probably discovered that there are basically two different ways of sending data back and fourth:
  • Through a serial port
  • Using a USB port
Although USB stands for "Universal Serial Bus," when I say "serial port" I am referring to the DE9 (commonly called DB9) connector that looks like this:


Posted Image

According to Wikipedia,

Quote

While such interfaces as Ethernet, FireWire, and USB all send data as a serial stream, the term "serial port" usually identifies hardware more or less compliant to the RS-232 standard
Serial and USB both have advantages and disadvantages:

Serial
Advantages:
  • Very easy to use from a software standpoint
  • More widely used than USB for hobby projects
Disadvantages:
  • Newer computers do not have serial ports
  • A USB-to-serial adapter must be used (I recommend this one, or this one)
  • A MAX232 level shifter must be used to convert the signals from 0V through 5V (microcontroller) to -12V through 12V (PC).
USB

Advantages:
  • All computers have several USB ports, so USB is much more convenient for users
  • Data transfer speeds can be much higher
  • Serial is becoming more obsolete as time goes on, but USB continues to be developed
Disadvantages:
  • Much harder to use than serial from a software point of view - A USB stack must be used on the microcontroller, and this is very complicated if being written from scratch (only a few people have done it)
  • Depending on the data transfer type you choose, a device driver may be required for the PC, and drivers are not easy to write
It's up to you to make to decide whether serial or USB works best for your project. As mentioned above, serial is much easier to use from a software point of view, but requires additional hardware (the level shifter and USB to serial adaptor). USB is much more convenient to use, but the software is extremely difficult to write from scratch. Should you decide USB is the right choice for your project, there are thankfully several free USB stacks available for PIC18 microcontrollers. One of these USB stacks is the Microchip USB Framework for PIC18, PIC24 & PIC32 microcontrollers, and that's what we are going to be using in this tutorial.

It's also worth noting that although the microcontroller software is much more complex when using USB, if we use the USB Communications Device Class (USB CDC) then the PC will interact with the microcontroller as if it was connected over a serial port. This means that we don't have to write a driver for our device, or learn how to communicate with a USB device from the PC; all we have to do is read and write data to the virtual COM port.



Downloading and installing the framework

The first thing you need to do is download the Microchip Application Libraries. After downloading the Microchip Application Libraries, which contains the USB Framework, you will need to install it.

If you left the default installation path, then you can find the USB CDC Framework in the folder

C:\Microchip Solutions v2011-06-02\USB\Device - CDC - Basic Demo
Copy the Firmware folder to another place on your computer, and rename it with your project name. I'll call it "Microchip PIC18 USB CDC Framework". There are a lot of workspace, project, and header files in this folder that don't need to be there. Delete all of them except for the "USB Device - CDC - Basic Demo - C18 - PICDEM FSUSB" files. Assuming you are using MPLAB 8, you can also delete the "MPLAB.X" folder.

Now open the project workspace (USB Device - CDC - Basic Demo - C18 - PICDEM FSUSB.mcw) and try building the project. You probably will get the following errors:

Quote

\Microchip PIC18 USB CDC Framework\usb_descriptors.c:157:Error [1027] unable to locate './USB/usb.h'
\Microchip PIC18 USB CDC Framework\usb_descriptors.c:158:Error [1027] unable to locate './USB/usb_function_cdc.h'
Halting build on first failure as requested.
We need to copy files to our project folder to avoid these errors (and others). Create a folder named "USB" inside your project folder. Now, browse to

C:\Microchip Solutions v2011-06-02\Microchip\Include\USB
and copy the following files to the folder you just created:

usb.h
usb_ch9.h
usb_common.h
usb_device.h
usb_function_cdc.h
usb_hal.h
usb_hal_pic18.h

Copy the following files from the given directories to your project directory:

C:\Microchip Solutions v2011-06-02\Microchip\Include
Compiler.h

C:\Microchip Solutions v2011-06-02\Microchip\USB\CDC Device Driver
usb_function_cdc.c

C:\Microchip Solutions v2011-06-02\Microchip\USB
usb_device.c
usb_device_local.h

C:\Microchip Solutions v2011-06-02\Microchip\Include
GenericTypeDefs.h

Go back to MPLAB, expand the "USB Stack" file folder in the file browser, and both "usb_device.c" and "usb_function_cdc.c" will have "(file not found)" next to them. Right click on "usb_device.c", choose "Locate Missing File," select "Use a file of my own choosing," click "Browse" and find the file in your project directory. You will need to do this for any other files you want to edit, as well.

Open "usb_device.c" and scroll to line 247:

#include "../USB/usb_device_local.h"
Change it to:
#include "./usb_device_local.h"

Try building the project again and you should get the message:

Quote

BUILD SUCCEEDED
Success! Now that we have the USB framework compiling, we need configure the framework to work for your device. I will be using a PIC18F4550.



Adding your device to the Microchip USB Framework

Right click on "Header Files," and add "HardwareProfile - PICDEM FSUSB.h" to your project so that it can be edited easily.

Open "HardwareProfile - PICDEM FSUSB.h" and comment out

#define DEMO_BOARD PICDEM_FS_USB
#define PICDEM_FS_USB
and below that putting

#define DEMO_BOARD PIC18F4550_USB
#define PIC18F4550_USB
Now, go back to "main.c" and before the line

#elif defined(PIC18F87J50_PIM)
add

#elif defined(PIC18F4550_USB)
Put your device configuration immediately after the above line. I will be using the following configuration (read the PIC18 configuration tutorial to learn how to configure your device).

	/* Device Configuration */
	#pragma config FOSC = HSPLL_HS	// 20 MHz crystal.
	#pragma config PLLDIV = 5	 	// Divide by 5 to provide the 96 MHz PLL with 4 MHz input.
	#pragma config CPUDIV = OSC1_PLL2 // Divide 96 MHz PLL output by 2 to get 48 MHz system clock.
	#pragma config USBDIV = 2	 	// "USB clock source comes from the 96 MHz PLL divided by 2."
	#pragma config FCMEN = OFF // "Fail-Safe Clock Monitor disabled."
	#pragma config IESO = OFF  // "Oscillator Switchover mode disabled."
	#pragma config PWRT = OFF  // "PWRT disabled."
	#pragma config WDT = OFF   // "HW Disabled - SW Controlled."
	#pragma config MCLRE = ON  // "MCLR pin enabled; RE3 input pin disabled."
	#pragma config BOR = ON	// Brown Out Reset enabled in hardware. In case of supply voltage drop, BOR resets the device to prevent erratic CPU behavior.
	#pragma config BORV = 3	// BOR occurs at 2V (1.2V below the minimum required voltage for the HSPLL oscillator mode).
	#pragma config VREGEN   = ON	   //USB Voltage Regulator
	#pragma config LVP = OFF   // Disable low voltage ICSP
	#pragma config ICPRT = OFF // Disable dedicated programming port (44-pin devices)
	#pragma config CP0 = OFF   // Disable code protection
Right click on "Header Files" and add "p18f4550.h" (located in "[c18 compiler install location]\h\") to your project.

Paste the following in a new file, save as "main.h", and add the header file to your project.

#ifndef MAIN_H
#define MAIN_H

/** V A R I A B L E S ********************************************************/
#pragma udata
extern char USB_In_Buffer[64];
extern char USB_Out_Buffer[64];

extern BOOL stringPrinted;
extern volatile BOOL buttonPressed;
extern volatile BYTE buttonCount;

/** P R I V A T E  P R O T O T Y P E S ***************************************/
void InitializeSystem(void);
void ProcessIO(void);
void USBDeviceTasks(void);
void YourHighPriorityISRCode();
void YourLowPriorityISRCode();
void USBCBSendResume(void);
void BlinkUSBStatus(void);
void UserInit(void);

#endif

Add the following line to "main.c" in the includes section

#include "main.h"

Find the three lines in "main.c" that say

static void InitializeSystem(void)
and replace it with

void InitializeSystem(void)
i.e. remove the "static" word from each of the three lines.

Go to the function "ProcessIO(void)" in "main.c" and delete everything inside the function. We are going to create a source file for our code to go in.

Create a new file and paste the following inside

#include "p18f4550.h"
#include "./USB/usb.h"
#include "./USB/usb_function_cdc.h"
#include "main.h"

void user(void)
{  
	BYTE numBytesRead;

	//Blink the LEDs according to the USB device status
	BlinkUSBStatus();
	// User Application USB tasks
	if((USBDeviceState < CONFIGURED_STATE)||(USBSuspendControl==1)) return;

	if(USBUSARTIsTxTrfReady())
	{
		numBytesRead = getsUSBUSART(USB_Out_Buffer,64);
		if(numBytesRead != 0)
		{
			BYTE i;
		  
			for(i=0;i<numBytesRead;i++)
			{
				switch(USB_Out_Buffer[i])
				{
					case 0x0A:
					case 0x0D:
						USB_In_Buffer[i] = USB_Out_Buffer[i];
						break;
					default:
						USB_In_Buffer[i] = USB_Out_Buffer[i];// + 1;
						break;
				}

			}

			putUSBUSART(USB_In_Buffer,numBytesRead);
		}
	}

	CDCTxService();
}

Save as "user.c," and then add the file to your project by right clicking "Source Files" and adding the file.

Again, create a new file, and inside it put:

#ifndef USER_H
#define USER_H

void user(void);

#endif
Save as "user.h" and add the header file to your project.

Open "main.c" again and in the includes section add

#include "user.h"
Go to the main() function in "main.c" (line 457) and after

ProcessIO(); 
add

user();

At this point, we have contained all of our "user" code in the "user.c" file, and you should now be able to build your project without any errors!



Installing the Microchip USB CDC driver

At this point the USB CDC Framework is configured for your device and should compile without any errors or warnings. Program your microcontroller and connect it to your computer with your USB connection. The final step before you can use your device is to install the Microchip USB CDC driver.

Open Device Manager, expand "Other devices," right click on "CDC RS-232 Emulation Demo" and click "Update Driver Software." A new window should open, click "Browse my computer for driver software." Browse to "C:\Microchip Solutions v2011-06-02\USB\Device - CDC - Basic Demo\inf" and click next. The microcontroller should now be listed under "Ports (COM & LPT)" as "USB Communications Port (COMx)." (the 'x' is a number that will change depending on which USB port you plug your microcontroller into).



Conclusion

Now that your microcontroller is programmed and plugged into the computer, it's time to have some fun! Fire up your favorite serial communications program (I like SuperMon and RealTerm) and try sending some data. The default behavior of the framework we have configured is to echo back any data received.

Posted Image

You can also download the PIC18F4550 USB CDC Framework MPLAB project from this tutorial.


0 Comments


or Sign In