Thread: Hi all i have a question about a code

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    2

    Hi all i have a question about a code

    This is part of the code, it write to LCD.
    Hitachi HD44780. It uses it in 4 bit mode

    PORTB bits 0-3 are connected to the LCD data bits 4-7 (high nibble)

    Code:
    lcd_putch(char c)
    {
    	LCD_RS = 1;	// write characters
    	PORTB = (PORTB & 0xF0) |  (c >> 4);
    	LCD_STROBE;
    	PORTB = (PORTB & 0xF0) |  (c & 0x0F);
    	LCD_STROBE;
    	DelayUs(40);
    }

    I cannot understand the next rows:

    PORTB = (PORTB & 0xF0) | (c >> 4);
    PORTB = (PORTB & 0xF0) | (c & 0x0F);

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    What about them can't you understand?

    > PORTB & 0xF0
    Preserves the top 4 bits, and clears the bottom 4 bits.

    The two operations on c get the top 4 bits and bottom 4 bits into position read to add (using |) to the existing portb value.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    2

    Thanks

    What role is this expression do: (c >> 4)

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Easily answered by looking in a book.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743
    It shifts the bits in c to the right by 4 places.

    So if c looked like:

    10100000

    It would now look like:

    00001010
    My Website

    "Circular logic is good because it is."

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by DavidP View Post
    It shifts the bits in c to the right by 4 places.

    So if c looked like:

    10100000

    It would now look like:

    00001010
    and c better be unsigned char for this
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need help to compile this code...
    By wise_ron in forum C Programming
    Replies: 17
    Last Post: 05-07-2006, 12:22 PM
  2. My First If Code, Question? :P
    By dimirpaw in forum C++ Programming
    Replies: 3
    Last Post: 11-29-2005, 08:49 PM
  3. End of Code Loop Question
    By JuanSverige in forum C++ Programming
    Replies: 1
    Last Post: 04-08-2003, 10:35 AM
  4. EXE to code? Decompile question.
    By RoD in forum Game Programming
    Replies: 4
    Last Post: 10-01-2002, 05:28 PM
  5. question about my code
    By killerasp in forum C++ Programming
    Replies: 7
    Last Post: 02-18-2002, 08:05 PM