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
  • It's not read as double, simply as a signed long (or integer).

    You should definitely read about integer promotion rules.

    The fix is to tell printf, that you're dealing with unsigned data. Use ubyte (unsigned char) instead of char.

    Also try:

    printf("0x91 Read : %hhx\n",mydata);
    

    I'd prefer the output like this, though:

    printf("0x91 Read : %#02hhx\n",mydata);
    

    Check the printf(3) manual page of any non-windows operating system to learn how to use printf.

Reply
  • It's not read as double, simply as a signed long (or integer).

    You should definitely read about integer promotion rules.

    The fix is to tell printf, that you're dealing with unsigned data. Use ubyte (unsigned char) instead of char.

    Also try:

    printf("0x91 Read : %hhx\n",mydata);
    

    I'd prefer the output like this, though:

    printf("0x91 Read : %#02hhx\n",mydata);
    

    Check the printf(3) manual page of any non-windows operating system to learn how to use printf.

Children
No data