Engineers Garage

  • Projects and Tutorials
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Products News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • White Papers
    • Webinars
  • EE Learning Center
    • Design Guides
      • WiFi & the IOT Design Guide
      • Microcontrollers Design Guide
      • State of the Art Inductors Design Guide
  • Women in Engineering

PIC USB CDC Device Interfacing

By Amol Wabale

 

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

Prototype of PIC USB CDC Device

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.

Prototype of PIC 18F2550 based USB CDC Device

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:

Image of Serial Port

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

Pin Diagram of USB Port

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

Circuit-Diagram-PIC-18F2550-Based-USB-CDC-Device

Project Datasheet

https://www.engineersgarage.com/wp-content/uploads/2019/10/MPLAB-PROJECT-FILES.zip


Project Video


Filed Under: Electronic Projects
Tagged With: cdc, microcontroller, pic, usb
 

Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


Featured Tutorials

  • PS2 Keyboard To Store Text In SD Card Using Arduino Circuit Setup On Breadboard
    How To Use PS2 Keyboard To Store Text In SD Card Using Arduino- (Part 42/49)
  • Wireless Path Tracking System Using Mouse, XBee And Arduino Circuit Setup On Breadboard
    How To Make A Wireless Path Tracking System Using Mouse, XBee And Arduino- (Part 43/49)
  • How to Make a Wireless Keyboard Using Xbee with Arduino- (Part 44/49)
  • Making Phone Call From GSM Module Using Arduino Circuit Setup On Breadboard
    How to Make Phonecall From GSM Module Using Arduino- (Part 45/49)
  • How to Make a Call using Keyboard, GSM Module and Arduino
    How To Make A Call Using Keyboard, GSM Module And Arduino- (Part 46/49)
  • Receiving SMS Using GSM Module With Arduino Prototype
    How to Receive SMS Using GSM Module with Arduino- (Part 47/49)

Stay Up To Date

Newsletter Signup

Sign up and receive our weekly newsletter for latest Tech articles, Electronics Projects, Tutorial series and other insightful tech content.

EE Training Center Classrooms

EE Classrooms

Recent Articles

  • Renesas delivers intelligent sensor solutions for IoT applications
  • Microchip Technology releases AVR-IoT Cellular Mini Development Board
  • Qualcomm acquires Cellwize to accelerate 5G adoption and spur infrastructure innovation
  • MediaTek’s chipset offers high-performance option for 5G smartphones
  • Nexperia’s new level translators support legacy and future mobile SIM cards

Most Popular

5G 555 timer circuit 8051 ai Arduino atmega16 automotive avr bluetooth dc motor display Electronic Part Electronic Parts Fujitsu ic infineontechnologies integratedcircuit Intel IoT ir lcd led maximintegratedproducts microchip microchiptechnology Microchip Technology microcontroller microcontrollers mosfet motor powermanagement Raspberry Pi remote renesaselectronics renesaselectronicscorporation Research samsung semiconductor sensor software STMicroelectronics switch Technology vishayintertechnology wireless

RSS EDABOARD.com Discussions

  • about ATmega328 ADC pins
  • Tuning the antenna to be conjugately matched to input impedance of the die
  • Netlist physical name update
  • nt1065_USB3 gnss receiver
  • LLC HB with synchronous rectifiers can be very dodgy?

RSS Electro-Tech-Online.com Discussions

  • undefined reference header file in proteus
  • Capacitor to eliminate speaker hum
  • Decapped Chip On Board
  • Sony KV-A2913E (chassis AE1C) auto shuts off after one minute
  • CRT TV repair
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • About Us
  • Contact Us
  • Advertise

Copyright © 2022 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Projects and Tutorials
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Products News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • White Papers
    • Webinars
  • EE Learning Center
    • Design Guides
      • WiFi & the IOT Design Guide
      • Microcontrollers Design Guide
      • State of the Art Inductors Design Guide
  • Women in Engineering