explain it

Status
Not open for further replies.

kaveh

New Member
hi
in the avr data sheets we have the following expression:
The following code example shows how to set port B pins 0 and 1 high, 2 and 3 low, and
define the port pins from 4 to 7 as input with pull-ups assigned to port pins 6 and 7. The
resulting pin values are read back again, but as previously discussed, a nop instruction
is included to be able to read back the value recently assigned to some of the pins.
Assembly Code Example(1)
...
; Define pull-ups and set outputs high
; Define directions for port pins
ldi r16,(1<<PB7)|(1<<PB6)|(1<<PB1)|(1<<PB0)
ldi r17,(1<<DDB3)|(1<<DDB2)|(1<<DDB1)|(1<<DDB0)
out PORTB,r16
out DDRB,r17
; Insert nop for synchronization
nop
; Read port pins
in r16,PINB
...
C Code Example(1)
unsigned char i;
...
/* Define pull-ups and set outputs high */
/* Define directions for port pins */
PORTB = (1<<PB7)|(1<<PB6)|(1<<PB1)|(1<<PB0);
DDRB = (1<<DDB3)|(1<<DDB2)|(1<<DDB1)|(1<<DDB0);
/* Insert nop for synchronization*/
_NOP();
/* Read port pins */
i = PINB;
...
my problem is,how the fllowing expression do a set bit for us
ldi r17,(1<<DDB3)|(1<<DDB2)|(1<<DDB1)|(1<<DDB0)
PORTB = (1<<PB7)|(1<<PB6)|(1<<PB1)|(1<<PB0);

thanks alot
 
The expression in the brackets will be evaluated as

(1<<PB7) = 0b10000000;
(1<<PB6) = 0b01000000;
(1<<PB1) = 0b00000010;
(1<<PB0) = 0b00000001;

The whole expression is Bitwise OR of all of them
(1<<PB7)|(1<<PB6)|(1<<PB1)|(1<<PB0) = 0b1100011;

Thus

PORTB = (1<<PB7)|(1<<PB6)|(1<<PB1)|(1<<PB0);

Will set output pins PB7, PB6, PB1 and PB0 to logic high state.
 
where does this definitones have been made?

hi dipal_z
thanks for reply,could you please inform me that ,where does this definitions have been made.i mean
PB0=0, PB1=1, PB2=2, ...

thanks alot
 
Re: where does this definitones have been made?

kaveh said:
hi dipal_z
thanks for reply,could you please inform me that ,where does this definitions have been made.i mean
PB0=0, PB1=1, PB2=2, ...

thanks alot
Checked the avr include files ??
 
Re: where does this definitones have been made?


That’s correct, for C definitions check the include files and for assembly check for .inc file. Most compilers name these file based on the device it is representing e.g. in ICCAVR header file for ATMega128 would be iom128v.h, in WinAVR its iom128.h
 
I was just curious: is doing:

"(1<<PB7)|(1<<PB6)" the same as:

"(1<<PB7)+(1<<PB6)" in AVR Studio 4?
 
DigiTan said:
I was just curious: is doing:

"(1<<PB7)|(1<<PB6)" the same as:

"(1<<PB7)+(1<<PB6)" in AVR Studio 4?

the + will produce a carry but | will not (case when pb7 and pb6 are same)
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…