USB Communication With PIC Microcontroller CDC – MikroC

USB communication with PIC Microcontroller

USB Communication With PIC Microcontroller CDC – MikroC

USB Communication Circuit Diagram

Figure 1: USB Communication Circuit Diagram

The CDC device class code is “0x02, this class defines various communications over USB.
The CDC class can be used to emulate the RS232 serial port (COM Port) and thus creating an easy solution to migrate the application from an old RS232 to the Universal Serial Bus (USB) interface without implementing many changes especially to the PC software. The device will create a virtual COM.

The current MikroC Pro for PIC version 6.0.0 does not have a built in USB CDC library in the compiler, but you can download the USB Device Library from LIBSTOCK. This library supports the USB HID Class, the USB CDC Class and the USB Mass Storage Device Class.

The file you are going to download has an .mpkg extension. You need the package manager to integrate this USB library into the MikroElekronika compilers. You can download the latest package manager from MikroElekronika website.

Once you have installed the package manager, Open the desired package file (*.mpkg) and it’s content will be displayed in Navigation and Information sections.

Click on File –> Install option and the installation will begin. Installer will copy your package content into destination folders and update your definition files.

Figure 2: Installing a library into MikroC compiler

New installed library will appear in Library Manager of your compiler, and only for those chips that are compatible with installed library.

Figure 3: New installed library (USB_Device_PIC) in library manager

Buy a USB PIC Microcontroller from Our Online Shop

Below is a quick descriptions of the USB CDC Class library routines as described in the USB Device Library help file. This library works when PIC microcontroller with built-in USB are used (e.g., PIC18F4550), and port pins RC4 and RC5 are connected to the D+ and D- pins of the USB connector respectively.
USB buffers for sending and receiving data must be placed in the USB RAM.
You must also remember to set your clock configurations correctly as learned in the article:

USB Communication with PIC Microcontroller

To write a device class, certain function callbacks must be implemented in order to be used with library.
These functions must be in the user code:
USBDev_GetDescriptorHandler: Called when host requests descriptor.
USBDev_EventHandler:  This function is called when specific event happens, like device reset.
USBDev_SetupReqHandler: Called when there is a need to process non-standard requests.

USBDev_DataReceivedHandler: Called when data packet is received.
USBDev_DataSentHandler: Called when data packet is sent.

Some other Library Routines

This function initializes USB module in device mode.
This function returns no data.

This is a routine for servicing usb interrupts. This function should be called from USB interrupt routine.
Before using this function, the device should be initialized first and the USB interrupt should be enabled.
This function returns no data.

This function returns the current USB Device state.
Prototype: uint8_t USBDev_GetDeviceState(); 

For more functions, please read the USB Device Library help file.

USB Device Library – CDC Class

This function initializes the USB.
Before you can use any other functions, this  function should be called first.
This function returns no data.

Prototype: TUSBCDCLineCoding* USBDev_CDCGetLineCoding(); 
Returns: Pointer to line coding structure.
Description: Returns pointer to line coding structure containing data rate, stop bits, parity and data bits.

Prototype: void USBDev_CDCSetLineCoding (uint32_t dwDTERate, uint8_t bCharFormat, uint8_t bParityType,
uint8_t bDataBits); 

This function returns no data.
Description: Sets line coding structure, data rate, stop bits, parity and data bits.
dwDTERate: data rate in bits per second.
bCharFormat: stop bits: 0 – 1 stop bit, 1 – 1.5 stop bits, 2 – 2 stop bits.
bParityType: parity: 0 – None, 1 – Odd, 2 – Even, 3 – Mark, 4 – Space.
bDataBits: data bits: 5, 6, 7, 8 or 16.

Prototype: void USBDev_CDCSendData(uint8_t* dataBuff, uint16_t dataLen); 
This function returns no data.
Description: This function sends data to the bulk data endpoint and requires two parameters:
dataBuff: pointer to the data (buffer to hold the data).
dataLen: length of the data (less or equal to the maximum packet size)

Prototype: void USBDev_CDCSetReceiveBuffer(uint8_t* dataBuff); 
This function returns no data.
Description: This function enables reception of one data packet on the data endpoint and it has one parameter:
dataBuff: pointer to the buffer where received packed will be stored.
The size of this reception buffer should be at least maximum packet size for that endpoint.


Prototype: extern void USBDev_CDCDataReceived(uint16_t size);
This function returns no data.
Description: This function is going to be called when the packet is received on data endpoint and it
has one parameters: size: size of received data.

Prototype: extern void USBDev_CDCParamsChanged();
This function returns no data.
Description:This function is going to be called when host issues Set Line Coding request.


This project example (circuit on figure 1) shows the usage of USB CDC device that functions as a virtual COM port.

This example will send “This is USB CDC Example by” to terminal then send “Press any key…” after this any character pressed will be echoed back to the USART terminal.

When the device is connected to PC for the first time, Windows will detect a new hardware. Windows will ask you to install the device drivers. Tick on Install from a list or specific location (Advanced) radio box. Then click Next.

Browse for the device drivers then click Next, Windows will install the drivers for you. The project can be tested by connecting the output of the microcontroller to a USB port of a PC and then running the HyperTerminal terminal emulation software or any other serial terminal software

If you are using Microsoft windows XP, Go to Start, All Programs, Accessories, Communications and Hyper Terminal to open a windows free Hyper Terminal. 
Connect your Hyper Terminal to the correct COM Port (your USB virtual port).
To find out which COM Port has been assigned to your virtual Serial port, go to device manager (if you are using Windows XP, go to My Computer, at right hand side pane, click on View system information, click on Hardware tab then Device Manager) and expend the Ports (COM & LPT) to see all the COM ports on your PC.

Note: Your computer will automatically allocate a COM port number to your virtual Com Port, it does not have to be the same a the one illustrated here below on figure 4 (COM25) 

Figure 4: Virtual COM Port (COM25) in device manager

If your serial driver was installed correctly, you should see Mikroe Virtual Com Port and in bracket the COM number in the device manager as shown on figure 2 above.
Set the communications parameters to 9600 baud, 8 data bits, 1 stop bit, and no parity bit as shown on figure 5 below.

Figure 5: COM Port settings

Note: If you connect your device in another USB port, you will have to re-install the drivers again.

Here is the full project code:

Figure 6 below shows data sent to terminal on a personal computer connected to the microcontroller via a USB port.. 

Figure 6: Computer Terminal

Read also on student companion:

Creating a Serial Port Interface with C#

You can download the full project files (MikroC source code and Drivers) below here. All the files are zipped, you will need to unzip them (Download a free version of the Winzip utility to unzip files).

Download: USB CDC Driver

Download: USB CDC Example

Share this post

has been added to your cart:
Select your currency