whats (UBRRH=BAUD_PRESCALE>>8)?

Discussion in 'Embedded Systems and Microcontrollers' started by khan yousufzai, Oct 16, 2012.

  1. khan yousufzai

    Thread Starter New Member

    Aug 31, 2012
    19
    0
    in the below program BAUD_PRESCALE =((crystal freq/(baudrate*16)-1)=77
    now i should do,UBRRL=BAUD_PRESCALE,BUT IN THE FOLLOWING FUNCTION ONE THING MAKES ME CONFUSED,UBRRH=(BAUD_PRESCALE>>8);WHATS THIS? PLEASE EXPLAIN,,,,THANKS SO MUCH,THE REAL HUMINITY

    void usart_init()
    {

    UCSRB |= (1<<RXCIE) | (1 << RXEN) | (1 << TXEN); // Turn on the transmission and reception circuitry
    UCSRC |= (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1); // Use 8-bit character sizes

    UBRRL = BAUD_PRESCALE; // Load lower 8-bits of the baud rate value into the low byte of the UBRR register
    UBRRH = (BAUD_PRESCALE >> 8); // Load upper 8-bits of the baud rate value into the high byte of the UBRR register
    }
     
  2. Papabravo

    Expert

    Feb 24, 2006
    10,138
    1,789
    >> is the shift right operator
    << is the shift left operator

    The left operand is shifted left(right) by a number of bits equal to the right hand operand. It is a common way to refer to the upper byte of a 16-bit word.
     
  3. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    If you have a BYTE PTR (or a char pointer), instead of shifting 8 times,
    you could use *(byte_ptr+1)

    If the upper byte is zero in your program, you can simply blank the destination. The lines maybe come from a generic template, where in some cases, the upper byte might be non-zero.
     
  4. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    As has been noted the >> is the C shift operator. { something >> 8 } is one way to rotate the high byte of a 16 bit quantity to the lower byte.

    If you would start with:

    BAUD_PRESCALE = 0b1010101001010101;

    then UBRRH would equal 0b10101010, or the high byte portion.
     
  5. Papabravo

    Expert

    Feb 24, 2006
    10,138
    1,789
    And most compilers are smart enough to avoid shifting if at all possible. Some might argue that the shift operation has more clarity than the pointer operation.
     
Loading...