You guys have seen my previous post on USB HID Communication using PIC with mikroC compiler. Click here to read again.
Now in this post, i will show you guys how to use one of the most popular CCS PIC-C compiler for USB-HID communication using PIC.
The specialty of this compiler is that it can be integrated with MPLAB IDE, so those who loves to write, compiler and debug using MPLAB IDE can use this compiler in the MPLAB IDE, which was not possible with mikroC compiler.
Create a project in MPLAB IDE and copy the code given below and paste it in new file. The code is self explanatory with the help of the comments written.
#include <18F4550.h>
#device ADC=8
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=48000000)
//RS232 Configuration
#use rs232(baud=9600, UART1, errors)
#define LED1 PIN_B7
#define LED2 PIN_B6
#define LED3 PIN_B5
//Transmit and Receive Packet Size
#define USB_CONFIG_HID_TX_SIZE 16
#define USB_CONFIG_HID_RX_SIZE 16
/*******VENDOR ID AND PRODUCT ID********/
#define USB_CONFIG_PID 1 //Chnage Vendor Id and Product Id
#define USB_CONFIG_VID 4660 //So that they will work with my Application
/***************************************/
/*******LCD Pin Configuration******/
#define LCD_ENABLE_PIN PIN_C2
#define LCD_RS_PIN PIN_C1
#define LCD_RW_PIN PIN_C0
#define LCD_DATA4 PIN_D4
#define LCD_DATA5 PIN_D5
#define LCD_DATA6 PIN_D6
#define LCD_DATA7 PIN_D7
/***********************************/
#include<pic18_usb.h>
#include<usb_desc_hid.h>
#include<usb.c>
#include<lcd.c>
//Macro Definition for LED ON and OFF
#define LED_ON(x) output_low(x)
#define LED_OFF(x) output_high(x)
void usb_debug_task(void)
{
static int8 last_connected;
static int8 last_enumerated;
int8 new_connected;
int8 new_enumerated;
new_connected = usb_attached();
new_enumerated = usb_enumerated();
if (new_enumerated)
LED_ON(LED1);
else
LED_OFF(LED1);
if (new_connected && !last_connected)
printf("\r\n\nUSB connected, waiting for enumaration...");
if (!new_connected && last_connected)
printf("\r\n\nUSB disconnected, waiting for connection...");
if (new_enumerated && !last_enumerated)
printf("\r\n\nUSB enumerated by PC/HOST");
if (!new_enumerated && last_enumerated)
printf("\r\n\nUSB unenumerated by PC/HOST, waiting for enumeration...");
last_connected=new_connected;
last_enumerated=new_enumerated;
}
void main(void)
{
unsigned char in_data[16];
unsigned int i;
set_tris_b(0x00); //Port-B as Output Port
set_tris_c(0x10);
output_b(0x00);
LED_OFF(LED1);
LED_OFF(LED2);
LED_OFF(LED3);
lcd_init(); //Initialize the LCD Module
lcd_gotoxy(3,1);
lcd_putc("PIC18F4550");
Delay_ms(1000);
lcd_gotoxy(3,2);
lcd_putc("USB EXAMPLE!");
printf("\r\n\nUSB Test Program--->Written in CCS PIC-C");
printf("\r\nEMBEDDED LABORATORY\r\n");
usb_init_cs();
Delay_ms(1000);
lcd_putc("\fRECEIVED DATA:-");
Delay_ms(1);
while(1)
{
usb_task();
usb_debug_task();
if(usb_enumerated())
{
if(usb_kbhit(1))
{
lcd_gotoxy(1,2);
lcd_putc(" ");
usb_get_packet(1,in_data,16);
printf("\r\nReceived Data: ");
for(i=0;i<16;i++)
{
printf("%c",in_data[i]);
//Send the Recevied Data to Serial Port
lcd_gotoxy(1+i,2);
lcd_putc(in_data[i]);
}
//Now Clear the in_data array
for(i=0;i<16;i++)
{
in_data[i] = '\0'; //Null Character
}
}
}
}
}
This is how the code looks in MPLAB IDE. Build this project and program the micro-controller with the hex files and configuration files.
USB HID Project in MPLAB Using CCS PIC-C Compiler |
USB HID Terminal by Embedded Laboratory |
Hello, can you send data from PIC to PC ?
ReplyDeleteCan you give me that code?
Thank you so much!
usb_get_packet function is used to get data from USB HID
Delete&
usb_put_packet function is used to send data from PC to PIC (HID Class)
Have a look at the following links
https://www.ccsinfo.com/forum/viewtopic.php?t=49670
https://www.ccsinfo.com/forum/viewtopic.php?t=42207
Hello.
ReplyDeleteI would like to write a new windows program using mcHID.dll
Where I can found info, include and/or lib file (.h)?
Search on Internet, but find nothing
Hi Massimo Moltoni
DeleteRefer this page
http://helmpcb.com/software/usb-hid-template-for-visual-basic-2005
ReplyDeleteHi Massimo Moltoni
Refer this page
http://helmpcb.com/software/usb-hid-template-for-visual-basic-2005
Hi, i would like to know regarding usb communication part 2 (MPLab IDE)
ReplyDeletein the code written in this page there seems to be missing lbraries like #include
#include
#include
#include