<< operator ?

Discussion in 'Programmer's Corner' started by rougie, Aug 1, 2010.

  1. rougie

    Thread Starter Active Member

    Dec 11, 2006
    410
    2
    Hello,

    The question involves a little knowledge about MCU's more particularly ....AVR's. Nonetheless, this question is a C question.

    In an LCD spec which shows code to test an LCD, shows the following lines of code:

    Code ( (Unknown Language)):
    1.  
    2. #define LCD_RS   PC7
    3. #define CLR_RS PORTC &= ~(1<<LCD_RS)
    4.  
    I am a PIC MCU guy, so I never used AVRs... so wouldn't know what "PC7" really means, but I guess is, it stands for an I/O output for bit 7 of port C of an AVR MCU!

    So my question is, what does (1<<LCD_RS) actually do?
    I know that the << operator is the left shift operator which has the following rules:

    value<<number of bits

    So a certain number of bits in "value" are shifted left... right?

    so in my example above, the contents of "value" which is 1 is shifetd left by the value in LCD_RS.... and so what is the value of LCD_RS if its an input????

    Confused!
    All help very appreciated!
    Rob
     
    Last edited: Aug 1, 2010
  2. tom66

    Senior Member

    May 9, 2009
    2,613
    214
    To clear a bit in a byte, you can AND it with a mask.

    So to clear bit #3 on a little endian processor: 11111011. Say the current port output is 10101111, and you used this mask to AND it, you would have 10101011... see how the bit has been cleared, but all the other bits are left alone.

    There are other ways to do this, but on a MCU speed is critical. The compiler, avr-gcc in this case, will optimise the statement to something like a single cycle bit clear operation.

    To create the mask you shift the constant '1' by the pin number, you then invert it, this is what the left shift operator is for.
     
  3. rougie

    Thread Starter Active Member

    Dec 11, 2006
    410
    2
    Hello Tom,

    Forget about this for a second:
    >you then invert it, this is what the left shift operator is for.

    Lets focus on this:
    >To create the mask you shift the constant '1' by the pin number

    (1<<LCD_RS)

    In the statement above, which is the pin number?
    Also, what value is LCD_RS.... its an output?????

    I understand if you tell me:

    int u=1;
    u = (u<<1)

    which will make u equal to 2. But this:

    (1<<LCD_RS)

    What is LCD_RS ???? Or where does the *value* of LCD_RS come from????
    Isn't LCD_RS an ouput that we want to set or clear based on the result of:

    #define CLR_RS PORTC &= ~(1<<LCD_RS)

    Confused!
    Rob
     
  4. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    It is a standard method of bit twiddling in C. Take a look here. http://en.wikipedia.org/wiki/Bit_manipulation. Below is some standard macros that will work in most C-compilers.
    #define bitset(var, bitno) ((var) |= 1UL << (bitno))
    #define bitclr(var, bitno) ((var) &= ~(1UL << (bitno)))
    #define toggle_bit(var,bitno) (var ^= (1UL << bitno))
    #define test_bit(var,bitno) (var & (1UL << bitno))
    If you are used to HI-Tech C you probably have written in your code
    Code ( (Unknown Language)):
    1.  
    2. RC7=0; clear bit 7 port C
    3.  
    This because the RC7 is defined in the header file, as bit 7 port C. To make bit twiddling for static variables more easy. But this is not implemented in all compilers.
     
  5. tom66

    Senior Member

    May 9, 2009
    2,613
    214
    LCD_RS will be defined in a header file somewhere. It corresponds to the pin number you have connected to the Register Select pin on your LCD, if it's a HD44780 compatible display.
     
  6. rougie

    Thread Starter Active Member

    Dec 11, 2006
    410
    2
    Okay, I see !

    So it acts something like this:

    //#define LCD_RS PC7
    #define CLR_RS PORTC &= ~(1<<7) // LCD_RS

    Now it makes sence! I just didn't know what PC7 stood for!

    Thanks
    Rob
     
Loading...