#define __USB_PIC_PERIF__ 1
//if using a 16bit PIC on an Explorer 16 borad, set this to 1
#define __USB_PIC_EXPLORER16__ 1
#if __USB_PIC_PERIF__
#define LED_ON(x) output_high(x)
#define LED_OFF(x) output_low(x)
#DEFINE LED1 PIN_B6
#DEFINE LED2 PIN_B7
#DEFINE LED3 PIN_C0
#define sw1 input(PIN_A3)
#include <18F2550.h>
#device ADC=8
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=48000000)
#endif
#use rs232(baud=9600, UART1, errors)
// you can change the tx and rx packet sizes here.
// in order to be compatbile with hiddemo.exe, these values must be 2.
#define USB_CONFIG_HID_TX_SIZE 2
#define USB_CONFIG_HID_RX_SIZE 2
/////////////////////////////////////////////////////////////////////////////
//
// Include the CCS USB Libraries. See the comments at the top of these
// files for more informaciónrmation
//
/////////////////////////////////////////////////////////////////////////////
#include <pic18_usb.h> //Microchip PIC18Fxx5x hardware layer for usb.c
#include <usb_desc_hid.h> //USB Configuration and Device descriptors for this UBS device
#include <usb.c> //handles usb setup tokens and get descriptor reports
/////////////////////////////////////////////////////////////////////////////
//
// Configure the demonstration I/O
//
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
//
// usb_debug_task()
//
// When called periodically, displays debugging informaciónrmation over serial
// to display enumeration and connection states. Also lights LED1 based upon
// enumeration and status.
//
/////////////////////////////////////////////////////////////////////////////
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)
{
int8 out_data[2];
int8 in_data[2];
int8 send_timer=0;
LED_OFF(LED1);
LED_OFF(LED2);
LED_OFF(LED3);
printf("\r\n\nCCS Vendor Specific HID Example");
#ifdef __PCH__
printf("\r\nPCH: v");
printf(__PCH__);
#elif defined(__PCD__)
printf("\r\nPCD: v");
printf(__PCD__);
#else
printf("\r\nPCM: v");
printf(__PCM__);
#endif
usb_init_cs();
#if !(__USB_PIC_PERIF__)
printf("\r\nUSBN: 0x%X", usbn_get_version());
#endif
printf("\r\n");
setup_adc_ports(AN0);
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(0);
while (TRUE)
{
usb_task();
usb_debug_task();
if (usb_enumerated())
{
if (!send_timer)
{
send_timer=250;
out_data[1]=read_adc();
out_data[0]=0x55;
if (usb_put_packet(1, out_data, 2, USB_DTS_TOGGLE))
printf("\r\n<-- Sending 2 bytes: 0x%X 0x%X", out_data[0], out_data[1]);
}
if (usb_kbhit(1))
{
usb_get_packet(1, in_data, 2);
printf("\r\n--> Received data: 0x%X 0x%X",in_data[0],in_data[1]);
if (in_data[0]) {LED_ON(LED2);} else {LED_OFF(LED2);}
if (in_data[1]) {LED_ON(LED3);} else {LED_OFF(LED3);}
}
send_timer--;
delay_ms(1);
}
}
}