There is an SRC variable of 16 bits, how to create two 8-bit variables, so that one is addressed to the upper part and the second to the lower part of the SRC variable? type construction
uint16_t src; uint8_t b1 = * ((uint8_t *) & src); uint8_t b2 = * (((uint8_t *) & src) +1);
should work but at compilation produces:
MAIN.c (16): error: # 28: expression must have a constant value uint8_t b1 = * ((uint8_t *) & src); MAIN.c (17): error: # 28: expression must have a constant value uint8_t b2 = * (((uint8_t *) & src) +1); MAIN.c: 0 warnings, 2 erros
How to correctly address the variables B1 and B2 that, when changing these variables, the corresponding value in the SRC variable and vice versa change. Sorry for my english :)
Maybe I'm not so explain my desire ... Parsing a 16-bit number occurs in an interrupt and EXTREME tacts are completely unnecessary ... The proposed version fits perfectly into the work of the main program conveniently and quickly enough, but for interrupts, these are extra delays within the interrupt.
uint16_t src; uint8_t b1, b2; //? how to initialize correctly? int main (void) { b1 = 0x5; // performs a simultaneous change in the variable in the high-order byte of the SRC = 0x0500 b2 = 0xa; // executes a simultaneous change in the variable in the lower part of the byte SRC = 0x050A src = 0xadd; // sets immediately and B1 = AA and B2 = DD }
why not a union?
HAS ANYBODY SAID USE A UNION?
USE A UNION.