#include <18F4550.h>
#device ADC=8
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN      //~~~ 20MHZ OSCILLATOR CONFIGS ~~~//
#use delay(clock=48000000)
#DEFINE USB_HID_DEVICE     FALSE
#define USB_EP1_TX_ENABLE  USB_ENABLE_BULK  //turn on EP1 for IN bulk/interrupt transfers
#define USB_EP2_RX_ENABLE  USB_ENABLE_BULK  //turn on EP1 for OUT bulk/interrupt transfers
#define USB_EP1_TX_SIZE    64  //size to allocate for the tx endpoint 1 buffer
#define USB_EP2_RX_SIZE    64   //size to allocate for the rx endpoint 1 buffer
#include <pic18_usb.h>
#include <descriptors.h>   //USB Configuration and Device descriptors 
#include <usb.c>           //handles usb setup tokens and get descriptor reports
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#use fast_io(A)
#use fast_io(B)
#byte PORTB=0xF82
int tempoA1,tempoA2,tempoA11;
int envia[4];  
unsigned char tempA[4];
void envia_nota()
{   usb_put_packet(1,envia,4,USB_DTS_TOGGLE);
}
void main(void) {
   
   tempoA11=tempoA2=0;
   envia[1]=0x90;
   envia[0]=envia[1]>>4;
   setup_adc_ports(AN0_TO_AN1);  
   setup_adc(ADC_CLOCK_INTERNAL);
   usb_init_cs();
   while (TRUE) {
      usb_task();
      if(usb_enumerated()) 
    {
      {   set_adc_channel(0);
         delay_us(30);
         tempoA1=read_adc();
         tempoA11=tempoA1/2;      
                       
        
         if(tempoA11!=tempoA2)   
            {   tempA[1]=0xB0;
               tempA[0]=tempA[1]>>4;
               tempA[2]=0x00;
               tempA[3]=tempoA11;
               usb_put_packet(1,tempA,4,USB_DTS_TOGGLE);
               tempoA2=tempoA11;
            }
               
          { If (input (PIN_B0))          
      {  delay_ms (10);             
         If (input (PIN_B0));       
            envia[1] = 0x90; //Note on
            envia[0] = envia[1]>>4;
            envia[2] = 0x00; // Nota 0
            envia[3] = 0x7F; // Volumen 127
            envia_nota();
            
         {  do { } while (input (PIN_B0));  
            bit_set(PORTB,0);
            delay_ms(60);
            bit_clear(PORTB,0);
            envia[1] = 0x90; //Note off
            envia[0] = envia[1]>>4;
            envia[2] = 0x00; //Nota 0
            envia[3] = 0x00; // Volumen 0
            envia_nota();
                }
      }
                
                { If (input (PIN_B1))          
      {  delay_ms (10);             
         If (input (PIN_B1));     
            envia[1] = 0x90; //Note on
            envia[0] = envia[1]>>4;
            envia[2] = 0x01; // Nota 0
            envia[3] = 0x7F; // Volumen 127
            envia_nota();
            
         {  do { } while (input (PIN_B1));  
            bit_set(PORTB,1);
            delay_ms(60);
            bit_clear(PORTB,1);
            envia[1] = 0x90; //Note off
            envia[0] = envia[1]>>4;
            envia[2] = 0x01; //Nota 0
            envia[3] = 0x00; // Volumen 0
            envia_nota();
             }
      }
                
                { If (input (PIN_B2))          
      {  delay_ms (10);             
         If (input (PIN_B2));      
            envia[1] = 0x90; //Note on
            envia[0] = envia[1]>>4;
            envia[2] = 0x02; // Nota 0
            envia[3] = 0x7F; // Volumen 127
            envia_nota();
            
         {  do { } while (input (PIN_B2));  
            bit_set(PORTB,2);
            delay_ms(60);
            bit_clear(PORTB,2);
            envia[1] = 0x90; //Note off
            envia[0] = envia[1]>>4;
            envia[2] = 0x02; //Nota 0
            envia[3] = 0x00; // Volumen 0
            envia_nota();
                }
              }
            }
          }
        }
      }
     }
   }
}