16 bit variable conversation to 8 bit variables

Thread Starter

ecka333

Joined Oct 1, 2009
76
I am programming PIC16F876A microcontroller (8 bit). Compiler Mikroc from Mikroelektronika. I have unsigned int variable Clock, which is 16 bit long and i need to write it to eeprom. As i understand, i need to convert this variable to two 8 bit variables, then write them to eeprom.
I am thinking to convert it in following way:
ClockHigh=Clock/255
ClockLow=Clock%255
and then store ClockHigh and ClockLow to eeprom .
When i need to retrieve variable Clock from eeprom, i am planning to use:
Clock=ClockHigh*255+ClockLow
Also i heard, that there is way to do all these conversations with shift operators >> and <<. I need most efficient way.
 

t06afre

Joined May 11, 2009
5,934
I am programming PIC16F876A microcontroller (8 bit). Compiler Mikroc from Mikroelektronika. I have unsigned int variable Clock, which is 16 bit long and i need to write it to eeprom. As i understand, i need to convert this variable to two 8 bit variables, then write them to eeprom.
I am thinking to convert it in following way:
ClockHigh=Clock/255
ClockLow=Clock%255
and then store ClockHigh and ClockLow to eeprom .
When i need to retrieve variable Clock from eeprom, i am planning to use:
Clock=ClockHigh*255+ClockLow
Also i heard, that there is way to do all these conversations with shift operators >> and <<. I need most efficient way.
I think a union would be best for you in this case. Be very careful with division and multiplications then working with C and MCUs. However compilers are quite smart so it could be that your compiler understand what really want to do.
Rich (BB code):
typedef union 
   { 
   unsigned short counter;
   signed char hilo_bytes[2]; 
   } clock_register;

clock_register clock;
// play with variable clock 
clock.counter = 0xffff;
 ADDH = hilo_bytes[0]; 
 ADDL = hilo_bytes[1];
 

thatoneguy

Joined Feb 19, 2009
6,359
Hmm.. Can you explain your code a little?
Roughly, A union is a way to create a new datatype, such as int, string, etc, but of the design you need. Compilers are generally very good at figuring out "what you meant" when using them, while attempting the same in assembly is a bit of a pain.
 

Markd77

Joined Sep 7, 2009
2,806
Actually it's a lot easier in assembler because clock is already two 8 bit files. It's manipulating 16 bit numbers which is harder........ but I digress.
 

spinnaker

Joined Oct 29, 2009
7,830
The / operator can be inefficient.

You can do this very easily with the & and the >> operator. This would break up a 16 bit variable into two 8 bits variables.
Rich (BB code):
  unsigned char loByte;
  unsigned char hiByte;
  unsigned int i = 0xff02;

  loByte = i & 0x00ff;
  hiByte = i >> 8;
 

t06afre

Joined May 11, 2009
5,934
It is here the union comes handy as the members that compose a union all share the same storage area within the same memory. Take a look at my code. Some notes my compiler use Little-endian then formatting numbers. I do not know the endianess for your compiler. If it use Big-endian the hi and low byte will be swapped.
Rich (BB code):
typedef union 
{ 
unsigned short counter;
signed char hilo_bytes[2]; 
} clock_register;
clock_register clock;
// play with variable clock 
void main()
{clock.counter = 0xffff;
TMR1L = clock.hilo_bytes[0]; 
TMR1H = clock.hilo_bytes[1];
//some time alter
clock.hilo_bytes[0]=TMR1L; 
clock.hilo_bytes[1]=TMR1H;
//Now the variable clock.counter will contain the TMR1 value read above 
}
 

MMcLaren

Joined Feb 14, 2010
861
My BoostC compiler automatically generates the correct single word instructions when I use the following macros;

Rich (BB code):
#define lo(x) (char)((x)&0xFF)
#define hi(x) (char)((x)/256)
Happy Holidays.

Cheerful regards, Mike
 
Top