0

I am experimenting an strange problem with C30 and MPLAB X, I have this piece of code:

unsigned char mode;
switch(mode){                       // Eligo el modo que se envio a traves del UART
        case FRECUENCIMETER:
            vFrecuencimetro();
            break;

        case LC_METER:
            vLC_Meter();
            break;

        case VOLTMETER:
            break;

        case 'L':
            vLogicAnalizer();
            break;
    }
    if(mode == 'L'){
        vLogicAnalizer();
    }

When mode is equal to 'L' character the switch doesn't enter in "case 'L'" as it should but it enters in the if sentece. But when I change my code on this way:

unsigned char mode;
    switch(mode){                     
        case 'L':
            vLogicAnalizer();
            break;
    }
    if(mode == 'L'){
        vLogicAnalizer();
    }

It enters perfectly on the switch statement. The other cases are defines like this:

#define FRECUENCIMETER      0
#define LC_METER            1
#define VOLTMETER           3

I am using C30 compiler v3.31 and MPLAB X v1.41. Hope you can help me.

Thank you


EDIT:

Ok according to the answer and comments I have changed my code on this way:

char mode;

#define FRECUENCIMETER      0
#define LC_METER            1
#define LOGIC_ANALIZER      76    // 'L'
#define VOLTMETER           3
#define NO_MODE             4

switch(mode){                       // Eligo el modo que se envio a traves del UART
            case FRECUENCIMETER:
            vFrecuencimetro();
            break;

        case LC_METER:
            vLC_Meter();
            break;

        case VOLTMETER:
            break;

        case LOGIC_ANALIZER:
            vLogicAnalizer();
            break;
    }

If before the switch statement I write:

mode = 'L';

The switch statement works perfectly, but when I get an 'L' from UART with the function ReadUART1() which returns and unsigned int:

unsigned int ReadUART1(void);

And it returns 'L' according to my debugger which correspond to number 76 in unsigned int the switch statement doesn't work but mode is still 'L'. Even if I change mode to unsigned int to be the same as the function it doesn't work. I think I will have to use if() statements.

Andres
  • 6,080
  • 13
  • 60
  • 110
  • 1
    and you're sure it isn't matching one of the other 'cases'? (hint: move `case 'L':` to the beginning of switch) – KevinDTimm Feb 28 '13 at 22:38
  • No, it doesn't match any other case. Cast to (int) worked perfectly. But if I put it on a #define LOGIC_ANALIZER ((int)'L') it doesn't work anymore – Andres Feb 28 '13 at 22:40
  • 1
    There's probably a 'promotion' issue as switch and case are supposed to take an `int`. So, change `mode` to `int` and also `case (int)'L':` and try that – KevinDTimm Feb 28 '13 at 22:44
  • 1
    When type promotions occur, the values/literals for each case are all converted to the type used by the `switch()` call. – Cloud Feb 28 '13 at 23:46
  • See my edits please, I changed everything to the same data type – Andres Mar 01 '13 at 00:25
  • Ok, why the negative vote? – Andres Mar 01 '13 at 18:51

1 Answers1

0

Solved. I changed:

unsigned int mode;

To:

volatile unsigned int mode;

Or use with compiler optimization level 0:

unsigned int mode;

Thank you for your help.

Andres
  • 6,080
  • 13
  • 60
  • 110