C_unions_bits.txt 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=67601
  2. typedef union {
  3. volatile unsigned char flinps;
  4. struct {
  5. unsigned char b0 : 1;
  6. unsigned char b1 : 1;
  7. unsigned char menubtileft : 1;
  8. unsigned char menubtimiddle : 1;
  9. unsigned char menubtiright : 1;
  10. unsigned char b5 : 1;
  11. unsigned char b6 : 1;
  12. unsigned char b7 : 1;
  13. };
  14. } flinps_union_t;
  15. typedef union {
  16. volatile unsigned char buttons;
  17. struct {
  18. unsigned char b0 : 1;
  19. unsigned char b1 : 1;
  20. unsigned char menubtbleft : 1;
  21. unsigned char menubtbmiddle : 1;
  22. unsigned char menubtbright : 1;
  23. unsigned char b5 : 1;
  24. unsigned char b6 : 1;
  25. unsigned char b7 : 1;
  26. };
  27. } buttons_union_t;
  28. typdef struct {
  29. flinps_union_t flinps;
  30. buttons_union_t buttons;
  31. unsigned char buttprsd;
  32. unsigned char fdsmsm;
  33. unsigned char tmpinps;
  34. } mydata_struct_t;
  35. mydata_struct_t mydata;
  36. int main(void) {
  37. mydata.flinps.menubtileft = 1;
  38. mydata.buttons.menubtbright = 0;
  39. mydata.buttprsd = 11;
  40. mydata.fdsmsm = 22;
  41. mydata.tmpinps = 33;
  42. }
  43. -------------------------------------------------------------------------------
  44. Как брать отдельные байты от слова?
  45. http://www.avrfreaks.net/index.php?name=PNphpBB2&file=printview&t=20709&start=0
  46. A UNION is the most elegant solution and designed to perform just this task.
  47. Code:
  48. union u_type //Setup a Union
  49. {
  50. unsigned int IntVar;
  51. unsigned char Bytes[2];
  52. }
  53. temp; //Assign a var name to the Union
  54. void main(void)
  55. {
  56. temp.IntVar=65535; //Assign a value to the Int var of the Union
  57. HighByte=temp.Bytes[1]; //Get the High Byte (255)
  58. LowByte=temp.Bytes[0]; //Get the Low Byte (255)
  59. }
  60. ----------
  61. into a series of char (8-bit) pointers. Example:
  62. Code:
  63. unsigned char low_byte;
  64. unsigned char high_byte;
  65. unsigned int the_value;
  66. ...
  67. the_value = 1234;
  68. low_byte = * ((unsigned char *)&the_value);
  69. high_byte = * ((unsigned char *)((&the_value)+1));
  70. 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.]
  71. Code:
  72. unsigned char low_byte;
  73. unsigned char high_byte;
  74. unsigned int the_value;
  75. unsigned char *the_ptr;
  76. ...
  77. the_value = 1234;
  78. the_ptr = * ((unsigned char *)&the_value);
  79. low_byte = *the_ptr;
  80. the_ptr++;
  81. high_byte = *the_ptr;
  82. ----------
  83. the bset way:
  84. it only compile in ONe line asm code !
  85. Code:
  86. #define LowB(x) (*((unsigned char*) &(##x)+1))
  87. #define HighB(x) (*((unsigned char*) &(##x)+0))
  88. for ex for low byte use LowB(x)
  89. только тут наверное младший байт без смещение (лежит первым), а старший
  90. байт -- +1. И тогда это совпадёт с предыдущим примером.