ATMEGA328P register regarding

Discussion in 'Embedded Systems and Microcontrollers' started by selvamurugan_t, Aug 25, 2016.

  1. selvamurugan_t

    Thread Starter New Member

    Aug 8, 2016
    17
    0
    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;
     
  2. MrChips

    Moderator

    Oct 2, 2009
    12,449
    3,365
    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.
     
  3. selvamurugan_t

    Thread Starter New Member

    Aug 8, 2016
    17
    0


    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
     
  4. MrChips

    Moderator

    Oct 2, 2009
    12,449
    3,365
    That is correct.

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

    That is not correct.

    Line 1: DDRB |= 1 << PINB0;

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