PIC16f877a switch not reading correctly

157 views Asked by At

I am having a problem with a switch case when using the UART functions. I receive data and store it into the eeprom. I think call a switch statement to see what was sent. I read the eeprom and the information is right but I am just not able to read the right one. It always comes base as an error (default case). I am using Hi-tech C compiler.

unsigned char tempVal;
tempVal = eeprom_read(cmdByteAddr);
switch(tempVal){
    //Get temperature
    case 30:
        writeByte('T');
        break;
    //Get temp high
    case 31:
        writeByte('T');
        writeByte('H');
        break;
    //Get temp low
    case 32:
        writeByte('T');
        writeByte('L');
        break;
    //Get humidity
    case 41:
        writeByte('H');
        break;
    //Get humidity high
    case 42:
        writeByte('H');
        writeByte('H');
        break;
    //Get humidity low
    case 43:
        writeByte('H');
        writeByte('L');
        break;
    //Error
    default:
        writeByte('E');
        writeByte(eeprom_read(cmdByteAddr));
        break;
}
1

There are 1 answers

0
chux - Reinstate Monica On

The value returned from eeprom_read() is not one of your cases. The switch() is working correctly. Adjust code to present a more meaningful error using the same switch variable and not another call to eeprom_read().

default:
    writeByte('E');
    writeByte(tempVal);
    break;  // Not sure why you want `break` here.

If you still get unsatisfactory results, try unsigned tempVal. Sometimes a compiler get confused, although it should not, on sub-int sized data. You may need to writeUnsigned(tempVal) or its equivalent.

You may want to print cmdByteAddr also. Maybe it is outside the EE range.