Oops! It appears that you have disabled your Javascript. In order for you to see this page as it is meant to appear, we ask that you please re-enable your Javascript!

USB Serial Communication with PIC Microcontroller – Flowcode

USB Serial Communication with PIC Microcontroller – Flowcode

USB Communication using PIC18F4550

Figure 1: USB Communication using PIC18F4550

Many computers especially portable ones do not have a serial port (COM Port) anymore. When a connection to a Personal Computer (PC) is required, a USB is the choice. The Universal Serial Bus (USB) is the widely used interface in electronic consumer products today. Most of electronic devices have at least one USB port on them, this include PCs, cameras, GPS devices, printers and so on. 

The Communication Device Class (CDC) 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.
In this article we are going to learn how to set up a serial communication between a microcontroller and a PC using a USB port by creating a virtual COM Port. 
Some PIC18 microcontrollers support USB interface directly. For example, the PIC18F25K50, PIC18F4450 and PIC18F4550 microcontrollers just to name a few all have a full-speed compatible USB interface that allows communication between a host PC and the microcontroller. 
Figure 1 above shows a typical connection of PIC18F4550 to a PC. 
PORTC pins RC4 (pin 23) and RC5 (pin 24) are used for USB interface. RC4 is the USB data D- pin, and RC5 is the USB data D+ pin. 
The maximum power available from the PC to an external USB device is limited to about 100mA at 5.0V, this is a nice feature that can be used to power a microcontroller as well. 
The USB bus is a very complex protocol. Flowcode provides a component for USB Serial ,The USB Serial device is used to stream data between a microcontroller and a PC.

                        Figure 2: USB Serial Component.

To insert a USB Serial component, on the components tool bar click on USB Serial under the Peripheral group. A USB Serial component (USBSerial(0)) will be inserted on the panel.

Figure 3: Inserting a USB Serial component

Select the USB Serial component then click on the “….” next to the Ext Properties to edit the USB Serial properties.

                     Figure 4: USB Serial Properties

VID: Vender ID
Each USB device must have a unique set of PID and VID codes. The default PID and VID values can be used for development and experimental purposes only but to release your own USB devices you must purchase a unique VID and PID from the USB specifications organization.
The default VID is: 0x12BF
PID: Product ID
PID codes are assigned for each product by a vender. PID together with the VID makes each USB device unique even when multiple USB devices are connected to the same computer. 
The Default PID is: 0xF010.  

This is the name of the device as it will appear in your device manager. 
The default name is: Flowcode USB Serial.

This is the manufacturer of the device as it will appear in your device manager. 
The default manufacturer is: Matrix Multimedia Ltd  

Enumeration Wait 
This will enable the enumeration timeout function or stalls the program in the initialise macro until USB enumeration has been fully completed. If timeout value is reached before enumeration is completed then the initialise macro will return error code 255. 
In default mode, it is disabled (un-ticked).

Enumeration Time 
If the Enumeration Wait is ticked (enabled), then this option will be enabled. This is time in seconds to wait for enumeration. 
The default time is 10 seconds

This is the version of the device as it will appear in your device manager. 
The default version is: 1.0

Generate Driver File 
Click on this button to generates the .inf file that contains the device driver information for your specific device. 
When plugging in your USB device for the first time, you will need to point the Driver install wizard to the location of this generated .inf file for your USB device to be recognized by the PC.

 Figure 5: Save USB Driver       

This component does not have a connection properties because the USB pins (Data + and Data – ) cannot be changed. Check your datasheet to find out which pins to use for your device. For the PIC18F4550, RC5 is used as Data + and RC4 as Data – 

Component Macros

Drag and drop the Component Macros from the Icons toolbar.

 Figure 6: Component Macro

Double click the component macro to open its properties. Click on the USBSerial(0) to see its macros.
The USB Serial has the following macros:
Send_String(String, Length)
Read_String(timeout_ms, String, Length)

Inialise_Serial( ) 
This macro is used to startup the USB communications driver and start the enumeration service. This allows the PC to see your device when it is plugged in to a USB port. 
This macro returns a 0 if the startup was successful and returns 255 if the startup failed.
This macro should always be called before other macros.                    

Figure 7: Initialise USB Serial

This macro is used to transmit a byte of data over the USB connection.
In the expression drop down box, input a data to send or select a variable.

            Figure 8: Send a Byte of data

Send_String(String, Length)
This macro is used to send a string or array of data over the USB connection.

Figure 9: Send a String of data

Use the macro to retrieve a byte of data coming from the USB port buffer. 
A time out value which is the length of time to wait for incoming byte in milliseconds (255 = wait forever) has to be specified. 
This macro will return the content of the received byte or 255 for timeout. 
You must specify a byte variable to hold the content of the retrieved data, in this example, “Byte_Data” variable is used.

  Figure 10: Read a Byte of data

Read_String(timeout_ms, String, Length) 
This macro is used to retrieve a string of information with maximum size of length from the incoming USB buffer. 
The length of time to wait for incoming byte in milliseconds (255 = wait forever) has to be specified as well as a string variable to store the incoming data.     

 Figure 11: Read a string of data

Clock Settings

The PIC18F2455/2550/4455/4550 family incorporate a different oscillator and microcontroller clock system than previous PIC18F devices due to the USB module.
The USB module requires a stable clock source and has to be compliant with both USB low-speed and full-speed specifications.
To accommodate these requirements, PIC18F2455/ 2550/4455/4550 devices include a new clock branch to provide a 48 MHz clock for full-speed USB operation. Since it is driven from the primary clock source, an additional system of prescalers and postscalers has been added to accommodate a wide range of oscillator frequencies.
If the clock is 6MHz, then we are working with low speed, if it is 48MHz then we are working with high speed.
A built in  Phase Locked Loop (PLL) inside the oscillator module allows us to raise the frequency to 48MHz from a low frequency of 4MHz, 8MHz and so on. More information can be found from the PIC18F4550 data sheet.

 Figure 12: Clock settings

The PLL circuit takes a 4MHz clock, if we are using a 4MHz crystal, then we don’t need to divide anything. 
If our crystal is 8MHz as in this case, in the Prescaler Selection select divide by 2 (8/2 = 4MHz) as shown on figure 12 above. If the clock is 20 MHz then Divide by 5 (20/5 = 4MHz) and so on.
–>As the output of the PLL circuit generates a 96MHz we have to divide it by 2 to get 48MHz. In the USB  Clock Selection select 96MHz PLL Src:/2.
–>In the CPU Sys CLK Select, select divide by 2.
–>Make sure your oscillator frequency is 8MHz and edit your Oscillator Selection to be: HS: HS + PLL, USB-HS.
–>Make sure that your USB Voltage Regulator is Enabled. This will enable the 3.3V built in USB Voltage regulator and it’s very important for your USB communication so you have to enable your PIC to generate the 3.3V or to supply it by yourself externally but take care, once you enabled your PIC to generate 3.3V you have to connect a capacitor to the Vusb pin and ground (this capacitor Value is 200nF to 470nF: C3 on figure 1) 

Full Flowcode project:  

The circuit diagram of the project is shown on figure 1:
When the project starts: 
The red LED will flash if the USB is not properly initialised, in the other hand, if it initialises successfully, the green LED will be switched on. and the following will follow:

1. The following message will be sent to the USB port: “Hello and Welcome to USB Communication” than “studentcompanion.co.za”
2. Afterwards, any message typed from the PC and sent to the microcontroller will be echoed back.

When the device is connected to PC for the first time, Windows will detect a new hardware.
Notes: For you to be able to simulate a USB connection, you will have to install Proteus Virtual USB drivers.
Go to All Programs from your computer start menu, then Proteus Professional, Virtual USB and then Install USB Drivers.

Figure 13: Found 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.

Figure 14: Found new Hardware wizard

Browse for the device drivers in the folder where you saved them as in figure 5 above. 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. 
If your serial driver was installed correctly, you should see Flowcode USB Serial and in bracket the COM number in the device manager.
Set the communications parameters to 9600 baud, 8 data bits, 1 stop bit, and no parity bit
Note: If you connect your device in another USB port, you will have to install the drivers again.

                    Figure 15: Project simulation

You can download the full project files (Flowcode and Proteus Schematic design) 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 Communication Flowcode Project

Download: USB Communication Flowcode Proteus

Download: USB Communication Flowcode Driver

Share this post

Select your currency
EUR Euro
USD United States (US) dollar