📬❄️📦Adafruit Holiday Shipping Deadlines 2019: Please place all UPS 3 Day orders by 11am ET Friday December 13 📬❄️📦
0

Need help on sample codes for LCD8Bit
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Need help on sample codes for LCD8Bit

by mikelua on Mon Nov 03, 2008 3:10 am

I'm new with Arduino and have very limited programming experience. I was looking at the 8 Bit LCD sample in Arduino tutorial (http://arduino.cc/en/Tutorial/LCD8Bits) and was having problem understanding the sample codes. Can anyone explain it to me?

For loop:

for (i=DB[0]; i <= DB[7]; i++) {
digitalWrite(i,value & 01);
value >>= 1;
}

Question # 1: For digitalWrite(pin,value), what would a value of "value & 01" mean/do?

Question # 2: What does "value >>= 1" do?

I have copied below the full code from the arduino tutorial page.

Thanks & Best Regards,
Michael


/* LCD Hola
* --------
*
* This is the first example in how to use an LCD screen
* configured with data transfers over 8 bits. The example
* uses all the digital pins on the Arduino board, but can
* easily display data on the display
*
* There are the following pins to be considered:
*
* - DI, RW, DB0..DB7, Enable (11 in total)
*
* the pinout for LCD displays is standard and there is plenty
* of documentation to be found on the internet.
*
* (cleft) 2005 DojoDave for K3
*
*/

int DI = 12;
int RW = 11;
int DB[] = {3, 4, 5, 6, 7, 8, 9, 10};
int Enable = 2;

void LcdCommandWrite(int value) {
// poll all the pins
int i = 0;
for (i=DB[0]; i <= DI; i++) {
digitalWrite(i,value & 01);
value >>= 1;
}
digitalWrite(Enable,LOW);
delayMicroseconds(1);
// send a pulse to enable
digitalWrite(Enable,HIGH);
delayMicroseconds(1); // pause 1 ms according to datasheet
digitalWrite(Enable,LOW);
delayMicroseconds(1); // pause 1 ms according to datasheet
}

void LcdDataWrite(int value) {
// poll all the pins
int i = 0;
digitalWrite(DI, HIGH);
digitalWrite(RW, LOW);
for (i=DB[0]; i <= DB[7]; i++) {
digitalWrite(i,value & 01);
value >>= 1;
}
digitalWrite(Enable,LOW);
delayMicroseconds(1);
// send a pulse to enable
digitalWrite(Enable,HIGH);
delayMicroseconds(1);
digitalWrite(Enable,LOW);
delayMicroseconds(1); // pause 1 ms according to datasheet
}

void setup (void) {
int i = 0;
for (i=Enable; i <= DI; i++) {
pinMode(i,OUTPUT);
}
delay(100);
// initiatize lcd after a short pause
// needed by the LCDs controller
LcdCommandWrite(0x30); // function set:
// 8-bit interface, 1 display lines, 5x7 font
delay(64);
LcdCommandWrite(0x30); // function set:
// 8-bit interface, 1 display lines, 5x7 font
delay(50);
LcdCommandWrite(0x30); // function set:
// 8-bit interface, 1 display lines, 5x7 font
delay(20);
LcdCommandWrite(0x06); // entry mode set:
// increment automatically, no display shift
delay(20);
LcdCommandWrite(0x0E); // display control:
// turn display on, cursor on, no blinking
delay(20);
LcdCommandWrite(0x01); // clear display, set cursor position to zero
delay(100);
LcdCommandWrite(0x80); // display control:
// turn display on, cursor on, no blinking
delay(20);
}

void loop (void) {
LcdCommandWrite(0x02); // set cursor position to zero
delay(10);
// Write the welcome message
LcdDataWrite('H');
LcdDataWrite('o');
LcdDataWrite('l');
LcdDataWrite('a');
LcdDataWrite(' ');
LcdDataWrite('C');
LcdDataWrite('a');
LcdDataWrite('r');
LcdDataWrite('a');
LcdDataWrite('c');
LcdDataWrite('o');
LcdDataWrite('l');
LcdDataWrite('a');
delay(500);
}

mikelua
 
Posts: 2
Joined: Mon Nov 03, 2008 2:56 am

Re: Need help on sample codes for LCD8Bit

by mtbf0 on Mon Nov 03, 2008 4:58 am

MikeLua wrote:I'm new with Arduino and have very limited programming experience. I was looking at the 8 Bit LCD sample in Arduino tutorial (http://arduino.cc/en/Tutorial/LCD8Bits) and was having problem understanding the sample codes. Can anyone explain it to me?

For loop:

for (i=DB[0]; i <= DB[7]; i++) {
digitalWrite(i,value & 01);
value >>= 1;
}

Question # 1: For digitalWrite(pin,value), what would a value of "value & 01" mean/do?

Question # 2: What does "value >>= 1" do?


"value & 01" gives you the low order bit of value. if for example value were 171, it would be 10101011 in binary. 1 is 00000001 in binary. & is a bitwise logical and operation, which means that each bit in the lefthand operand is anded with its corresponding bit in the righthand operand. the result of an and operation is "1" if and only if both of its operands are "1", so the result of 10101011 & 00000001 is 00000001. had the low order bit of the lefthand been "0" the result of the operation would have been 00000000. so the purpose of the operation value & 01 is, as i said, to extract the low order, (ie rightmost) bit of value.

"value >>= 1" shifts value one bit to the right. this is equivalent to dividing by two. the result of shifting 171 one bit to the right is 85, or 01010101. the effect of this is to discard the bit that was extracted in the previous & operation.

by putting these two operations in the for loop all 8 bits of value are extracted and output to the appropriate pins of the lcd.

the bitwise and and shift operators are both described in the arduino reference.
"i want to lead a dissipate existence, play scratchy records and enjoy my decline" - iggy pop, i need more
User avatar
mtbf0
 
Posts: 1645
Joined: Sat Nov 10, 2007 12:59 am
Location: oakland ca

Re: Need help on sample codes for LCD8Bit

by mikelua on Mon Nov 03, 2008 9:32 am

Thank you so much for explaining the bitwise and, the shift operator and how the two operations are used in the for loop. It's brilliant. :)

mikelua
 
Posts: 2
Joined: Mon Nov 03, 2008 2:56 am

Please be positive and constructive with your questions and comments.