Remote controlled USB mouse using PIC18F4550

Building a USB mouse using PIC18F4550 microcontroller and CCS C compiler is easy as shown in the link below:
USB Mouse using PIC18F4550 microcontroller

Also, it is not hard to add an infrared remote control to the previous USB project. This post shows how did I build a simple IR remote controlled USB mouse using PIC18F4550 microcontroller and Car MP3 IR remote control (NEC protocol).
The link below shows how to decode NEC IR remote control (remote controls may not have the same code messages, so to know the code of each button we’ve to decode it first):
NEC Protocol decoder with PIC16F887 microcontroller

Hardware Required:

  • PIC18F4550 microcontroller
  • IR remote control (NEC protocol)
  • IR receiver
  • 8MHz Crystal Oscillator
  • 2 x 22pF capacitors
  • 47uF polarized capacitor
  • 470nF Capacitor
  • 10K ohm resistor
  • USB Connector
  • +5V Power Supply (USB VCC can be used)
  • Breadboard
  • Jumper Wires

Remote controlled USB mouse using PIC18F4550 circuit:

PIC18F4550 USB mouse remote control circuit

The IR receiver has 3 pins: GND, VCC and OUT. The OUT pin is connected to RB0 (INT0 pin).
The circuit shown above is powered from an external 5V source which means there is no need to connect the VCC pin of the USB connector, but this pin can be used to supply the circuit and therefore the external 5V source has to be removed.
In this project the microcontroller runs with 8 MHz crystal oscillator and MCLR pin function is disabled.

Remote controlled USB mouse using PIC18F4550 C code:
The C code below was tested with CCS C compiler version 5.051.
In this project the PIC18F4550 MCU runs with 8 MHz crystal oscillator and it is configured so that the MCU runs at 8 MHz. I enabled PLL2 for the USB module in order to get 48 MHz which allows the USB module to run at full speed (12 Mbps).

The output of the IR receiver is connected to the external interrupt 0 pin (RB0/INT0) and with that I used it to decode the IR remote control. The NEC code massage is 32-bit long (2 words).
I used Timer1 to measure pulse and space widths and I used Timer1 interrupt to reset the decoding process in case of very long pulse or very long space (time out). Timer1 is configured to increment every 1 us. So for example for the 9 ms pulse I used the interval between 8500 and 9500.

I used Timer3 with the repeated remote control codes. If a button is pressed with hold the remote control will send the button massage code and then it sends 9 ms pulse –> 2.5 ms space –> 562 us pulse and so on every 110 ms. (more details at: http://www.sbprojects.com/knowledge/ir/nec.php)
From previous if a button is pressed with hold it will continuously sets Timer3 to the value 25000 ( set_timer3(25000); ). When the button is released Timer3 will overflow and its interrupt will reset the code message ( nec_code = 0; ).
The function usb_put_packet(1, out_data, 4, USB_DTS_TOGGLE); sends a packet of 4 bytes from the array out_data over USB.The variable array out_data is a 4-byte array where:
Byte 1 corresponds to button status. The left button of the mouse is represented by 1 and the right button is represented by 3.
Byte 2 and byte 3 correspond to X axis and Y axis directions respectively.
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 remote control which I used is shown below:

Car MP3 remote control

I used the buttons numbered from 1 to 6 where:

Button NumberFunctionCode
1Left button0x40BF30CF
2Move cursor up0x40BFB04F
3Right button0x40BF708F
4Move cursor left0x40BF08F7
5Move cursor down0x40BF8877
6Move cursor right0x40BF48B7

The complete C code is below.

Remote controlled USB mouse using PIC18F4550 video:
The following video is divided into 2 parts, part shows PC screen which is the main part and the small part shows my simple hardware circuit (sorry for video quality).


Discover more from Simple Circuit

Subscribe to get the latest posts sent to your email.

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