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 :)
You've not initialised Src to point to anything.
C and C++ have different rules
uint16_t src; uint8_t b1 = * ((uint8_t *) & src); // not legal in C (not const), yes in C++ uint8_t b2 = * (((uint8_t *) & src) +1); // not legal in C (not const), yes in C++ #define LOWBYTE(a) * ((uint8_t *) &a) // Assumes Little Endian #define HIGHBYTE(a) * (((uint8_t *) &a) +1) // Assumes Little Endian void main() { uint8_t b1 = * ((uint8_t *) & src); // legal in C and C++ uint8_t b2 = * (((uint8_t *) & src) +1); // legal in C and C++ b1 = LOWBYTE(src); // legal in both b2 = HIGHBYTE(src); // legal in both }
Thanks for the answers :) there are several BUT. The command "b1 = LOWBYTE (src); // legal in both" takes 4 cycles of the processor and that loss in the sum of 8 bars at the same time it's not exactly what you wanted. I want to declare SRC variable 16bit and two variables on 8bit LOWBYTE and HIGHBYTE but so that the change of one caused the change of the other and vice versa. In general, a single address space for these variables. In the bascomAVR it is implemented like this:
Dim Data_wr As Word Dim X_low As Byte At Data_wr Overlay 'LOWBYTE ... Dim X_high As Byte At Data_wr + 1 Overlay 'HIGHBYTE | SRC | adress|0 7|8 15| |HIGHBYTE| LOWBYTE |
You are describing exactly what a union in C would do.
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.
Hello, THANK YOU to everyone who helped solve my question :) After the last clue, I solved the task posed :) Here's the code I'm currently using:
union Register32 { struct { uint8_t byte1; uint8_t byte2; uint8_t byte3; uint8_t byte4; } bytes; struct { uint16_t low; uint16_t high; } words; uint32_t dword; }; typedef union Register32 EAX; int main (void) { EAX pw1; pw1.dword = 0xaaddC0FF; }
For all who are interested in - a single address space variable and access to bits and bytes. Full version:
union Register32 { struct { unsigned a0: 1; unsigned a1: 1; unsigned a2: 1; unsigned a3: 1; unsigned a4: 1; unsigned a5: 1; unsigned a6: 1; unsigned a7: 1; unsigned a8: 1; unsigned a9: 1; unsigned a10: 1; unsigned a11: 1; unsigned a12: 1; unsigned a13: 1; unsigned a14: 1; unsigned a15: 1; unsigned a16: 1; unsigned a17: 1; unsigned a18: 1; unsigned a19: 1; unsigned a20: 1; unsigned a21: 1; unsigned a22: 1; unsigned a23: 1; unsigned a24: 1; unsigned a25: 1; unsigned a26: 1; unsigned a27: 1; unsigned a28: 1; unsigned a29: 1; unsigned a30: 1; unsigned a31: 1; } bit; struct { uint8_t byte1; uint8_t byte2; uint8_t byte3; uint8_t byte4; } bytes; struct { uint16_t low; uint16_t high; } words; uint32_t dword; }; union Register16 { struct { unsigned a0: 1; unsigned a1: 1; unsigned a2: 1; unsigned a3: 1; unsigned a4: 1; unsigned a5: 1; unsigned a6: 1; unsigned a7: 1; unsigned a8: 1; unsigned a9: 1; unsigned a10: 1; unsigned a11: 1; unsigned a12: 1; unsigned a13: 1; unsigned a14: 1; unsigned a15: 1; } bit; struct { uint8_t low; uint8_t high; } bytes; uint16_t dint; }; union Register8 { struct { unsigned a0: 1; unsigned a1: 1; unsigned a2: 1; unsigned a3: 1; unsigned a4: 1; unsigned a5: 1; unsigned a6: 1; unsigned a7: 1; } bit; uint8_t bytes; }; typedef union Register32 EAX32; typedef union Register16 EAX16; typedef union Register8 EAX8;