Click here to Skip to main content
15,917,174 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I was wondering if I could ask you for some help, i'm playing with a pic USB interface tutorial, where an unsigned char is written to the firmware from a c++ application.

C++
ProcessIO(void)
{
if((USBDeviceState < CONFIGURED_STATE)||(USBSuspendControl==1)) return;
       if(!HIDRxHandleBusy(USBOutHandle))                                    
           {
             switch(RecievedDataBuffer[0])
              {
                 case 0x01:          //do something          
           break;
                 case 0x02:          //do something
           break;
              }
	USBOutHandle = HIDRxPacket(HID_EP,(BYTE*)&ReceivedDataBuffer,64);
    }
} 


The problem is the RecievedDataBuffer[0] is tested through switch statements. I have about 60 different commands to be sent VIA usb to the firmware from a c++ application (the code I am talking about is the firmware for a PIC 18F), the only difference being the hexi code stored in the DataRecievedBuffer[0]. So instead of writing 60 switch statements, only with different hex values, I would just like to replace the switch statement above to the code below:

MIDL
j = ReceivedDataBuffer[0];             //unsigned char j = recieved data
        upper_nibble = j & 0x0f0;              //bit mask lower bits
        PORTB = upper_nibble;
        DelayMs(100);

        RB1 = 1;                               //pulse enable line
        DelayMs(100);
        RB1 = 0;
        DelayMs(100);

        PORTB = 0x00;
        k = (j << 4) | (j >> 4);               //swap nibbles
        lower_nibble = k & 0x0f0;              //again bit mask to leave the lower nibble         PORTB = lower_nibble;

        DelayMs(100);
        RB1 = 1;                               //pulse enable and reset
        DelayMs(100);
        RB1 = 0;
        DelayMs(100);
        PORTB = 0x00;                          //clear port



So i'm looking to send each command recieved in the buffer to the pins of my PIC, but the program crashes...Do you have any suggestion of how I could fix this?

OK...I think this is pretty unclear...At the moment, there is a switch statement between:
VB
if(!HIDRxHandleBusy(USBOutHandle))

...and
MIDL
USBOutHandle = HIDRxPacket(HID_EP,(BYTE*)&amp;ReceivedDataBuffer,64);

All i would like to do is replace this switch statement with the second block of code down from the top of this page, but it seems like when ever I try to put a statement other than the switch statement above, the system is completly unresponsive. I don't have an in-circuit degugger so I'm doing this blindly. I was just wondering if somebody could see something fundamentally wrong in replacing:
MIDL
switch(RecievedDataBuffer[0])

with something like:
C++
If( (RecievedDataBuffer[0]) = 0x02)

Thanks

Martyn
Posted
Updated 28-Jul-11 0:28am
v4
Comments
Richard MacCutchan 28-Jul-11 5:04am    
Your question makes no sense; what does this code have to do with a switch statement?
kutalinelucas 28-Jul-11 5:16am    
Sorry, I guess formatting doesn't work on the submit comment part so I can't illustrate my point with code, and I was given a row yesterday for submitting a follow up question in the 'solution' bit....if you look at the top code, a hex value is recieved in unsigned char RecievedDataBuffer[0]. Then the program enters a large switch statement...ie switch(RecievedDataBuffer[0])...case 0x01: etc....As I have 60-odd possible hex values to be sent to the firmware, and the only difference being the hex value, I would like to replace a massive switch statement with the bottom code, which simply assigns the contents of 'RecievedDataBuffer' to 'j', so i can break the hex into nibbles and send to portB.
Richard MacCutchan 28-Jul-11 5:49am    
1. You can use the "Improve Question" button to change your original query.
2. I still don't understand your problem, you want to remove the switch block and replace it with different code; OK go ahead if that will solve the issue. You also say that the program crashes but not where it crashes or what the circumstances are. Are you able to use a debugger to trace the program?
3. When replying please use the "Reply" button, not "Add comment".
kutalinelucas 28-Jul-11 6:30am    
Hi Richard. I updated the question. The problem is I CAN'T replace the switch statement with the different code. I just thought the if statement above would be a direct replacement for the switch statement that works. Again I can't dunug unfortunatly so i'm pretty stuck.
kutalinelucas 28-Jul-11 6:42am    
I'm an idiot...I just left out a '=' in the if statement...hopefully it'll be ok now...thanks

1 solution

its so easy

If( (RecievedDataBuffer[0]) = 0x02)
{
}
else If( (RecievedDataBuffer[0]) = 0x01)
{
}
else
{
cout << "gone to hell";
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900