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.

1 thought on “USB Mouse using PIC18F4550 microcontroller – CCS C”

  1. Do you think that it can be controlled by potentiometers instead of switches?

    I’m experimenting with the code to use the gamepad analog stick as a mouse.

    I have a signed int8 variable for X and Y (signed int x=0,y=0;) but i was wondering if it can be changed to int16 to use the full 10bit ADC on the pic18f4550 for more precision of movement, because when i move the joystick it jumps very fast to the left corner or rigth corner, up and down respectively.

    My code at the moment looks like this:



    out_data[0]=0; //Boton izquierdo
    out_data[1]=x; //Eje X positivo y negativo
    out_data[2]=y; //Eje Y positivo y negativo
    out_data[3]=0; //Boton derecho

Leave a Comment

Your email address will not be published. Required fields are marked *

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

Scroll to Top