简体   繁体   中英

PIC16f877a switch not reading correctly

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;
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM