NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » lcd.c bitwise operations

January 10, 2011
by kemil
kemil's Avatar

Hi Guys,

Can I just check I am following through the binary operations of the lcd_home() function found in the lcd.c source code correctly.

void lcd_home() {    
lcd_set_type_command();    
lcd_write_byte(0x02);    
delay_ms(50);    
}

working through the above code:
 lcd_set_type_command()--> PORTD &= ~(1<<PD7); = 00000000 in binary

Then:
 lcd_write_byte(0x02);
which is equivalent to:

 lcd_write_nibble( (0x02 >> 4) & 0x0f );
so in binary this is

lcd_write_nibble( (0010 >> 4) & 1111 )---> 0000 in binary
so plugging 0000  (where c is) into lcd_write_nibble gives

PORTD &= ~(0x0f << 2); which is 0b11000011

PORTD |= (c&0x0f) << 2;  which is PORTD |= 0000|1111 << 2
                           hence 11000011 |= 00000000 = 11000011

 So taking 0b11000011 as PORTD the line below is 
 PORTD |= (1<<PD6); hence 11000011|= 01000000 = 11000011
 delay_us(1);

 PORTD &= ~(1<<PD6); hence 11000011 &= ~(01000000) = 10000011
 delay_us(1);

IS this first part correct? If not could you point out what I am doing wring and what is should be....

Thanks for your help

kemil

January 10, 2011
by bretm
bretm's Avatar

~(1<<PD7) is 01111111, not 00000000

lcd_write_byte(0x02) is equivalent to lcd_write_nibble(0) followed by lcd_write_nibble(2)

"So taking 0b11000011 as PORTD..." It's not. PD7 is low because of lcd_set_type_command. PD6 is low because it's only high during that first delay_us(1) call in lcd_write_nibble. PD1 and PD0 may or may not be low--those are not controlled by lcd.c. So without seeing the rest of your program I would say that PORTD at that point is 000000XX where X=don't know. Then it goes to 010000XX for 1us, and then back to 000000XX.

And then, when lcd_write_nibble(2) happens, PORTD goes to 000010XX momentarily, and then 010010XX for 1us, and then back to 000010XX which is where lcd_write_byte(0x02) leaves it.

Post a Reply

Please log in to post a reply.

Did you know that NerdKits has been featured on Slashdot, Hack A Day, Hacked Gadgets, the MAKE blog, and other DIY-oriented websites? Learn more...