09 September 2021

8-Bit Character LCD/Character LCD Clock Update

In the Character LCD Clock post I mentioned wanting to test out 8-bit communication and having to write my own library for it, and I recently remembered about it to stick it in my projects folder.

I ended up grabbing the datasheet for the 20×4 display that I have and Adafruit's character_lcd.py source code for references, but when I really started looking at both of them (and playing with some things in a Python shell), I found I didn't need to write my own library and could just modify Adafruit's code. As for what I modified, I changed _LCD_4BITMODE = const(0x00) to _LCD_4BITMODE = const(0x10) to set it to 8-bit mode from 4-bit. Yes the variable name didn't get changed, but it means that there is minimal change to the code. The other thing I changed was the __init__( arguments for the additional data pins, the class variables that are made from those arguments, the write function (specifics soon), and the __init__( arguments for Character_LCD_Mono and Character_LCD_RGB.

In the write function, it normally sets the "top" four bits to be written on the pins set to self.dl4 through self.dl7, sends a three-part pulse (self._pulse_enable() function), then sets the "bottom" four bits on the same four pins before another pulse. For 8-bit, all eight pins need to be set, and I just brought up the lines for the "bottom" four bits above the lines for the "top" four bits and then changed the class variables to match the ones I added before. The pulse only needs to be sent once after the eight pins are set.

Character_LCD_Mono and Character_LCD_RGB have the __init__( arguments change since they pass the pins over to the parent class (the 20×4 display I have does have a backlight).

I used mpy-cross to turn it into a smaller .mpy file and tried it out. I ended up making it the wrong version because I never recompiled mpy-cross after setting the CircuitPython version to compile when I was compiling CircuitPython. Anyway, after that, the 8-bit library worked just fine, and I thought it seems faster, but I think it's just a placebo. The reason is that the pulse function has three 1-microsecond pauses and the only other limiting factor is the 1-millisecond pause in the write function before it sets all the pins; therefore with 4-bit, 1006 microseconds is spent to write one character, and 1003 microseconds for 8-bit. It's very negligible, and I can understand why Adafruit never bothered with making an 8-bit library or why 4-bit is so much more common, it's because the benefit for eating up four more pins is not worthwhile in any meaningful way. However, 8-bit makes sense when it's a small project that doesn't use all that many pins, which means it's just to connect the pins to something rather than nothing.

And because of this, I decided to make the Character LCD Clock have an 8-bit connection just to use up some more pins and not let my library modding go to waste (well, it's not that hard of a mod, but still...).

No comments:

Post a Comment