What is the preferred method for allowing byte (or even nibble) access
to unsigned long variables?
example:
a setup program that is taking a number off a screen (in BCD) on a per
character basis (say with a button that steps to each character) and
places the result in an unsigned long int (preferably still in BCD).
I thought of three methods:
1. Use a union.
2. Use a character array and then use shift assignments to initialize
the proper variables before and after
3. Try to access the RAM directly (access the unsigned long as if it
were a character array).
implementation:
1. should be obvious
2.
unsigned char testlong[4];
// initialize from RAM
testlong[0] = ramval & 0x000F;
testlong[1] = ( ramval & 0x00F0 ) >> 4;
testlong[2] = ( ramval & 0x0F00 ) >> 8;
testlong[3] = ( ramval & 0xF000 ) >> 12;
// Mess with testlong using loops, etc
// Copy value back to RAM
correctraw = ( testlong[3] << 12 ) + ( testlong[2] << 8 ) + (
testlong[1] << 4 ) + testlong[0];
3. Access the unsigned int as a string. The compiler complains about
this. But one way around the complaints is to use a pointer.
unsigned long int *longpointer;
longpointer = *ramval;
longpointer[0] = (uchar)something;
etc.
The reason for wanting to access the variable using byte operations is
that it makes it easy to "step across" ram val with an index. The only
alternative I can think of is to hard code each "byte" update of the
ramval (lots of code).
Thanks for any ideas.
One last thing: is there a way to force the compiler to use a RAM
location for a variable as opposed to the Stack (I know, same thing), or
a register?
-Mark