Serial communication with PC and embedded system has always been a big deal for the students in their project and professional in their work. So tried to work on that part and created an easy way for interfacing between PC and embedded system using PIC Microcontroller PIC18F2550 AND PIC18F4550. This tutorial provides the basic knowledge of creating small compact USB device interface between pc and embedded system and its controlling. You can control, moniter or implement data acquisition system using this tutorial
Fig. 1: Prototype of PIC USB CDC Device
Details
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.
Fig. 2: Prototype of PIC 18F2550 based USB CDC Device
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 forth:
- 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 DB9 (commonly called DB9) connector that looks like this:
Fig. 3: Image of Serial Port
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.
USB Pin Diagram
Fig. 4: Pin Diagram of USB Port
The included file and hex files are given in the attachment, complete MPLAB project
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.
Project Source Code
###
#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(); }###
Circuit Diagrams
Project Datasheet
Project Video
Filed Under: Electronic Projects
Filed Under: Electronic Projects
Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.