An LCD Gotcha

Last week I posted some AVR gotchas, but now it’s time for an LCD gotcha!

<rue_shop2> Landon, for over 6 years, everyone who gets a hd447880  screen has written their own library

Yep, that’s what I did. I wrote some LCD helpers in assembly for this project. The big problem I encountered was my strings were getting truncated when I read them from the EEPROM and displayed them on the LCD. After verifying my EEPROM code was correct, I turned to my LCD code. I had just switched to waiting on the Busy Flag of the LCD instead of a max delay.

Assuming all of my ports are set up correctly (PortD6 is the Enable line of the LCD)

STILL_BUSY:
  sbi PORTD, 6 ; enable
  nop
  in R16, PIND ; read in Data7 (attached to PortD3)
  cbi PORTD, 6 ; disable
  andi R16, 0x08 ; mask out all but pin 3
  brne STILL_BUSY

Ok, you might not see the problem yet. Let me give you some more information. I was also working in 4 bit mode. Look again.

The gotcha is that with 4 bit, reads need to be done twice, otherwise you’ll be reading part of the current address next time you strobe the Enable line. Duh! This wasn’t apparent in the article I was using though, because 4 bit mode was almost an afterthought in the microcontroller implementation and there was no way you would be punching buttons fast enough by hand to need to wait on the Busy Flag.

Corrected code:

STILL_BUSY:
  sbi PORTD, 6 ; enable
  nop
  in R16, PIND ; read in Data7 (attached to PortD3)
  cbi PORTD, 6 ; disable
  rcall PULSE_ENABLE ; need to call twice, because of 4 bit mode
  andi R16, 0x08 ; mask out all but pin 3
  brne STILL_BUSY