
#include <18F85J50.h>
#fuses  HSPLL,NOWDT,PLL5,STVREN,NOXINST,NODEBUG,NOCPUDIV,PROTECT,FCMEN,NOIESO,NOEASHFT,MCU
#use delay(clock=20000000)                                    

#define USB_HID_DEVICE     FALSE             //deshabilitamos el uso de las directivas HID               
#define USB_EP1_TX_ENABLE  USB_ENABLE_BULK   //turn on EP1(EndPoint1) for IN bulk/interrupt transfers
#define USB_EP1_RX_ENABLE  USB_ENABLE_BULK   //turn on EP1(EndPoint1) for OUT bulk/interrupt transfers
#define USB_EP1_TX_SIZE    16                 //size to allocate for the tx endpoint 1 buffer
#define USB_EP1_RX_SIZE    16                //size to allocate for the rx endpoint 1 buffer  
#define LED_VERDE PIN_C0
#define LED_AMARILLO PIN_C1
#define LED_ROJO PIN_C2  

#define Enciende Output_High
#define Apaga Output_Low
#define Conmuta Output_Toggle 

#define RecCommand recbuf[0]
          
#define COMMAND_LED 1
#define COMMAND_FIRMWARE 2
#define COMMAND_STRING_RS232 3   

const int8 Lenbuf = 32; 
int8 recbuf[Lenbuf];


                                                                                             

#include <pic18_usb.h>     //Microchip PIC18Fxx5x Hardware layer for CCS's PIC USB driver    
#include "usb_desc_scope.h" //Configuración del USB por Enumerador Palitroquez 
#include <usb.c>           //handles usb setup tokens and get descriptor reports   

 
                                                                                                                  
                            
                                              
                                                                                                                             
void main(void) {                                                                                                       

set_tris_c(0x00); //Todo salidas                                               
output_c(0xAA);  
usb_init(); //inicializa el USB
usb_task(); //habilita periférico usb e interrupciones
usb_wait_for_enumeration(); //espera hasta que el PicUSB sea configurado por Windows

while (TRUE)
  {  
  usb_task();    //habilita periférico usb e interrupciones
    if(usb_enumerated()){
      Apaga(LED_ROJO);
      Enciende(LED_VERDE);
      if (usb_kbhit(1)){   //Vale 1 si el endpoint de recepción contiene datos del host
       
        // Recibe Packet
        usb_get_packet(1, recbuf, Lenbuf);
        
        
        }
                                
        }
      }
    }
                                                                                                
 
  


                         
   
                                                      
