Controlling a PIC Microcontroller from a PC Graphical User Interface (GUI) through USB

Controlling a pic microcontroller from a PC GUI through USB

Controlling a PIC Microcontroller from a PC Graphical User Interface (GUI) through USB

Controlling a PIC Microcontroller from a PC Graphical User Interface diagramFigure 1: Controlling a PIC Microcontroller from a PC Graphical User Interface diagram

A Graphical User Interface is a man-machine interface device, in which objects to handle are drawn as icons on the screen, so the user can send controls by a pointing device, usually a mouse or a keyboard.

It is always easy and requires less skills to operate a device from a visual representations by simply clicking a mouse or using a keyboard rather than a command line. The GUI can also be used to interface with other external devices located in different places. 

There are a lot of different kinds of software which can be used to design a GUI platform, the choice will usually depend on personal preferences, software capabilities and the operating systems (Windows, Linux, Mac…).
Among the popular ones we have Microsoft Visual studio with its popular programming languages visual basic and C#, Labview, Python, Matlab etc.

In the project: Controlling a PIC Microcontroller from a PC Graphical User Interface (GUI), we designed a Graphical User Interface (GUI) software using Microsoft Visual C# to control the LEDs connected to the PIC microcontroller. This software could be installed in any computer running windows operating systems. The computer connects to the microcontroller using an RS232 serial cable. The PIC microcontroller will receive commands from the computer to control devices connected to it such as motors, LEDs etc.

As the RS-232 serial interface port (COM Port) is now rarely found on a personal computer (PC), this once common interface has been virtually replaced by the Universal Serial Bus or USB for short.

Today USB has grown beyond PCs to become the common interface for many embedded industrial and consumer products like in cameras, GPS, printers, etc.

This has created a problem because many PC Software were designed to communicate with the embedded applications using the RS-232 interface as the USART is still one of the simplest serial protocol to use with any microcontroller, many devices are still using it as their main communication protocol like GSM/GPRS modules, GPS, etc.

In this project, we are going to emulate the serial port interface by using the USB port to control the PIC from the PC GUI interface. By using the USB in CDC class, they will be no single change required on the PC GUI software.

This can be used to easily migrate old projects using RS232 to USB quickly without changing the computer software, we will just need to change the PIC to a PIC with USB interface and change the microcontroller code.


Figure 2: Emulating the Serial Port Interface

To learn more, please read these article first:

USB Communication With PIC Microcontroller CDC – XC8

USB Communication With PIC Microcontroller CDC – mikroC

Controlling a PIC Microcontroller from a PC Graphical User Interface (GUI)

For rapid prototyping or for commercial products it’s always a must to use specialized reputable PCB manufacturer instead of doing it yourself and luckily these days, you don’t have to spend a fortune to get your boards manufactured professionally.

We recommend PCBWay a Chinese-based PCB manufacturer specializing in PCB prototyping, small volume production and PCB assembly service with more than a decade of experience, you can get good quality 10 PCBs (1-2 layers) from them for only $5. They also have a 24 hours tracking function on their order page, you will be able to monitor each stage of how they are manufacturing your PCBs till when they are ready for shipping.

As the whole world is battling with the Coronavirus, this is the time for global solidarity and collaboration for combating COVID-19, Love will conquer all fears!, is also joining hands with the global community. In this moment of need as there are shortages of masks all over the world, they are now sending free face masks(10 pcs) randomly with your orders. Priority is given to areas with severe outbreaks. If you have any question or you need the mask, you can contact directly your sales representative.

To learn more about how to get your free masks or how to get cheap great quality PCB, please click on the image below or visit PCBWay Home page

You can also download for free the Handbook of COVID-19 Prevention and Treatment which provides a comprehensive guidelines and best practices by China’s top experts for coping with COVID-19.

Take care and stay healthy! We stand with you! #StandWithPCBWayers

Buy a USB PIC Microcontroller from Our Online Shop

USB Configuration with MPLAB Code Configurator

You can refer the the configuration and explanation in the USB Communication With PIC Microcontroller CDC – XC8 article, in this project we will add the LED pins configuration.

We are using the PIC18F45K50, three LEDs are connected to PORTB, Red LED on RB0, Yellow LED on RB1 and Green LED on RB2. When the user Click on the RED LED button, it will send a ‘1’ to the PIC microcontroller to switch ON the RED LED only, if the YELLOW LED is clicked, a ‘2’ will be sent to switch ON the YELLOW LED and if the GREEN LED button is clicked, a ‘3’ will be sent to switch ON the GREEN LED.

USB Clock Settings

Start a new MPLAB X project and select the PIC18F45K50. Start the MCC to configure our peripherals.

Click on the System Module in the Project Resources. When the the PIC18F45k50 is used for USB connectivity, a 6MHz or 48MHz must be provided to the USB module for operation in either Low-Speed (1.5 Mbit/s) or Full-Speed modes (12 Mbit/s). To achieve this we will need a USB frequency of 48MHz which we can achieved by using the internal 16MHz clock with PLL (Phase-locked loop). This PIC has 3x and 4xPLL Clock multipliers as shown on the figure below. By using the internal 16MHz with 3x PLL we can raise the USB Clock frequency to 48MHz.

Figure 3: PIC18F45K50 Clock settings for 48MHz USB Full speed using MCC

The HFINTOSC is tuned using Full-speed USB events. The ACT is enabled by setting the ACTEN bit of the ACTCON register. The ACT uses the selected ACT reference clock to tune the 16 MHz Internal Oscillator to an accuracy of 16 MHz ± 0.2%. The tuning automatically adjusts the OSCTUNE register every reference clock cycle

Figure 4: PIC18F45K50 ACTON Register

MLA USB Device Lite

Under the Device Resources, Double click the MLA USB Device Lite to add it to the Properties Resources under the Peripherals. Select it to set its Properties. This is where we’re gonna configure all the USB settings, with MCC the setup is super easy.

Figure 5: MLA USB Device Lite configuration in MCC

CDC Settings:

Figure 6: USB CDC Settings

Pin Module

Pins RB0, RB1 and RB2 are set as output pins and given custom names in MCC as Red, Yellow and Green respectively.

Figure 7: Pin module

Full Microcontroller code with MPLAB XC8 compiler

USB Configuration with mikroC

For mikroC pro for PIC USB configuration, please refer to this article:

USB Communication With PIC Microcontroller CDC – mikroC

PC GUI Software

Watch the Video Tutorial part 1: Design with C#

Figure 8 below shows the PC GUI interface. C# Serial port component is used to send data to serial port of the computer, the same code will be used whether using USB or RS232.

PC GUI Software InterfaceFigure 8: PC GUI Software Interface

We have Three LED buttons on the form. The Red, Yellow and Green buttons. Clicking on a button will send a command to the microcontroller.

Some few serial port class methods:

  • Close(): Closes the port connection, sets the IsOpen property to false, and disposes of the internal Stream object. Call this to disconnect your serial port.
  • DiscardInBuffer(): Discards data from the serial driver’s receive buffer.
  • Dispose(): Releases all resources used by the Component.(Inherited from Component.)
  • Open(): Opens a new serial port connection.
  • Read(Char[], Int32, Int32): Reads a number of characters from the SerialPort input buffer and writes them into an array of characters at a given offset.
  • ReadExisting(): Reads all immediately available bytes, based on the encoding, in both the stream and the input buffer of the SerialPort object.
  • ReadLine(): Reads up to the NewLine value in the input buffer.
  • Write(String): Writes the specified string to the serial port.
  • Write(Char[], Int32, Int32): Writes a specified number of characters to the serial port using data from a buffer as shown on the code below.
Before sending any data, the serial port must be open first by using  the serialPort.Open();

To learn in detail how to design a serial port interface with C#, please read the article:

Creating a serial port interface with C#

Figure 9 below shows the PC GUI interface sending commands to PIC microcontroller. In this example when the Red button is clicked, the PIC red LED switches ON (character 1 is sent to PIC), when the yellow button is clicked, the PIC yellow LED switches ON (character 2 is sent to PIC) and lastly when the Green button is clicked, the PIC green LED is switched ON (character 3 is sent to PIC). If a button is clicked again while already ON character 4 is sent to PIC to switch OFF all LEDs.

PC GUI Software interface sending commands to PIC microcontrollerFigure 9: PC GUI Software interface sending commands to PIC microcontroller

Watch the Video Tutorial part 2: Setup Project

Full GUI C# code

You can download the full project files (MPLAB XC8 source code and C# Project) 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 MPLAB X Project: PIC-USB-CDC-GUI

Download C# GUI Project: pic_gui_c_project

Share this post

has been added to your cart:
Select your currency