Port initialization with ATMEGA16
Posted: Tue Jun 29, 2010 12:06 pm
Hello,
I have some problems with port initialization on ATMEGA16: I have some examples and I seek also in the datasheet and I didn't understand what means to initialize: PORTA (or PORTB, PORTC, PORTD, doesn't matter) or DDRA (or B, C, D) with a hexadecimal value -- I know that every bit means something but I don't understand this: if bit is high "1", what really means, neither for bit low "0" for PORT and DDR.
I have also seen an example with _BV(something), I don't understand basically what function is doing.
//bit 0 of Port B is output, rest are automatically inputs
DDRB = _BV(0);
//bit 0 of Port B is input, rest are automatically outputs
DDRB = ~_BV(0);
//bit 0 and bit 1 of Port B are outputs, rest are automatically inputs
DDRB = _BV(0) | _BV(1);
//bit 0 and bit 1 of Port B are inputs, rest are automatically outputs
DDRB = ~( _BV(0) | _BV(1) );
I guess _BV(0) sets output the bit zero of a port and ~_BV(0) sets input.
_BV(X) ; X=0 -> 7; what is a doubt for me is _BV(X)=pow(2,X) or _BV(X)=~pow(2,X) (bit X is set or is cleared? ).
I want also SBI to be explained SBI and CBI and how can I do the same thing without using these functions.
Thanks in advance.
Thanks in advance.
I have some problems with port initialization on ATMEGA16: I have some examples and I seek also in the datasheet and I didn't understand what means to initialize: PORTA (or PORTB, PORTC, PORTD, doesn't matter) or DDRA (or B, C, D) with a hexadecimal value -- I know that every bit means something but I don't understand this: if bit is high "1", what really means, neither for bit low "0" for PORT and DDR.
I have also seen an example with _BV(something), I don't understand basically what function is doing.
//bit 0 of Port B is output, rest are automatically inputs
DDRB = _BV(0);
//bit 0 of Port B is input, rest are automatically outputs
DDRB = ~_BV(0);
//bit 0 and bit 1 of Port B are outputs, rest are automatically inputs
DDRB = _BV(0) | _BV(1);
//bit 0 and bit 1 of Port B are inputs, rest are automatically outputs
DDRB = ~( _BV(0) | _BV(1) );
I guess _BV(0) sets output the bit zero of a port and ~_BV(0) sets input.
_BV(X) ; X=0 -> 7; what is a doubt for me is _BV(X)=pow(2,X) or _BV(X)=~pow(2,X) (bit X is set or is cleared? ).
I want also SBI to be explained SBI and CBI and how can I do the same thing without using these functions.
Thanks in advance.
Thanks in advance.