USB Mouse using PIC18F4550 microcontroller – CCS C

PIC18F4550 microcontroller has USB module which can work as a HID (Human Interface Device). The USB HID device doesn’t need any additional driver because it’s already installed in most of modern operating systems.
Using PIC18F4550 as a HID device we can easily transfer data between PC and the microcontroller as shown at the following URL:
PIC18F4550 USB HID Example using CCS PIC C

This topic shows how to build a simple USB HID mouse using PIC18F4550 microcontroller (PIC18F2550 can also be used).

Required Components:

  • PIC18F4550 Microcontroller ( or PIC18F2550) 
  • 8MHz Crystal Oscillator
  • 2 x 22pF capacitors
  • 470nF Capacitor
  • 6 Push-Buttons
  • USB Connector
  • +5V Power Supply (USB VCC can be used)
  • Protoboard
  • Jumper Wires

Project circuit schematic is shown below:
USB mouse PIC18F4550 circuit

USB Mouse using PIC18F4550 C code:
The CCS C compiler has USB Mouse driver which allows us to build a USB mouse more easily. The following line is used to add this driver to this project:
#include <usb_desc_mouse.h>
In this project there is no need to receive data from the HID device, we have only to send data to this device using the following line:
usb_put_packet(1, out_data, 4, USB_DTS_TOGGLE);
Where out_data is an array of 4 elements (4 bytes):
Byte 1 corresponds to button status. The left button of the mouse is represented by 0x01 and the right button is represented by 0x03.
Byte 2 and byte 3 correspond to X axis and Y axis directions.
Byte 4 for the mouse wheel which is not used in this project.
The X axis and Y axis bytes are signed int8 which can vary from -127 to 127. The ( – ) sign indicates direction ( for X axis left/right and for Y axis up/down) and the number means number of steps at a time.
The complete C code is as the one below, it was tested with version 5.051.

USB Mouse using PIC18F4550 video:
This video shows a simple hardware circuit connected to PC where the screen is divided into two parts, the left part is a recorded video of the PC screen and the right side shows the hardware circuit.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.