CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

MIDI data problem

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Markdem



Joined: 24 Jun 2005
Posts: 206

View user's profile Send private message Send e-mail

MIDI data problem
PostPosted: Wed Feb 21, 2007 11:47 pm     Reply with quote

hi All, i am tring to make a MIDI to USB converter. I have the hardware all setup OK, but when I start reciving the data, I only get the first byte. the other two bytes are never what they sould be, and mostly come out as 0x00.
Sometimes, even the first byte will be somthing that it should not be.

The first byte of data should be a 0x90 for a note on, and 0x80 for a note off. This sometimes happends, but the next byte should be 0x60 for middle C, but it almost always is 0x00. Sometimes I will get 0x40.

The following is the code i am using;

Code:

#include <18F4550.h>
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN,NOBROWNOUT
#use delay(clock=48000000)

#use rs232(baud=31250, BITS=8, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

#define USB_CON_SENSE_PIN    PIN_D1

#include <STDLIB>
#include <usb_cdc.h>
         
int1 RXReady;
int i;
int BeatMask[33];
int MIDIData[4];



#int_rda
void MIDIIn()
   {
      RXReady = TRUE;

      MIDIData[0] = getc();
      MIDIData[1] = getc();
      MIDIData[2] = getc();
   }

void main()
   {
      delay_ms(100);
      
      
      RXReady = 0;

      usb_init_cs();

      usb_task();
   
      enable_interrupts(INT_RDA);
      enable_interrupts(GLOBAL);

      while(1)
         {
            usb_task();
                                       
            if (RXReady)
               {   
                  RXReady = FALSE;
                     
                  if(bit_test(MIDIData[0],7))                  //Check to see if the first byte is the start of a MIDI message
                     {
                        printf(usb_cdc_putc,"%X, %X, %X\r\n",MIDIData[0], MIDIData[1], MIDIData[2]);
                        RXReady = FALSE;
                     }
               }

         }
   }



Also, you may notice that i am doing serial comms inside the ISR, and i know that is wrong, but if i just try to set the RXReady flag, the code will never start to get the data from the buffer.

Lastly, i did have a for loop for the serial comms, but removed it for testing.

I think I might have some problems with data buffing, but i thought that the pic had a 3 byte hardware buffer, which would mean i should never lose data.

Can anyone see what i am doing wrong.

Thank you for you help.
mbge5amw



Joined: 13 Dec 2004
Posts: 15
Location: Yorkshire, UK

View user's profile Send private message MSN Messenger

PostPosted: Thu Feb 22, 2007 2:37 am     Reply with quote

one potential problem I could see would be if the unit starts running after the MIDI stream has begun, or if a byte is lost, in this case the unit may not read characters 0,1,2 of the MIDI stream, instead it may read 1,2,0 or 2,0,1.

I would suggest reading 1 character at a time in the interrupt routine (otherwise it will be stuck in there from reception of the first character to reception of the third character)
In this interrupt routine check the MSB and if high then reset a character counter to 0, otherwise increment the character counter.
When your character counter indicates three characters have been received and you check that the first character had valid synch bit set a flag to get the main routine to decode the data.

If you think there may be a chance that you are off doing USB routines for long enough for a new character to come in for the start of the next MIDI command before you have processed this current command then it may also be wise to copy all 3 bytes to a separate buffer at the end of the ISR.

HTH

Andy
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group