Controlador MIDI por USB con PIC para Virtual Dj

Estoy rompiendome el cerebro con esto, asi que pido algo de ayuda u orientacion.
Juntando todo lo que lei en las 18 paginas, arme este codigo:
Código:
#include <18F2455.h>
#device ADC=8
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#fuses NOMCLR   //Habilita MCLR/RE3 como entrada   
#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 <usb_bootloader.h>
#include <pic18_usb.h>
#include <.\include\midi7.h>   //USB Configuration and Device descriptors for this UBS device
#include <usb.c>           //handles usb setup tokens and get descriptor reports

#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#use i2c(MASTER,SCL=PIN_B3,SDA=PIN_B2,SLOW,FORCE_HW,RESTART_WDT)
#use fast_io(A)
#use fast_io(B)
#use fast_io(C)

#byte PORTA=0xF80
#byte PORTB=0xF81
#byte LATB=0xF8A
#byte BTRISB=0xF93
#byte INTCON=0xFF2
#byte ADCON1=0XFC1

short bitT;
int tempoA1,tempoA2,tempoA11,tempoB1,tempoB2,tempoB11;
int matriz1,matriz2,matriz1bis,matriz2bis,diferencias,contador;
int matriz3,matriz4,matriz3bis,matriz4bis;
int leds1,leds2,leds3;
int recibe[8];
unsigned char envia[4];
unsigned char tempA[4];
unsigned char tempB[4];

/////////////////////////////////////////
//subprograma para enviar notas
/////////////////////////////////////////
void envianota()
{   
   envia[1]=0x90;
   envia[0]=envia[1]>>4;
   
   usb_put_packet(1,envia,4,USB_DTS_TOGGLE);
}

//////////////////////////////////////////
//busca diferencias en los botones para enviar solamente
//los que hayan cambiado
//el subprograma trada 2.33uS si no hay diferencias
//////////////////////////////////////////
void busca_diferencias()
{   diferencias=matriz1^matriz1bis;
   if(diferencias!=0)
      {   contador=0x00;
         while(contador<=0x07)
         { if(bit_test(diferencias,0))
            {   envia[2]=contador;
               bitT=bit_test(matriz1,contador);
               envia[3]=0x7F*bitT;
               envianota();
            }
         contador++;
         rotate_right(&diferencias,1);

      }
      }
   diferencias=matriz2^matriz2bis;
   if(diferencias!=0)
      {   contador=0x08;
         while(contador<=0x0F)
         { if(bit_test(diferencias,0))
            {   envia[2]=contador;
               bitT=bit_test(matriz2,(contador-8));
               envia[3]=0x7F*bitT;
               envianota();
            }
         contador++;
         rotate_right(&diferencias,1);

      }
      }
   diferencias=matriz3^matriz3bis;
   if(diferencias!=0)
      {   contador=0x10;
         while(contador<=0x17)
         { if(bit_test(diferencias,0))
            {   envia[2]=contador;
               bitT=bit_test(matriz3,(contador-16));
               envia[3]=0x7F*bitT;
               envianota();
            }
         contador++;
         rotate_right(&diferencias,1);

      }
      }
   diferencias=matriz4^matriz4bis;
   if(diferencias!=0)
      {   contador=0x18;
         while(contador<=0x1F)
         { if(bit_test(diferencias,0))
            {   envia[2]=contador;
               bitT=bit_test(matriz4,(contador-24));
               envia[3]=0x7F*bitT;
               envianota();
            }
         contador++;
         rotate_right(&diferencias,1);

      }
      }
}
////////////////////////////////////////////////////
//interrupción para escaneo de botones
//tarda 25uS+retardo_rebote_botón
////////////////////////////////////////////////////
#int_RB
void RB_isr()
{
   matriz1bis=matriz1;
   matriz2bis=matriz2;
   matriz3bis=matriz3;
   matriz4bis=matriz4;
   LATB=PORTB;
   delay_ms(20);
   if(bit_test(PORTB,7)&&bit_test(PORTB,6)&&bit_test(PORTB,5)&&bit_test(PORTB,4)){
      matriz1=matriz2=matriz3=matriz4=0;
      busca_diferencias();
      return;
   }
   output_high(PIN_A2);
   output_high(PIN_A3);
   output_high(PIN_A4);
   output_high(PIN_A5);
   output_high(PIN_C0);
   output_high(PIN_C1);
   output_high(PIN_C2);
   delay_cycles(10);
   output_low(PIN_C6);
   delay_cycles(10);
   if(!(bit_test(PORTB,7)&&bit_test(PORTB,6)&&bit_test(PORTB,5)&&bit_test(PORTB,4)))
   {
      if(!bit_test(PORTB,7)) bit_set(matriz4,4);
      else bit_clear(matriz4,4);
      if(!bit_test(PORTB,6)) bit_set(matriz4,5);
      else bit_clear(matriz4,5);
      if(!bit_test(PORTB,5)) bit_set(matriz4,6);
      else bit_clear(matriz4,6);
      if(!bit_test(PORTB,4)) bit_set(matriz4,7);
      else bit_clear(matriz4,7);
      delay_cycles(10);

   }
   output_low(PIN_C2);
   delay_cycles(10);
   output_high(PIN_C6);
   delay_cycles(10);
   if(!(bit_test(PORTB,7)&&bit_test(PORTB,6)&&bit_test(PORTB,5)&&bit_test(PORTB,4)))
   {
      if(!bit_test(PORTB,7)) bit_set(matriz4,0);
      else bit_clear(matriz4,0);
      if(!bit_test(PORTB,6)) bit_set(matriz4,1);
      else bit_clear(matriz4,1);
      if(!bit_test(PORTB,5)) bit_set(matriz4,2);
      else bit_clear(matriz4,2);
      if(!bit_test(PORTB,4)) bit_set(matriz4,3);
      else bit_clear(matriz4,3);
      delay_cycles(10);

   }
   output_low(PIN_C1);
   delay_cycles(10);
   output_high(PIN_C2);
   delay_cycles(10);
   if(!(bit_test(PORTB,7)&&bit_test(PORTB,6)&&bit_test(PORTB,5)&&bit_test(PORTB,4)))
   {
      if(!bit_test(PORTB,7)) bit_set(matriz3,4);
      else bit_clear(matriz3,4);
      if(!bit_test(PORTB,6)) bit_set(matriz3,5);
      else bit_clear(matriz3,5);
      if(!bit_test(PORTB,5)) bit_set(matriz3,6);
      else bit_clear(matriz3,6);
      if(!bit_test(PORTB,4)) bit_set(matriz3,7);
      else bit_clear(matriz3,7);
      delay_cycles(10);
   
   }
   output_low(PIN_C0);
   delay_cycles(10);
   output_high(PIN_C1);
   delay_cycles(10);
   if(!(bit_test(PORTB,7)&&bit_test(PORTB,6)&&bit_test(PORTB,5)&&bit_test(PORTB,4)))
   {
      if(!bit_test(PORTB,7)) bit_set(matriz3,0);
      else bit_clear(matriz3,0);
      if(!bit_test(PORTB,6)) bit_set(matriz3,1);
      else bit_clear(matriz3,1);
      if(!bit_test(PORTB,5)) bit_set(matriz3,2);
      else bit_clear(matriz3,2);
      if(!bit_test(PORTB,4)) bit_set(matriz3,3);
      else bit_clear(matriz3,3);
      delay_cycles(10);
   
   }
   output_low(PIN_A5);
   delay_cycles(10);
   output_high(PIN_C0);
   delay_cycles(10);
   if(!(bit_test(PORTB,7)&&bit_test(PORTB,6)&&bit_test(PORTB,5)&&bit_test(PORTB,4)))
   {
      if(!bit_test(PORTB,7)) bit_set(matriz2,4);
      else bit_clear(matriz2,4);
      if(!bit_test(PORTB,6)) bit_set(matriz2,5);
      else bit_clear(matriz2,5);
      if(!bit_test(PORTB,5)) bit_set(matriz2,6);
      else bit_clear(matriz2,6);
      if(!bit_test(PORTB,4)) bit_set(matriz2,7);
      else bit_clear(matriz2,7);
      delay_cycles(10);
   
   }
   output_low(PIN_A4);
   delay_cycles(10);
   output_high(PIN_A5);
   delay_cycles(10);
   if(!(bit_test(PORTB,7)&&bit_test(PORTB,6)&&bit_test(PORTB,5)&&bit_test(PORTB,4)))
   {
      if(!bit_test(PORTB,7)) bit_set(matriz2,0);
      else bit_clear(matriz2,0);
      if(!bit_test(PORTB,6)) bit_set(matriz2,1);
      else bit_clear(matriz2,1);
      if(!bit_test(PORTB,5)) bit_set(matriz2,2);
      else bit_clear(matriz2,2);
      if(!bit_test(PORTB,4)) bit_set(matriz2,3);
      else bit_clear(matriz2,3);
      delay_cycles(10);
   
   }
   output_low(PIN_A3);
   delay_cycles(10);
   output_high(PIN_A4);
   delay_cycles(10);
   if(!(bit_test(PORTB,7)&&bit_test(PORTB,6)&&bit_test(PORTB,5)&&bit_test(PORTB,4)))
   {
      if(!bit_test(PORTB,7)) bit_set(matriz1,4);
      else bit_clear(matriz1,4);
      if(!bit_test(PORTB,6)) bit_set(matriz1,5);
      else bit_clear(matriz1,5);
      if(!bit_test(PORTB,5)) bit_set(matriz1,6);
      else bit_clear(matriz1,6);
      if(!bit_test(PORTB,4)) bit_set(matriz1,7);
      else bit_clear(matriz1,7);
      delay_cycles(10);
   
   }
   output_low(PIN_A2);
   delay_cycles(10);
   output_high(PIN_A3);
   delay_cycles(10);
   if(!(bit_test(PORTB,7)&&bit_test(PORTB,6)&&bit_test(PORTB,5)&&bit_test(PORTB,4)))
   {
      if(!bit_test(PORTB,7)) bit_set(matriz1,0);
      else bit_clear(matriz1,0);
      if(!bit_test(PORTB,6)) bit_set(matriz1,1);
      else bit_clear(matriz1,1);
      if(!bit_test(PORTB,5)) bit_set(matriz1,2);
      else bit_clear(matriz1,2);
      if(!bit_test(PORTB,4)) bit_set(matriz1,3);
      else bit_clear(matriz1,3);
      delay_cycles(10);
   
   }
      output_low(PIN_A2);
      output_low(PIN_A3);
      output_low(PIN_A4);
      output_low(PIN_A5);
      output_low(PIN_C0);
      output_low(PIN_C1);
      output_low(PIN_C2);
      output_low(PIN_C6);
      LATB=PORTB;
      busca_diferencias();
      return;
}

///////////////////////////////////////////
//programa principal
///////////////////////////////////////////
void main(void) 
{   delay_ms(1000);
   set_tris_A(0b00000011);      //PORTA todo salidas, menos AN0 y AN1
   set_tris_B(0b11111111);      //PORTB entradas
   set_tris_C(0b10000000);      //RC7 como entrada
   port_B_pullups(TRUE);
   LATB=PORTB;
   output_C(0);
   PORTA=0;
   tempoA11=tempoA2=tempoB11=tempoB2=0;
   leds1=leds2=leds3=0;
   setup_adc_ports(AN0_TO_AN1);
   setup_adc(ADC_CLOCK_INTERNAL);
   
      usb_init_cs();

   INTCON=0;
   enable_interrupts(INT_RB);   //Interrupciones por cambio en RB7 a RB4
   enable_interrupts(global);
   
      while (TRUE) {
         usb_task();

         if(usb_enumerated()) 
      {
            //if (usb_kbhit(2)) 
         {
                //if(bit_test(PORTA,1)) bit_clear(PORTA,1);
            //else bit_set(PORTA,1);
               //usb_get_packet(2,recibe,8);
                    
            }         
      
//lectura del potenciometro del pitch A
           set_adc_channel(0);
         delay_us(30);
         tempoA1=read_adc();
         tempoA11=tempoA1/2;      //convierte del rango
                           //0xFF a 0x7F para Midi
         if(tempoA11==0x3F) bit_set(leds2,6);  //tempoA centrado a cero
         else bit_clear(leds2,6);

//comprueba si ha cambiado el tempo para enviarlo         
         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;
            }
//lectura del potenciometro del pitch B
           set_adc_channel(1);
         delay_us(30);
         tempoB1=read_adc();
         tempoB11=tempoB1/2;      //convierte del rango 
                           //0xFF a 0x7F para Midi
         if(tempoA11==0x3F) bit_set(leds2,7);  //tempoA centrado a cero
         else bit_clear(leds2,7);

//comprueba si ha cambiado el tempo para enviarlo
         if(tempoB11!=tempoB2)
            {   tempB[1]=0xB0;
               tempB[0]=tempB[1]>>4;
               tempB[2]=0x01;
               tempB[3]=tempoB11;
               usb_put_packet(1,tempB,4,USB_DTS_TOGGLE);
               tempoB2=tempoB11;
            }
                          
      }
   }
}

////////////////////////////////////////////////////
//interrupción para pulsos encoder
////////////////////////////////////////////////////
#int_EXT2
void EXT2_isr()
{     
      //giro derecha
   if(!bit_test(INTCON2,4))       //entra por flanco bajada
      {   if(bit_test(PORTA,1)) p1=4;
         if(!bit_test(PORTA,1)) p1=1;
      }
      //giro izqierda
   if(bit_test(INTCON2,4))         //entra por flanco subida
      {    if(bit_test(PORTA,1)) p1=3;
         if(!bit_test(PORTA,1)) p1=2;
      }
   
   INTEDGE=!INTEDGE;
   
   if(p1==2||p1==4) encoderA=0x01;
   if(p1==1||p1==3) encoderA=0x7F;
   //if(encoderA>0x7F) encoderA=0x01;
   //if(encoderA<0x01) encoderA=0x7F;

   pulsos[3]=encoderA;
   usb_put_packet(1,pulsos,4,USB_DTS_TOGGLE);
}

//////////////////////////////////////////////
// Leds
//////////////////////////////////////////////
if (usb_kbhit(1)) 
{
  usb_get_packet(1,recibe,8);
}
int32 leds;
#locate leds=0x50
#byte leds1=0x50
#byte leds2=0x51
#byte leds3=0x52

/////////////////////////////////////////
//subprograma para lectura leds
////////////////////////////////////////

void detecta_led()
{	leds1copia=leds1;
	leds2copia=leds2;
	leds3copia=leds3;	
	recibe[2]=recibe[2]-32;
	if(recibe[3]==127) bit_clear(leds,recibe[2]);		//encender leds
	if(recibe[3]==0) bit_set(leds,recibe[2]);		//apagar leds
	
	if(leds1!=leds1copia){
		i2c_start();
		i2c_write(0x40);
		i2c_write(leds1);
		i2c_stop();
		}	
	if(leds2!=leds2copia){
		i2c_start();
		i2c_write(0x42);
		i2c_write(leds2);
		i2c_stop();
		}	
	if(leds3!=leds3copia){
		i2c_start();
		i2c_write(0x44);
		i2c_write(leds3);
		i2c_stop();
		}	
}

Pero cuando lo compilo (Mpalb 8.92, CCS 5.015), me tira los siguientes errores:

Código:
Clean: Deleting intermediary and output files.
Clean Warning: File "D:\Archivos\manuales\Controlador Fueguito 01\controlador midi\MIDI_12.o" doesn't exist.
Clean: Deleted file "MIDI_12.ERR".
Clean: Done.
Executing: "C:\Archivos de programa\PICC\Ccsc.exe" +FH "MIDI_12.c" +DF +LN +T +A +M +Z +Y=9 +EA
>>> Warning 203 "C:\Archivos de programa\PICC\drivers\pic18_usb.c" Line 643(1,1): Condition always TRUE
*** Error 99 "MIDI_12.c" Line 19(5,65): Option invalid   Wrong pins for H/W
*** Error 12 "MIDI_12.c" Line 376(17,24): Undefined identifier   INTCON2
*** Error 12 "MIDI_12.c" Line 377(33,35): Undefined identifier   p1
*** Error 12 "MIDI_12.c" Line 378(33,35): Undefined identifier   p1
*** Error 12 "MIDI_12.c" Line 381(16,23): Undefined identifier   INTCON2
*** Error 12 "MIDI_12.c" Line 382(34,36): Undefined identifier   p1
*** Error 12 "MIDI_12.c" Line 383(33,35): Undefined identifier   p1
*** Error 12 "MIDI_12.c" Line 386(4,11): Undefined identifier   INTEDGE
*** Error 12 "MIDI_12.c" Line 388(7,9): Undefined identifier   p1
*** Error 12 "MIDI_12.c" Line 389(7,9): Undefined identifier   p1
*** Error 12 "MIDI_12.c" Line 393(4,10): Undefined identifier   pulsos
*** Error 12 "MIDI_12.c" Line 394(21,27): Undefined identifier   pulsos
*** Error 43 "MIDI_12.c" Line 400(1,3): Expecting a declaration
*** Error 43 "MIDI_12.c" Line 400(4,5): Expecting a declaration
*** Error 28 "MIDI_12.c" Line 400(15,16): Expecting an identifier
*** Error 43 "MIDI_12.c" Line 400(17,18): Expecting a declaration
*** Error 43 "MIDI_12.c" Line 401(1,2): Expecting a declaration
*** Error 28 "MIDI_12.c" Line 402(18,19): Expecting an identifier
*** Error 48 "MIDI_12.c" Line 402(20,26): Expecting a (
*** Error 43 "MIDI_12.c" Line 402(27,28): Expecting a declaration
*** Error 43 "MIDI_12.c" Line 402(28,29): Expecting a declaration
*** Error 43 "MIDI_12.c" Line 402(29,30): Expecting a declaration
*** Error 43 "MIDI_12.c" Line 403(1,2): Expecting a declaration
*** Error 12 "MIDI_12.c" Line 415(2,12): Undefined identifier   leds1copia
*** Error 12 "MIDI_12.c" Line 416(1,11): Undefined identifier   leds2copia
*** Error 12 "MIDI_12.c" Line 417(1,11): Undefined identifier   leds3copia
*** Error 12 "MIDI_12.c" Line 422(11,21): Undefined identifier   leds1copia
*** Error 53 "MIDI_12.c" Line 424(11,15): Expecting function name  ::
*** Error 53 "MIDI_12.c" Line 425(11,16): Expecting function name  ::
*** Error 12 "MIDI_12.c" Line 428(11,21): Undefined identifier   leds2copia
*** Error 53 "MIDI_12.c" Line 430(11,15): Expecting function name  ::
*** Error 53 "MIDI_12.c" Line 431(11,16): Expecting function name  ::
*** Error 12 "MIDI_12.c" Line 434(11,21): Undefined identifier   leds3copia
*** Error 53 "MIDI_12.c" Line 436(11,15): Expecting function name  ::
*** Error 53 "MIDI_12.c" Line 437(11,16): Expecting function name  ::
      35 Errors,  1 Warnings.
Build Failed.
Halting build on first failure as requested.
BUILD FAILED: Wed Aug 31 11:20:15 2016

me exede por completo :/
 
Se ve que tomaste como referencia el archivo MIDI_12.c, y los errores que existen deben ser porque agregaste código de otros programas.
El programa del archivo MIDI_12.c compila bien, pero modificando el error que tiene en la configuración del módulo I2C.

Adjunto el archivo con los cambios. No sé si funcione ya que no estoy metido en ésto.

Nota:
Compilado con CCS PIC C Compiler v 5.062

Suerte.
 

Adjuntos

  • MIDI_12.rar
    107 KB · Visitas: 30
Gracias D@rk, lo probé y compiló perferctamente, ahora voy a ver como agrego las cosas que faltan a ver si sale funcionando algo, asi que si tira errores es culpa mia :D cualquier duda que se me presente volveré a preguntar por aca.

Gracias de nuevo.
 
hola rachelies como andas, mira no se si a ti se te ha presentado un problema que se me sucede a mi. el controlador funciona perfectamente en mi pc de escritorio pero en mi portátil algunas veces se cuelga, el pic sigue trabajando pero el programa en este caso guitar rig no me recibe los datos que manda el pic y ya he probado en varios portátiles y pasa lo mismo.


crooin mira el archivo que coloca rachelies es el mas importante ahoramismo no estoy en el pc donde tengo el archivo de ccs primero que todo si has manejado usb de pic?
Buena noche. Un gusto leer tantos buenos consejos.
T
engo el mismo inconveniente que el compañero.
M
i máquina funciona perfecto, pero si dejo de tocarla por menos de un minuto, se inhabilita, sigue apareciendo el controlador midi pero queda congelado.
Les agradecería cualquier información al respecto...
 
Última edición por un moderador:
Atrás
Arriba