0

I have written the following piece of code to write the value '9' as an integer to, and read from, the 24LC512 EEPROM.

uint8_t dataByteIn  = 0x39;
uint8_t dataByteOut = 0;

To write:

HAL_I2C_Mem_Write(&hi2c1,(0x50 << 1), 0x00, I2C_MEMADD_SIZE_16BIT, &dataByteIn, 1, 1000);

To read:

HAL_I2C_Mem_Read(&hi2c1, (0x50 << 1), 0x00, I2C_MEMADD_SIZE_16BIT, &dataByteOut, 1, 1000);

I have no issue with reading or writing itself, but the actual representation of the values after reading is baffling to me. The reason dataByteIn is 0x39 (the hex value for the decimal value '9'), is because I've found its the only way to write and read back decimal numbers. What I'm trying to achieve is initialise the data I want to write, as an uint8_t in its decimal value i.e. uint8_t dataByteIn = 9;, then read it back as '9'. How would I achieve this?

I am reading the value on a serial terminal using USART:

HAL_UART_Transmit(&huart3, &dataByteOut, sizeof(dataByteOut), 1000);
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
S23
  • 119
  • 12
  • 1
    Does `uint8_t dataByteIn = '9';` work for you? That is, you want to write the ASCII (printable) value to the eeprom (i.e. `0x39`) rather than the _binary_ value `0x09`? Or, if not, do you want to send/receive the binary value but make it printable in progress/debug messages (which seems more common to me)? – Craig Estey Aug 24 '23 at 20:44
  • After rereading your question, I'm thinking the second option. Send 0x09 and get back the binary. Then, to print, (e.g.) `printf("%2.2X %c\n",rval,rval);` or `putchar(rval + '0');` – Craig Estey Aug 24 '23 at 20:53
  • 1
    I can't really understand your question. Do you want to write value `0x9` but read back `0x39`? That's not how any type of memory work. You read what you have written. Why not to initialize `uint8_t dataByteIn = '9'` if you want to store decimal representation? Regarding second, you can either store length of string (one byte or more, depending on maximal required length) and then characters itself, or write special 'end of string' marker, e.g. `0`. In former case you will read fixed size length, then characters. In latter case you will read characters until meet 'end of line'. – dimich Aug 24 '23 at 21:14

0 Answers0