RGB LED cube 5x5x5  1.0
Martin Stejskal, Schmidt Dominik
 All Files Functions Variables Macros Groups Pages
Macros
bit_operations.h File Reference

Macros and functions for bit operations in C. More...

#include <avr/io.h>

Go to the source code of this file.

Macros

#define set_bit_simple(var, bit_num)   var = (var) | (1<<bit_num)
 Set bit in variable. More...
 
#define set_bit(var, bit_num)   set_bit_simple(var, bit_num)
 Set bit in variable. More...
 
#define clr_bit_simple(var, bit_num)   var = (var) & (~(1<<bit_num))
 Clear bit in variable. More...
 
#define clr_bit(var, bit_num)   var = (var) & (~(1<<bit_num))
 Clear bit in variable. More...
 
#define io_set_dir_out_simple(X, pin)   DDR##X = DDR##X | (1<< pin)
 Set pin direction as output. More...
 
#define io_set_dir_out(X, pin)   io_set_dir_out_simple(X,pin)
 Set pin direction as output. More...
 
#define io_set_dir_in_simple(X, pin)
 Set pin direction as input. More...
 
#define io_set_dir_in(X, pin)   io_set_dir_in_simple(X,pin)
 Set pin direction as input. More...
 
#define io_set_1_simple(X, pin)   PORT##X = PORT##X | (1 << pin )
 Set pin level (in PORTx register) to '1'. More...
 
#define io_set_1(X, pin)   io_set_1_simple(X,pin)
 Set pin level (in PORTx register) to '1'. More...
 
#define io_set_H(X, pin)   io_set_1(X,pin)
 Set pin level (in PORTx register) to '1' (HIGH) More...
 
#define io_set_0_simple(X, pin)   PORT##X = PORT##X & (~(1 << pin))
 Set pin level (in PORTx register) to '0'. More...
 
#define io_set_0(X, pin)   io_set_0_simple(X,pin)
 Set pin level (in PORTx register) to '0'. More...
 
#define io_set_L(X, pin)   io_set_0(X,pin)
 Set pin level (in PORTx register) to '0' (LOW) More...
 
#define io_read_simple(X, pin)   ( PIN##X & (1 << pin) )>>pin
 Read pin level (in PINx register). More...
 
#define io_read(X, pin)   io_read_simple(X,pin)
 Read pin level (in PINx register). More...
 
#define io_read_fast_simple(X, pin)   PIN##X & (1 << pin)
 Read pin level (in PORTx register) a little bit faster, but... More...
 
#define io_read_fast(X, pin)   io_read_fast_simple(X,pin)
 Read pin level (in PORTx register) a little bit faster, but... More...
 
#define dir_out   1
 
#define dir_in   0
 

Detailed Description

Macros and functions for bit operations in C.

Author
Martin Stejskal

Some operations are not "easy to write" so there is "easy to use". If it is not necessary, then macros simple do not use

Definition in file bit_operations.h.

Macro Definition Documentation

#define clr_bit (   var,
  bit_num 
)    var = (var) & (~(1<<bit_num))

Clear bit in variable.

Clear defined bit in variable or register.
Example:
clr_bit_simple(DDRB,PB4); // Set PB4 as input
clr_bit_simple( data, 4); // Set 4th bit to '0' in variable "data"
Following code will work too:
#define LED_PORT DDRB
#define LED_pin 4
clr_bit(LED_port, LED_pin);

Parameters
varInput variable.
bit_numDefine witch bit will be rewritten. For 8 bit numbers use 0~7
Returns
Result is saved to input variable

Definition at line 101 of file bit_operations.h.

#define clr_bit_simple (   var,
  bit_num 
)    var = (var) & (~(1<<bit_num))

Clear bit in variable.

Clear defined bit in variable or register.
Example:
clr_bit_simple(DDRB,PB4); // Set PB4 as input
clr_bit_simple( data, 4); // Set 4th bit to '0' in variable "data"
Following code will NOT work!!!:
define LED_PORT DDRB
define LED_pin 4
clr_bit_simple(LED_port, LED_pin);

Parameters
varInput variable.
bit_numDefine witch bit will be rewritten. For 8 bit numbers use 0~7
Returns
Result is saved to input variable

Definition at line 81 of file bit_operations.h.

#define io_read (   X,
  pin 
)    io_read_simple(X,pin)

Read pin level (in PINx register).

Pin is defined by PORT and pin number. So macro take this two parameters
and create one line command. Advantage is, that only port post-fix is
given, so it can be easy combined with io_set_dir_in . Pin must be set
as input before! This macro just read PINx register and shift result!
Example:
io_read(D,6); // Produce code: ( ( PIND & (1<<6) )>>6 )
Following code will work too:
#define LED_PORT B
#define LED_pin 3
io_read(LED_PORT, LED_pin);

Parameters
XPort name. Example: D -> PIND
pinPin number. Example 6 -> PD6 (PD6 can be given too)
Returns
1 if on pin is logic 1 (HIGH), otherwise return 0

Definition at line 335 of file bit_operations.h.

#define io_read_fast (   X,
  pin 
)    io_read_fast_simple(X,pin)

Read pin level (in PORTx register) a little bit faster, but...

... beware of return value! Pin is defined by PORT and pin number. So
macro take this two parameters and create one line command. Advantage is,
that only port post-fix is given, so it can be easy combined with
io_set_dir_in . Pin must be set as input before! This macro just read
PINx register and NOT shift result!
Example:
io_read_fast(A,5); // Produce code: PINA & (1<<5);
Following code will work too:
#define LED_PORT B
#define LED_pin 3
io_read_fast(LED_PORT, LED_pin);

Parameters
XPort name. Example: A -> PORTA
pinPin number. Example 5 -> PA5 (PA5 can be given too)
Returns
0 if on pin is logic 0 (LOW), otherwise return NON ZERO
value

Definition at line 383 of file bit_operations.h.

#define io_read_fast_simple (   X,
  pin 
)    PIN##X & (1 << pin)

Read pin level (in PORTx register) a little bit faster, but...

... beware of return value! Pin is defined by PORT and pin number. So
macro take this two parameters and create one line command. Advantage is,
that only port post-fix is given, so it can be easy combined with
io_set_dir_in . Pin must be set as input before! This macro just read
PINx register and NOT shift result!
Example:
io_read_fast_simple(A,5); // Produce code: PINA & (1<<5);
Following code will NOT work!!!:
#define LED_PORT B
#define LED_pin 3
io_read_fast_simple(LED_PORT, LED_pin);

Parameters
XPort name. Example: A -> PORTA
pinPin number. Example 5 -> PA5 (PA5 can be given too)
Returns
0 if on pin is logic 0 (LOW), otherwise return NON ZERO
value

Definition at line 359 of file bit_operations.h.

#define io_read_simple (   X,
  pin 
)    ( PIN##X & (1 << pin) )>>pin

Read pin level (in PINx register).

Pin is defined by PORT and pin number. So macro take this two parameters
and create one line command. Advantage is, that only port post-fix is
given, so it can be easy combined with io_set_dir_in . Pin must be set
as input before! This macro just read PINx register and shift result!
Example:
io_read_simple(D,6); // Produce code: ( ( PIND & (1<<6) )>>6 )
Following code will NOT work!!!:
#define LED_PORT B
#define LED_pin 3
io_read_simple(LED_PORT, LED_pin);

Parameters
XPort name. Example: D -> PIND
pinPin number. Example 6 -> PD6 (PD6 can be given too)
Returns
1 if on pin is logic 1 (HIGH), otherwise return 0

Definition at line 314 of file bit_operations.h.

#define io_set_0 (   X,
  pin 
)    io_set_0_simple(X,pin)

Set pin level (in PORTx register) to '0'.

Pin is defined by PORT and pin number. So macro take this two parameters
and create one line command. Advantage is, that only port post-fix is
given, so it can be easy combined with io_set_dir_out . Pin must be set
as output before! This macro just set PORTx register!
Example:
io_set_0(D,2); // Produce code: PORTD = PORTD & (~ (1<<2) );
Following code will work too:
#define LED_PORT B
#define LED_pin 3
io_set_0(LED_PORT, LED_pin);

Parameters
XPort name. Example: D -> PORTD
pinPin number. Example 2 -> PD2 (PD2 can be given too)

Definition at line 277 of file bit_operations.h.

#define io_set_0_simple (   X,
  pin 
)    PORT##X = PORT##X & (~(1 << pin))

Set pin level (in PORTx register) to '0'.

Pin is defined by PORT and pin number. So macro take this two parameters
and create one line command. Advantage is, that only port post-fix is
given, so it can be easy combined with io_set_dir_out . Pin must be set
as output before! This macro just set PORTx register!
Example:
io_set_0_simple(D,2); // Produce code: PORTD = PORTD & (~ (1<<2) );
Following code will NOT work!!!:
#define LED_PORT B
#define LED_pin 3
io_set_0_simple(LED_PORT, LED_pin);

Parameters
XPort name. Example: D -> PORTD
pinPin number. Example 2 -> PD2 (PD2 can be given too)

Definition at line 257 of file bit_operations.h.

#define io_set_1 (   X,
  pin 
)    io_set_1_simple(X,pin)

Set pin level (in PORTx register) to '1'.

Pin is defined by PORT and pin number. So macro take this two parameters
and create one line command. Advantage is, that only port post-fix is
given, so it can be easy combined with io_set_dir_out . Pin must be set
as output before! This macro just set PORTx register!
Example:
io_set_1(A,7); // Produce code: PORTA = PORTA | (1<<7);
Following code will work too:
#define LED_PORT B
#define LED_pin 3
io_set_1(LED_PORT, LED_pin);

Parameters
XPort name. Example: A -> PORTA
pinPin number. Example 7 -> PA7 (PA7 can be given too)

Definition at line 220 of file bit_operations.h.

#define io_set_1_simple (   X,
  pin 
)    PORT##X = PORT##X | (1 << pin )

Set pin level (in PORTx register) to '1'.

Pin is defined by PORT and pin number. So macro take this two parameters
and create one line command. Advantage is, that only port post-fix is
given, so it can be easy combined with io_set_dir_out . Pin must be set
as output before! This macro just set PORTx register!
Example:
io_set_1_simple(A,7); // Produce code: PORTA = PORTA | (1<<7);
Following code will NOT work!!!:
#define LED_PORT B
#define LED_pin 3
io_set_1_simple(LED_PORT, LED_pin);

Parameters
XPort name. Example: A -> PORTA
pinPin number. Example 7 -> PA7 (PA7 can be given too)

Definition at line 200 of file bit_operations.h.

#define io_set_dir_in (   X,
  pin 
)    io_set_dir_in_simple(X,pin)

Set pin direction as input.

Pin is defined by PORT and pin number. So macro take this two parameters
and create command. Advantage is, that only port post-fix is given, so it
can be easy combined with io_read . Also clear PIN bit (for case, that
pin was set as output before).
Example:
io_set_dir_in(C,0); // Produce code:
// DDRC = DDRC & (~ (1<<0) ) ; PINC = PINC & (~ (1<<0) );
Following code will work too:
#define LED_PORT B
#define LED_pin 3
io_set_dir_in(LED_PORT, LED_pin);

Parameters
XPort name. Example: C -> DDRC + PINC
pinPin number. Example 0 -> PC0 (PC0 can be given too)

Definition at line 180 of file bit_operations.h.

#define io_set_dir_in_simple (   X,
  pin 
)
Value:
DDR##X = DDR##X & (~(1 << pin));\
PIN##X = PIN##X & (~(1 << pin))

Set pin direction as input.

Pin is defined by PORT and pin number. So macro take this two parameters
and create command. Advantage is, that only port post-fix is given, so it
can be easy combined with io_read . Also clear PIN bit (for case, that
pin was set as output before).
Example:
io_set_dir_in_simple(C,0); // Produce code:
// DDRC = DDRC & (~ (1<<0) ) ; PINC = PINC & (~ (1<<0) );
Following code will NOT work!!!:
#define LED_PORT B
#define LED_pin 3
io_set_dir_in_simple(LED_PORT, LED_pin);

Parameters
XPort name. Example: C -> DDRC + PINC
pinPin number. Example 0 -> PC0 (PC0 can be given too)

Definition at line 159 of file bit_operations.h.

#define io_set_dir_out (   X,
  pin 
)    io_set_dir_out_simple(X,pin)

Set pin direction as output.

Pin is defined by PORT and pin number. So macro take this two parameters
and create one line command. Advantage is, that only port post-fix is
given, so it can be easy combined with io_set_1
Example:
io_set_dir_out(B,3); // Produce code: DDRB = DDRB | (1<<3);
Following code will work too:
#define LED_PORT B
#define LED_pin 3
io_set_dir_out(LED_PORT, LED_pin);

Parameters
XPort name. Example: B -> DDRB
pinPin number. Example 3 -> PB3 (PB3 can be given too)

Definition at line 138 of file bit_operations.h.

#define io_set_dir_out_simple (   X,
  pin 
)    DDR##X = DDR##X | (1<< pin)

Set pin direction as output.

Pin is defined by PORT and pin number. So macro take this two parameters
and create one line command. Advantage is, that only port post-fix is
given, so it can be easy combined with io_set_1
Example:
io_set_dir_out_simple(B,3); // Produce code: DDRB = DDRB | (1<<3);
Following code will NOT work!!!:
#define LED_PORT B
#define LED_pin 3
io_set_dir_out_simple(LED_PORT, LED_pin);

Parameters
XPort name. Example: B -> DDRB
pinPin number. Example 3 -> PB3 (PB3 can be given too)

Definition at line 121 of file bit_operations.h.

#define io_set_H (   X,
  pin 
)    io_set_1(X,pin)

Set pin level (in PORTx register) to '1' (HIGH)

Same as macro io_set_1 .
Example:
io_set_1(A,7); // Produce code: PORTA = PORTA | (1<<7);
Following code will work too:
#define LED_PORT B
#define LED_pin 3
io_set_H(LED_PORT, LED_pin);

Parameters
XPort name. Example: A -> PORTA
pinPin number. Example 7 -> PA7 (PA7 can be given too)

Definition at line 237 of file bit_operations.h.

#define io_set_L (   X,
  pin 
)    io_set_0(X,pin)

Set pin level (in PORTx register) to '0' (LOW)

Same as macro io_set_0 .
Example:
io_set_0(D,2); // Produce code: PORTD = PORTD & (~ (1<<2) );
Following code will work too:
#define LED_PORT B
#define LED_pin 3
io_set_L(LED_PORT, LED_pin);

Parameters
XPort name. Example: D -> PORTD
pinPin number. Example 2 -> PD2 (PD2 can be given too)

Definition at line 293 of file bit_operations.h.

#define set_bit (   var,
  bit_num 
)    set_bit_simple(var, bit_num)

Set bit in variable.

Set defined bit in variable or register.
Example:
set_bit(DDRB,PB4); // Set PB4 as output
set_bit( data, 5); // Set 5th bit to '1' in variable "data"
Following code will work too:
#define LED_PORT DDRB
#define LED_pin 4
set_bit(LED_port, LED_pin);

Parameters
varInput variable.
bit_numDefine witch bit will be rewritten. For 8 bit numbers use 0~7
Returns
Result is saved to input variable

Definition at line 62 of file bit_operations.h.

#define set_bit_simple (   var,
  bit_num 
)    var = (var) | (1<<bit_num)

Set bit in variable.

Set defined bit in variable or register.
Example:
set_bit_simple(DDRB,PB4); // Set PB4 as output
set_bit_simple( data, 5); // Set 5th bit to '1' in variable "data"
Following code will NOT work!!!:
#define LED_PORT DDRB
#define LED_pin 4
set_bit_simple(LED_port, LED_pin);

Parameters
varInput variable.
bit_numDefine witch bit will be rewritten. For 8 bit numbers use 0~7
Returns
Result is saved to input variable

Definition at line 43 of file bit_operations.h.