This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

_RBYTE problem - reads double when data is >7Fh ?

Hello,

Below is a sample code for main.c, which writes 3 different SFR locations with various values.
What I noticed, and don't have explanation for, is that when the data is >7Fh, the _rbyte reads it as double, not char.

main.c

#include <REG51XC.H>

sfr SFR_REG1 = 0xF8;
sfr SFR_REG2 = 0xF9;
sfr SFR_REG3 = 0x91;

void main (void) {


    SFR_REG1 = 0x01;
    SFR_REG1 = 0x11;
    SFR_REG1 = 0x20;
    SFR_REG1 = 0x30;
    SFR_REG1 = 0x40;
    SFR_REG1 = 0x50;
    SFR_REG1 = 0x60;
    SFR_REG1 = 0x70;
    SFR_REG1 = 0x77;
    SFR_REG1 = 0x80;
    SFR_REG1 = 0x81;
    SFR_REG1 = 0xF1;
    SFR_REG1 = 0xFF;
    SFR_REG1 = 0x27;

    SFR_REG2 = 0x01;
    SFR_REG2 = 0x11;
    SFR_REG2 = 0x20;
    SFR_REG2 = 0x30;
    SFR_REG2 = 0x40;
    SFR_REG2 = 0x50;
    SFR_REG2 = 0x60;
    SFR_REG2 = 0x70;
    SFR_REG2 = 0x77;
    SFR_REG2 = 0x80;
    SFR_REG2 = 0x81;
    SFR_REG2 = 0xF1;
    SFR_REG2 = 0xFF;
    SFR_REG2 = 0x27;

    SFR_REG3 = 0x01;
    SFR_REG3 = 0x11;
    SFR_REG3 = 0x20;
    SFR_REG3 = 0x30;
    SFR_REG3 = 0x40;
    SFR_REG3 = 0x50;
    SFR_REG3 = 0x60;
    SFR_REG3 = 0x70;
    SFR_REG3 = 0x77;
    SFR_REG3 = 0x80;
    SFR_REG3 = 0x81;
    SFR_REG3 = 0xF1;
    SFR_REG3 = 0xFF;
    SFR_REG3 = 0x27;

    while (1) {
    }
}

functions.ini

signal void rbyte_test1 (void) {
  char mydata;

  while (1) {
    wwatch(D:0xF8);
    mydata = _RBYTE(D:0xF8);
    printf("0xF8 Read : %x\n",mydata);
  }
}

signal void rbyte_test2 (void) {
  char mydata;

  while (1) {
    wwatch(D:0xF9);
    mydata = _RBYTE(D:0xF9);
    printf("0xF9 Read : %x\n",mydata);
  }
}

signal void rbyte_test3 (void) {
  char mydata;

  while (1) {
    wwatch(D:0x91);
    mydata = _RBYTE(D:0x91);
    printf("0x91 Read : %x\n",mydata);
  }
}

rbyte_test1();
rbyte_test2();
rbyte_test3();

Here is the output after executing the code :

0xF8 Read : 1
0xF8 Read : 11
0xF8 Read : 20
0xF8 Read : 30
0xF8 Read : 40
0xF8 Read : 50
0xF8 Read : 60
0xF8 Read : 70
0xF8 Read : 77
0xF8 Read : ffffff80
0xF8 Read : ffffff81
0xF8 Read : fffffff1
0xF8 Read : ffffffff
0xF8 Read : 27
0xF9 Read : 1
0xF9 Read : 11
0xF9 Read : 20
0xF9 Read : 30
0xF9 Read : 40
0xF9 Read : 50
0xF9 Read : 60
0xF9 Read : 70
0xF9 Read : 77
0xF9 Read : ffffff80
0xF9 Read : ffffff81
0xF9 Read : fffffff1
0xF9 Read : ffffffff
0xF9 Read : 27
0x91 Read : 1
0x91 Read : 11
0x91 Read : 20
0x91 Read : 30
0x91 Read : 40
0x91 Read : 50
0x91 Read : 60
0x91 Read : 70
0x91 Read : 77
0x91 Read : ffffff80
0x91 Read : ffffff81
0x91 Read : fffffff1
0x91 Read : ffffffff
0x91 Read : 27

Any suggestions ?

Thanks in advance.

Parents
  • What I noticed, and don't have explanation for, is that when the data is >7Fh, the _rbyte reads it as double, not char.

    No. Double has got nothing to do with this. Signed and unsigned integers, however, do.

    What you're seeing is a consequence of your choice of variable type to hold those values in, and your chosen printf() format specifier. Look up what %x actually expects for input and compare it to what you're giving it. Look up the range of 'char' in debugger function. You'll find that there's simply no way your 'char' can hold any value >0x7F.

    Lessen to be taken from this: you hardly ever actually want to use un-adorned "char". MISRA C has a rule forbidding it for just this reason.

Reply
  • What I noticed, and don't have explanation for, is that when the data is >7Fh, the _rbyte reads it as double, not char.

    No. Double has got nothing to do with this. Signed and unsigned integers, however, do.

    What you're seeing is a consequence of your choice of variable type to hold those values in, and your chosen printf() format specifier. Look up what %x actually expects for input and compare it to what you're giving it. Look up the range of 'char' in debugger function. You'll find that there's simply no way your 'char' can hold any value >0x7F.

    Lessen to be taken from this: you hardly ever actually want to use un-adorned "char". MISRA C has a rule forbidding it for just this reason.

Children