123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- union _word
- {
- uint16_t word;
- uint8_t byte[2];
- } wrd;
- wrd.byte[0] = data;
- wrd.byte[1] = reg;
- -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
- http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=67601
- typedef union {
- volatile unsigned char flinps;
- struct {
- unsigned char b0 : 1;
- unsigned char b1 : 1;
- unsigned char menubtileft : 1;
- unsigned char menubtimiddle : 1;
- unsigned char menubtiright : 1;
- unsigned char b5 : 1;
- unsigned char b6 : 1;
- unsigned char b7 : 1;
- };
- } flinps_union_t;
-
- typedef union {
- volatile unsigned char buttons;
- struct {
- unsigned char b0 : 1;
- unsigned char b1 : 1;
- unsigned char menubtbleft : 1;
- unsigned char menubtbmiddle : 1;
- unsigned char menubtbright : 1;
- unsigned char b5 : 1;
- unsigned char b6 : 1;
- unsigned char b7 : 1;
- };
- } buttons_union_t;
- typdef struct {
- flinps_union_t flinps;
- buttons_union_t buttons;
- unsigned char buttprsd;
- unsigned char fdsmsm;
- unsigned char tmpinps;
- } mydata_struct_t;
- mydata_struct_t mydata;
- int main(void) {
- mydata.flinps.menubtileft = 1;
- mydata.buttons.menubtbright = 0;
- mydata.buttprsd = 11;
- mydata.fdsmsm = 22;
- mydata.tmpinps = 33;
- }
- -------------------------------------------------------------------------------
- Как брать отдельные байты от слова?
- http://www.avrfreaks.net/index.php?name=PNphpBB2&file=printview&t=20709&start=0
- A UNION is the most elegant solution and designed to perform just this task.
- Code:
- union u_type //Setup a Union
- {
- unsigned int IntVar;
- unsigned char Bytes[2];
- }
- temp; //Assign a var name to the Union
- void main(void)
- {
- temp.IntVar=65535; //Assign a value to the Int var of the Union
- HighByte=temp.Bytes[1]; //Get the High Byte (255)
- LowByte=temp.Bytes[0]; //Get the Low Byte (255)
- }
- ----------
- into a series of char (8-bit) pointers. Example:
- Code:
- unsigned char low_byte;
- unsigned char high_byte;
- unsigned int the_value;
- ...
- the_value = 1234;
- low_byte = * ((unsigned char *)&the_value);
- high_byte = * ((unsigned char *)((&the_value)+1));
- or a char-sized pointer could be used the same way. [I'd probably use the pointer method for repetitive uses within an app and/or many-byte variables.]
- Code:
- unsigned char low_byte;
- unsigned char high_byte;
- unsigned int the_value;
- unsigned char *the_ptr;
- ...
- the_value = 1234;
- the_ptr = * ((unsigned char *)&the_value);
- low_byte = *the_ptr;
- the_ptr++;
- high_byte = *the_ptr;
- ----------
- the bset way:
- it only compile in ONe line asm code !
- Code:
- #define LowB(x) (*((unsigned char*) &(##x)+1))
- #define HighB(x) (*((unsigned char*) &(##x)+0))
- for ex for low byte use LowB(x)
- только тут наверное младший байт без смещение (лежит первым), а старший
- байт -- +1. И тогда это совпадёт с предыдущим примером.
- ----------
- typedef union {
- uint16_t u16; // element specifier for accessing whole u16
- int16_t i16; // element specifier for accessing whole i16
- struct {
- #ifdef LITTLE_ENDIAN // Byte-order is little endian
- uint8_t u8L; // element specifier for accessing low u8
- uint8_t u8H; // element specifier for accessing high u8
- #else // Byte-order is big endian
- uint8_t u8H; // element specifier for accessing low u8
- uint8_t u8L; // element specifier for accessing high u8
- #endif
- } s16; // element spec. for acc. struct with low or high u8
- } nt16_t;
- nt16_t a;
- a.u16 = 0x0123;
- a.s16.u8H == 0x01;
- typedef union {
- uint32_t u32; // element specifier for accessing whole u32
- int32_t i32; // element specifier for accessing whole i32
- struct {
- #ifdef LITTLE_ENDIAN // Byte-order is little endian
- uint16_t u16L; // element specifier for accessing low u16
- uint16_t u16H; // element specifier for accessing high u16
- #else // Byte-order is big endian
- uint16_t u16H; // element specifier for accessing low u16
- uint16_t u16L; // element specifier for accessing high u16
- #endif
- } s32; // element spec. for acc. struct with low or high u16
- } nt32_t;
|