uC techniques
This is useful for Bit mathematics cookbook.
var |= mast : (or) : sets a bit
var &= ~mask : (and not) : clears a bit
var ^= mask : (xor) : flip a bit
Code:
#ifndef _AVR035_H_
#define _AVR035_H_
// from AVR035: Efficient C Coding for AVR
#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 SETBITMASK(x,y) (x |= (y))
#define CLEARBITMASK(x,y) (x &= (~y))
#define FLIPBITMASK(x,y) (x ^= (y))
#define CHECKBITMASK(x,y) (x & (y))
#define VARFROMCOMB(x, y) x
#define BITFROMCOMB(x, y) y
#define C_SETBIT(comb) SETBIT(VARFROMCOMB(comb), BITFROMCOMB(comb))
#define C_CLEARBIT(comb) CLEARBIT(VARFROMCOMB(comb), BITFROMCOMB(comb))
#define C_FLIPBIT(comb) FLIPBIT(VARFROMCOMB(comb), BITFROMCOMB(comb))
#define C_CHECKBIT(comb) CHECKBIT(VARFROMCOMB(comb), BITFROMCOMB(comb))
#define _CONCAT(a,b) a##b
#define PORT(x) _CONCAT(PORT,x)
#define PIN(x) _CONCAT(PIN,x)
#define DDR(x) _CONCAT(DDR,x)
#endif
Use:
Code:
#define Status_LED PORTA, 3
C_SETBIT(Status_LED);
C_CLEARBIT(Status_LED);
Following table lists register bit settings and resulting function of port pins
register bits →
pin function↓ DDRx.n PORTx.n PINx.n
------------------------------------------------------------------------------------------------------
tri stated input 0 0 read data bit x = PINx.n;
pull-up input 0 1 read data bit x = PINx.n;
output 1 write data bit n/a
PORTx.n = x;