ATMEGA328P register regarding

Thread Starter

selvamurugan_t

Joined Aug 8, 2016
18
Hi


I am new to ATMGEA microcontroller .I have started to check the LED program.It working

Line 1: DDRB |= 1 << PINB0;
Line 2: _delay_ms(100);
Line 3: PORTB &= ~(1 << PINB0);
Line4: _delay_ms(100);


In Line1: what i understand means

PINB0= 0000 0001
After 1<<PINB0

PINB0=0000 0010;


DDRB |=0000 0010;
my question is sum+=rem means sum=0+rem value;

Here what will be DDRB value and OR function with 0000 0010;
 

MrChips

Joined Oct 2, 2009
30,795
PINB0 = 0

1 << PINB0 results in 1

This is always inefficient code if the compiler does no optimization.
I prefer to use a bit mask
PINB0_MASK = 0x01
DDRB |= PINB0_MASK

Better still, do it in a header file
#define PINB0_MASK 0x01

sum += rem is the same as sum = sum + rem

DDRB |= 0x01 is the same as DDRB = DDRB | 0x01

The purpose of this code is to set bit-0 of DDRB while leaving all other bits unchanged.
 

Thread Starter

selvamurugan_t

Joined Aug 8, 2016
18
PINB0 = 0

1 << PINB0 results in 1

This is always inefficient code if the compiler does no optimization.
I prefer to use a bit mask
PINB0_MASK = 0x01
DDRB |= PINB0_MASK

Better still, do it in a header file
#define PINB0_MASK 0x01

sum += rem is the same as sum = sum + rem

DDRB |= 0x01 is the same as DDRB = DDRB | 0x01

The purpose of this code is to set bit-0 of DDRB while leaving all other bits unchanged.


SO you are saying DDRB=0b0000 0000 | 0b0000 0001
and result is =0x0000 0001
I am correct.
Reset of the registers all the bits initialized with 0000 0000 correct or not
 

MrChips

Joined Oct 2, 2009
30,795
SO you are saying DDRB=0b0000 0000 | 0b0000 0001
and result is =0x0000 0001
I am correct.
That is correct.

But
DDRB |= 0b0000 00001
is not
DDRB = 0b0000 0000 | 0b0000 0001

Reset of the registers all the bits initialized with 0000 0000 correct or not
That is not correct.

Line 1: DDRB |= 1 << PINB0;

This instruction sets bit-0 of DDRB to 1.
 
Top