1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| // add this defines
#define SETBIT(ADDRESS,BIT) (ADDRESS |= (1<<BIT))
#define CLEARBIT(ADDRESS,BIT) (ADDRESS &= ~(1<<BIT))
#define FLIPBIT(ADDRESS,BIT) (ADDRESS ^= (1<<BIT))
#define CHECKBIT(ADDRESS,BIT) (ADDRESS & (1<<BIT))
#define WRITEBIT(RADDRESS,RBIT,WADDRESS,WBIT) (CHECKBIT(RADDRESS,RBIT) ? SETBIT(WADDRESS,WBIT) : CLEARBIT(WADDRESS,WBIT))
// and then you can use
SETBIT(PORTX,2); // set bit2 of portx (set to 1)
SETBIT(PORTY,0); // set bit0 of porty
CLEARBIT(PORTX,5); // clear bit5 of portx (set to 0)
FLIPBIT(PORTX,2); // invert bit2 of portX, if it was 1 it becomes 0, and if 0 becomes 1
// example for portx 0x10101010 the result is 0x10101110
if (CHECKBIT(PORTX,4)) {} else {} // result is true if bit4 of portx is 1, false if it is 0
if (!CHECKBIT(PORTX,4)) {} else {} // result is true if bit4 of portx is 0, false if it is 1
WRITEBIT(PORTX,0,PORTY,2); // copy the bit0 of portx to bit2 of porty |