Hola a todos.
Basándome en el criterio que me habéis dado, he dado con un ejemplo de Mikroelectronika referente a esto mismo en concreto.
Create the Union template somwhere before "void main ()":
union U16_
{
unsigned int word; // For accessing the whole 16-bit unsigned int
unsigned char byte[2]; // For accessing 16-bits as individual bytes
};
y luego...
Next create the instance where you need to use the Union in a function and load it with data:
void WriteMyEE_Bytes (unsigned int MyInWord)
{
union U16_ Int2Bytes;
...
Int2Bytes.word = MyInWord;
EEPROM_WR (Int2Bytes.byte[1]); // write the hi byte
EEPROM_WR (Int2Bytes.byte[0]); // write the lo byte to eeprom
...
}
Como no es mi caso, hay esto a continuación.
This same kind of union can also work the other way around when you want to combine 2-bytes into 1 word (16-bits). This technique is handy for reading 16-bit timer registers or ADC registers... ect... basically anytime two bytes are read out and are needed as a combined word.
unsigned int MakeMyWord (unsigned char HiInbyte, unsigned char LoInbyte)
{
union U16_ Bytes2Int;
...
...
Bytes2Int.byte[1] = HiInbyte; // combine bytes
Bytes2Int.byte[0] = LoInbyte; //
...
return (Bytes2Int.word); // return the combined bytes as a 16-bit word
}
Lo adapto a mis necesidades como sigue....
union U16_
{
unsigned int word; // For accessing the whole 16-bit unsigned int
unsigned char byte[2]; // For accessing 16-bits as individual bytes
};
unsigned int MakeMyWord (unsigned char HiInbyte, unsigned char LoInbyte)
{
union U16_ Bytes2Int;
Bytes2Int.byte[1] = HiInbyte; // combine bytes
Bytes2Int.byte[0] = LoInbyte; //
return (Bytes2Int.word); // return the combined bytes as a 16-bit word
}
Si esta última parte la pongo dentro de una función, me da un error diciendo que "return" no puede estar dentro de la función y si la dejo fuera los errores son de redefinición, el punto de separación Bytes2Int.byte no lo acepta, y al final, sabiendo que es una buena solución, no se como aplicarlo.
¿Alguien me da una solución?
Gracias.
P.D.
La página del ejemplo está en esta dirección
http://www.mikroe.com/forum/viewtopic.php?f=88&t=25930