Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

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?

akg said:
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 ??

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?
 
Status
Not open for further replies.

Latest threads

Back
Top