PIC18F4550 microcontroller has 1 USB (Universal Serial Bus) communication module. This topic shows how to use PIC18F4550 as a USB HID (Human Interface Device) to send and receive data from the PC.
The USB HID device doesn’t need any additional driver because it’s already installed in most of modern operating systems.
PIC18F4550 USB HID example hardware circuit:
The following figure shows our example basic circuit schematic.
The USB power supply pin (5V) can be used in this project.
To send/receive data to/from the microcontroller using USB communication a software named HID Terminal from MikroElektronika is used. The main interface of this software is shown below:
HID Terminal can be downloaded from the following link:
HID Terminal
About Example:
After plugging the USB cable which comes from the microcontroller, the device will appear in the HID devices list which named USB HID Example. If we want to send data to the microcontroller just tape the text and click on Send.
The LCD is used to display the data received by the microcontroller and after 1 second the microcontroller sends the same data back to the PC which will appear in the HID terminal software.
PIC18F4550 USB HID Example C code:
The C code below was tested with CCS C compiler version 5.051.
In this project the an external oscillator (8MHz) is used to run the microcontroller as well as the USB module. PIC18F4550 microcontroller always needs an external oscillator to run its USB module.
The fuses used in this project are:
#fuses HSPLL PLL2 CPUDIV1 USBDIV VREGEN NOMCLR
HSPLL : high speed oscillator is used with PLL enabled
PLL2 : Divide by 2 (8MHz oscillator input)
CPUDIV1 : No system clock postscaler
USBDIV : USB clock source comes from PLL divide by 2
VREGEN : USB voltage regulator enabled
NOMCLR : Master Clear pin used for I/O (MCLR pin function disabled)
The program can send and receive a maximum of 16 bytes. The sizes can be changed with the following two lines:
#define USB_CONFIG_HID_TX_SIZE 16 // Transmit packet size (bytes)
#define USB_CONFIG_HID_RX_SIZE 16 // Receive packet size (bytes)
The following two lines for product ID and vendor ID numbers. This numbers can be changed:
#define USB_CONFIG_PID 1 //Chnage Product Id
#define USB_CONFIG_VID 1234 //Chnage Vendor Id
Other lines are described in the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | // PIC18F4550 USB HID Examlpe CCS C code //LCD module connections #define LCD_RS_PIN PIN_D0 #define LCD_RW_PIN PIN_D1 #define LCD_ENABLE_PIN PIN_D2 #define LCD_DATA4 PIN_D3 #define LCD_DATA5 PIN_D4 #define LCD_DATA6 PIN_D5 #define LCD_DATA7 PIN_D6 //End LCD module connections #define USB_CONFIG_HID_TX_SIZE 16 // Transmit packet size (bytes) #define USB_CONFIG_HID_RX_SIZE 16 // Receive packet size (bytes) #define USB_CONFIG_PID 1 //Chnage Product Id #define USB_CONFIG_VID 1234 //Chnage Vendor Id #define USB_STRINGS_OVERWRITTEN #include <18F4550.h> #fuses HSPLL PLL2 CPUDIV1 USBDIV VREGEN NOMCLR #use delay(clock = 48000000) #include <lcd.c> #define USB_STRING(x) (sizeof(_unicode(x))+2), 3, _unicode(x) rom char USB_STRING_DESC[]={ //string 0 (must always provide this string) //4(length of string index),3(desciptor type is string),9&4(Microsoft Defined for US-English) 4, 3, 9, 4, //string 1 - vendor (this is optional, but we specified it's use in the device descriptor) USB_STRING("CCS"), //string 2 - product (this is optional, but we specified it's use in the device descriptor) USB_STRING("USB HID Example"), //string 3 - serial number (this is optional, but we specified it's use in the device descriptor) USB_STRING("123456789")}; #include <usb_desc_hid.h> #include<pic18_usb.h> #include<usb.c> char data[16]; void main(){ lcd_init(); // Initialize LCD module lcd_putc('\f'); // LCD clear lcd_gotoxy(1, 1); lcd_putc("USB HID Example"); usb_init_cs(); // Initialize USB hardware delay_ms(1000); while(TRUE){ usb_task(); if(usb_enumerated()){ // If the device has been enumerated by the PC if(usb_kbhit(1)){ // If endpoint1 has data in it's receive buffer lcd_gotoxy(1, 2); lcd_putc(" "); // Clear 2nd line // Read up to 16 bytes from endpoint1 buffer and save it to variable data usb_get_packet(1, data, 16); lcd_gotoxy(1, 2); printf(lcd_putc, data); // Display the received bytes on LCD delay_ms(1000); // Wait 1 second // Return the received bytes back usb_put_packet(1, data, 16, USB_DTS_TOGGLE); } } } } |
PIC18F4550 USB HID Example Videos:
The following video shows our example in protoboard circuit.
And the following video shows example Proteus simulation.
Discover more from Simple Circuit
Subscribe to get the latest posts sent to your email.
Well, I tried also this code, but Win10 doesn’t know my device. It says that try again.
I removed all LED code and make a normal serial USB.
I used 8MHz crystal with 22pF capacitors and measured by oscilloscope that signal was 8MHz.
Then I try to put LED on PIN B0 and it start to oscillate 500 Hz.
I used CCS v5.115 compiler and PICKIT2 prommer.
I have made working PICs about 30 years.
The picture is here
Can you help ?
Pekka
I add the Device Management USB configuration
Device USB\VID_0000&PID_0002\5&6032f3f&0&3 was configured.
Driver Name: usb.inf
Class Guid: {36fc9e60-c465-11cf-8056-444553540000}
Driver Date: 06/21/2006
Driver Version: 10.0.19041.2546
Driver Provider: Microsoft
Driver Section: BADDEVICE.Dev.NT
Driver Rank: 0xFF0000
Matching Device Id: USB\DEVICE_DESCRIPTOR_FAILURE
Outranked Drivers: usb.inf:USB\DEVICE_DESCRIPTOR_FAILURE:00FF2000
Device Updated: false
Parent Device: USB\ROOT_HUB\4&1439a827&0
hi there i tried this code but when i connect usb to my laptop it shows error USB device not recognized. i also checked the connection but it still shows same error.
do you have the other libraries such as LCD.c and usb.c?
this example buil good wery and function with win7 64bit
this example buil good but not function with win7 64bit
USB_STRING(“CCS”),
//string 2 – product (this is optional, but we specified it’s use in the device descriptor)
USB_STRING(“USB HID Example”),
//string 3 – serial number (this is optional, but we specified it’s use in the device descriptor)
USB_STRING(“123456789”)};
I had this error message. I could not solve it. Please help me? Email: [email protected]
Expression must evaulate to a constant – _ unicode