Hi, I want to create a bit field structure for a group of bits associated with 'sfr P1' /* Bit definitions within sfr P1 */ #define NRAMEN (1 << 5) #define A20 (1 << 4) #define A19 (1 << 3) #define A18 (1 << 2) #define A17 (1 << 1) #define A16 (1 << 0) #define NCSDUART NRAMEN+A18 struct myp1 { unsigned char bankSelect :6; /* select banked device */ unsigned char wdog :1; }; struct myp1 data port1 _at_ 0x90; /* problem line doesnt work */ My problem is the last line, how do i get my struct to use P1 so I can do the following ? port1.bankSelect = NCSDUART; /* set/res the appropriate bits on port1 */ Or will i have to start doing something like the following (which i was trying to avoid)? P1 = (P1 & 0xC0) + NCSDUART; /* mask off unwanted bits then select appropriate banked device */ Thanks Mark.
You can't do the struct version. SFR's are in the DATA space from 0x80 to 0xFF. The compiler will not generate the appropriate mov's for what want to do. For setting one bit use sbit:
sfr r_port1 = 0x90; sbit r_nRamEn = r_port1 ^ 5; /* Enable RAM */ r_nRamEn = 0; /* use the RAM */ /* Disable RAM */ r_nRamEn = 1;
#define SELECT_BANK(port, dev) do { port &= ~(dev); port |= (dev); } while (0) /* <-- No semicolon! */ /* Usage */ #define NRAMEN (1 << 5) #define A20 (1 << 4) #define A19 (1 << 3) #define A18 (1 << 2) #define A17 (1 << 1) #define A16 (1 << 0) #define NCSDUART (NRAMEN + A18) SELECT_BANK(r_port1, NSCDUART);
I guess my botched post (above) proves one shouldn't use CodeWright to edit one's replies. Sorry for the mess.