16bit to 8bit to save data in eeprom and read it as a 16 bit value again

Aloolo

Jun 29, 2024
1
Joined
Jun 29, 2024
Messages
1

Stack Overflow

  1. Products

  2. electro_nooobbbb's user avatar
    electro_nooobbbb
  3. 2

Saving 16 bit data to eeprom by converting it to 8 bits modbus stm8l IC

Ask Question
Asked today
Modified today
Viewed 15 times
0
Hello i am pretty new to modbus and this is my first program with it, I have several registers (21) 16bit register and some of them are NVM (non volatile memory) capable.
So, in order to save 16 bit register value to eeprom I am splitting the data into 8 bits such as
[INPUT_SAMPLE] //(basically this is a 16 bit register that samples out the adc between 200-2000 range) default val is 200

If(DUL==YES){
Stm8_eeprom_store_data (u_8 analog[input_samples >>8) ;

Stm8_eeprom_store_data (u_8 analog[input_samples) ;}

Stm_wdt // calling watchdog

I tried this code but it didn't help instead
if (DUL==YES){
Stm8_eeprom_store_data (((u_8)analog[input_samples>>8)+((u_8)analog[input_samples);
}

This code helped but the thing is, I can't store values greater than 255(8bit) my question is that do I need to convert it back to 16bit.
NOTE: I am using stm8l52c6 IC with modbus , the nvm is properly configured everything is working the only thing that's frustrating me is that I can't store val greater than 247-255 (8bit) . To read the configured registers I am using modbus tester & scada admin
I'd appreciate any quick and meaningful response from your end. Thank you in advance.
I was expecting it to store 16bit value but since my eeprom can only read 8 bit values that's why I had to split the 16 bit into 8bit but the thing is now it can store only 8 bit data if I try entering 16bit value for eg 2000; it would output some random value as 208 ...why is that so? Do I need to convert the 8 bit to 16 bit again or what
 

Martaine2005

May 12, 2015
5,276
Joined
May 12, 2015
Messages
5,276
I’m not clued up on this at all so maybe nonsense, but I remember cloning hard drives with the same situation. I could clone a 250Gb onto a 1Tb and only ‘see’ 250Gb. The rest of the available storage space was unavailable.So you might have to change the settings again. Or try different software?.
 

Harald Kapp

Moderator
Moderator
Nov 17, 2011
14,270
Joined
Nov 17, 2011
Messages
14,270
When you need to split a 16 bit word into 2 8 bit words (bytes) and vice versa, one way of doing this in C is this:
C:
uint16 data; //defines a 16 bit datum

uint8 data_high; //defines a byte to store the upper 8 bits of "data"
uint8 data_low;  //defines a byte to store the lower 8 bits of "data"

//split data
data_low = (uint8) (data & 0x00FF); // mask out lower 8 bits
data_high = (uint8) (data >>8);   // shift to the right to access the upper 8 bits

// you can now store the upper and lower byte as separate bytes e.g. in the EEPROM
...
    
//re-assemble data
data = ((uint16) data_low) | ((uint16) (data_high)<<8)

data_low = (uint8) (data & 0xFF);
bitwise and of the 16 bit data with an 8 bit mask 0000 0000 1111 1111 which sets the upper 8 bits to zero, then casts the remaining lower 8 bits to an 8 bit value.

data_high = (uint8) (data >>8);
shifts the 16 bit data 8 positions to the right, then casts the result to an 8 bit value, leaving the former upper 8 bits as a single 8 bit value.

data = ((uint16) data_low) | ((uint16) (data_high)<<8);
casts the 8 bit data_low to a 16 bit value, then merges with the shifted data_high to create a 16 bit value.

Note that I assumes unsigned integers (uint) to avoid messing with the sign or 2's complement when using signed integers
 
Top