ragulin
Posts: 60
Joined: Tue Dec 22, 2015 6:52 pm

LCD High and Low bits

Sun May 08, 2016 3:36 pm

Hey, I need a little help with understanding the code I use. I know that this code sets the 4 bits operational mode for my LCD display. I cant find the reason of the "if" function and its parameters in the datasheet though....thanks for advice !

The code:

Code: Select all

# High bits
  GPIO.output(LCD_D4, False)
  GPIO.output(LCD_D5, False)
  GPIO.output(LCD_D6, False)
  GPIO.output(LCD_D7, False)
  if bits&0x10==0x10:
    GPIO.output(LCD_D4, True)
  if bits&0x20==0x20:
    GPIO.output(LCD_D5, True)
  if bits&0x40==0x40:
    GPIO.output(LCD_D6, True)
  if bits&0x80==0x80:
    GPIO.output(LCD_D7, True)

# Low bits
  GPIO.output(LCD_D4, False)
  GPIO.output(LCD_D5, False)
  GPIO.output(LCD_D6, False)
  GPIO.output(LCD_D7, False)
  if bits&0x01==0x01:
    GPIO.output(LCD_D4, True)
  if bits&0x02==0x02:
    GPIO.output(LCD_D5, True)
  if bits&0x04==0x04:
    GPIO.output(LCD_D6, True)
  if bits&0x08==0x08:
    GPIO.output(LCD_D7, True)
And the datasheet of my LCD!
http://www.winstar.com.tw/UserFiles/dow ... 064564.pdf

rzusman
Posts: 347
Joined: Fri Jan 01, 2016 10:27 pm

Re: LCD High and Low bits

Sun May 08, 2016 4:51 pm

It's inefficient code.
The bits are all set to zero first, then set to 1 one-at-a-time if they need to be.

It would be better to treat all 8 GPIOs as a byte, and set them all at once.

ragulin
Posts: 60
Joined: Tue Dec 22, 2015 6:52 pm

Re: LCD High and Low bits

Sun May 08, 2016 5:47 pm

rzusman wrote:It's inefficient code.
The bits are all set to zero first, then set to 1 one-at-a-time if they need to be.

It would be better to treat all 8 GPIOs as a byte, and set them all at once.

My circuit works in 4 bits mode, I just dont know why and how I have this parts in there...I dont see a reason why its if and bits&0x10 etc..

Code: Select all

 if bits&0x10==0x10:
    GPIO.output(LCD_D4, True)

rzusman
Posts: 347
Joined: Fri Jan 01, 2016 10:27 pm

Re: LCD High and Low bits

Sun May 08, 2016 7:11 pm

It's redundant.
Take any references to bits 4,5,6,7 out.

Return to “Beginners”