Search code examples
switch-statementmsp430

Switch statement jumps from one case to another


This is a part of a project for MSP430 microcontroller. My problem is that this switch statement jumps from case 1: (and case 2: as well) to case 11: with no apparent reason. I tried debugging it, the switch value selected_cmd is not changing during the process. The variable selected_cmd is a global variable, its value is set in another function. If I put simple statement selected_cmd = 2 before this switch statement, it works as expected. If selected_cmd is set programmatically, it jumps from one case to another. How to solve this?

switch (selected_cmd)
{
    case 0: // set light level
    {
        DALI_MsgBuf[1] = value;
        DALI_FF = DALI_MsgBuf[1] | (DALI_MsgBuf[0] << 8);
        DALI_Transmit(DALI_FF);
        timer_sleep_ms(5);
        break;
    }
    case 1: // turn off
    {
        DALI_FF = TURN_OFF | ((DALI_MsgBuf[0] << 8) | 0x01);
        DALI_Transmit(DALI_FF);
        timer_sleep_ms(5);
        break;
    }
    case 2: // recall max
    {
        DALI_FF = RECALL_MAX_LEVEL | ((DALI_MsgBuf[0] << 8) | 0x01);
        DALI_Transmit(DALI_FF);
        timer_sleep_ms(5);
        break;
    }
    case 3: // recall min
    {
        DALI_FF = RECALL_MIN_LEVEL | ((DALI_MsgBuf[0] << 8) | 0x01);
        DALI_Transmit(DALI_FF);
        timer_sleep_ms(5);
        break;
    }
    case 4: // store max lvl
    {
        DALI_FF = (DATA_TRANSFER_REGISTER << 8) | value;
        DALI_Transmit(DALI_FF);
        timer_sleep_ms(5);
        DALI_FF = STORE_THE_DTR_AS_MAX_LEVEL | ((DALI_MsgBuf[0] << 8) | 0x01);
        DALI_Transmit(DALI_FF);
        timer_sleep_ms(5);
        DALI_Transmit(DALI_FF);
        timer_sleep_ms(5);
        break;
    }
    case 5: // store min lvl
    {
        DALI_FF = (DATA_TRANSFER_REGISTER << 8) | value;
        DALI_Transmit(DALI_FF);
        timer_sleep_ms(5);
        DALI_FF = STORE_THE_DTR_AS_MIN_LEVEL | ((DALI_MsgBuf[0] << 8) | 0x01);
        DALI_Transmit(DALI_FF);
        timer_sleep_ms(5);
        DALI_Transmit(DALI_FF);
        timer_sleep_ms(5);
        break;
    }
    case 6: // set fade rate
    {
        DALI_FF = (DATA_TRANSFER_REGISTER << 8) | value;
        DALI_Transmit(DALI_FF);
        timer_sleep_ms(5);
        DALI_FF = STORE_THE_DTR_AS_FADE_RATE | ((DALI_MsgBuf[0] << 8) | 0x01);
        DALI_Transmit(DALI_FF);
        timer_sleep_ms(5);
        DALI_Transmit(DALI_FF);
        timer_sleep_ms(5);
        break;
    }
    case 7: // set fade time
    {
        DALI_FF = (DATA_TRANSFER_REGISTER << 8) | value;
        DALI_Transmit(DALI_FF);
        timer_sleep_ms(5);
        DALI_FF = STORE_THE_DTR_AS_FADE_TIME | ((DALI_MsgBuf[0] << 8) | 0x01);
        DALI_Transmit(DALI_FF);
        timer_sleep_ms(5);
        DALI_Transmit(DALI_FF);
        timer_sleep_ms(5);
        break;
    }
    case 8: // add to group
    {
        DALI_FF = (ADD_TO_GROUP_0 | value) | ((DALI_MsgBuf[0] << 8) | 0x01);
        DALI_Transmit(DALI_FF);
        timer_sleep_ms(5);
        DALI_Transmit(DALI_FF);
        timer_sleep_ms(5);
        break;
    }
    case 9: // remove from group
    {
        DALI_FF = (REMOVE_FROM_GROUP_0 | value) | ((DALI_MsgBuf[0] << 8) | 0x01);
        DALI_Transmit(DALI_FF);
        timer_sleep_ms(5);
        DALI_Transmit(DALI_FF);
        timer_sleep_ms(5);
        break;
    }
    case 10: // store as scene
    {
        DALI_FF = STORE_ACTUAL_LEVEL_IN_THE_DTR | ((DALI_MsgBuf[0] << 8) | 0x01);
        DALI_Transmit(DALI_FF);
        timer_sleep_ms(5);
        DALI_FF = (STORE_THE_DTR_AS_SCENE_0 | value) | ((DALI_MsgBuf[0] << 8) | 0x01);
        DALI_Transmit(DALI_FF);
        timer_sleep_ms(5);
        DALI_Transmit(DALI_FF);
        timer_sleep_ms(5);
        break;
    }
    case 11:    // go to scene
    {
        DALI_FF = (GO_TO_SCENE_0 | value) | ((DALI_MsgBuf[0] << 8) | 0x01);
        DALI_Transmit(DALI_FF);
        timer_sleep_ms(5);
        break;
    }
    case 12:    // remove from scene
    {
        DALI_FF = (REMOVE_FROM_SCENE_0 | value) | ((DALI_MsgBuf[0] << 8) | 0x01);
        DALI_Transmit(DALI_FF);
        timer_sleep_ms(5);
        DALI_Transmit(DALI_FF);
        timer_sleep_ms(5);
        break;
    }
    case 13:    // reset
    {
        DALI_FF = RESET | ((DALI_MsgBuf[0] << 8) | 0x01);
        DALI_Transmit(DALI_FF);
        timer_sleep_ms(5);
        DALI_Transmit(DALI_FF);
        timer_sleep_ms(5);
        break;
    }
    default: break;
}

Solution

  • Use

    switch (selected_cmd)
    {
        case 0: // set light level
            DALI_MsgBuf[1] = value;
            DALI_FF = DALI_MsgBuf[1] | (DALI_MsgBuf[0] << 8);
            DALI_Transmit(DALI_FF);
            timer_sleep_ms(5);
            break;
        case 1: // turn off
            DALI_FF = TURN_OFF | ((DALI_MsgBuf[0] << 8) | 0x01);
            DALI_Transmit(DALI_FF);
            timer_sleep_ms(5);
            break;
        case 2: // recall max
            DALI_FF = RECALL_MAX_LEVEL | ((DALI_MsgBuf[0] << 8) | 0x01);
            DALI_Transmit(DALI_FF);
            timer_sleep_ms(5);
            break;
        ...
    

    Do not use brace blocks after a case: statement

    Do you call this function from an interrupt. Then the debugger might get confused when a new interupt enters the method again.