Deklaracja bitow w CodeVisionAVR

Loading thread data ...

Deklaracja którą masz poniżej powinna działać pod każdym komplikatorem C. Musisz sobie tylko wybrać odpowiednią kolejność bitów w bajcie.

Regards, /J.D.

#include <stdio.h>

union moja_swietna_unia { unsigned char BYTE; struct { #if defined MSB_FIRST unsigned char B7:1; unsigned char B6:1; unsigned char B5:1; unsigned char B4:1; unsigned char B3:1; unsigned char B2:1; unsigned char B1:1; unsigned char B0:1; #elif defined LSB_FIRST unsigned char B0:1; unsigned char B1:1; unsigned char B2:1; unsigned char B3:1; unsigned char B4:1; unsigned char B5:1; unsigned char B6:1; unsigned char B7:1; #else #error "Musisz zdefiniować kolejność bitów w bajcie." #endif } BIT; };

int main(int argc, char **argv) { union moja_swietna_unia moja_swietna_zmienna;

moja_swietna_zmienna.BYTE = 0x99; printf("%#x\n", moja_swietna_zmienna.BYTE); /* 0x99 */

moja_swietna_zmienna.BIT.B7 ^= 1; /* przełącz bit 7 */ moja_swietna_zmienna.BIT.B6 = 1; /* ustaw bit 6 */ moja_swietna_zmienna.BIT.B3 = 0; /* wyzeruj bit 3 */ printf("%#x\n", moja_swietna_zmienna.BYTE); /* 0x51 */

return 0; }

Reply to
Jan Dubiec

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.