Hi all i have a question about a code

This is a discussion on Hi all i have a question about a code within the C++ Programming forums, part of the General Programming Boards category; This is part of the code, it write to LCD. Hitachi HD44780. It uses it in 4 bit mode PORTB ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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, 07: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, 07:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21