Learning from Arduino. Three LCD Libraries for the M2 & X2 chips

Flenser

Senior Member
I have uploaded these three ports of the Arduino liquidcrystal LCD library to the PICAXE chips:
For driving character LCDs in 8-bit mode https://picaxeforum.co.uk/threads/lcd-library-lcd_lib-8bit.33077/
For driving character LCDs in 4-bit mode https://picaxeforum.co.uk/threads/lcd-library-lcd_lib-4bit.33078/
For driving character LCDs using the PCF8574-based I2C backpack https://picaxeforum.co.uk/threads/lcd-library-pcf8574-i2c.33079/

Why did I write these?

The following times where the forum was unable to help people to drive LCD displays using a PICAXE chip had stuck in my mind:
- In March 2021 we were not able to help Peter Howarth (New Member) and ended up recommending a book by Ron Hackett or a pair of articles from Nuts & Volts for him to try their tutorials.
- In Dec 2021 dennis shubert (New Member) ended up deciding "Think I will get an arduino and lib may work then Thanks for your time."
- In Feb 2022 woschx (New Member) ended up deciding to use an Arduino to drive the LCD instead of a PICAXE and made the comment "With an Arduino and the appropriate library, I have now been able to control the display. It's a pity that there doesn't seem to be anything like that for the Picaxe." (worschx later tried out PhilHornby's SainsmartLCD2004.bas code and from his comment ended up being able to use it instead of an Arduino.)

For Arduino users using the LiquidCrystal LCD library is pretty easy.
Here is all the code needed to configure the LiquidCrystal library and initialize the LCD taken from that library's "Hello World" example program.:
Code:
// include the library code:
#include <LiquidCrystal.h>

// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

// set up the LCD's number of columns and rows:
lcd.begin(16, 2);

That's it. These four lines complete the configuration of the library and the initialization of the LCD.
The rest of the code in the "Hello World" example simply calls the LCD library's commands to send commands and write characters to the LCD.

On the other hand, when users ask the forum for help using an LCD in their PICAXE program this is what happens:
- One or more forum members provide a link to a working example program that drives an LCD display that:
- use different pins and ports to connect to the LCD and​
- give different instructions on how to connect the LCD to the PICAXE chip, either an ASCII schematic or as a text list of connections.​
- The user that we are helping needs to read these programs and understand them well enough to either modify the example for their own use or else to copy the LCD code out of the example into their own program.
- The program examples we provide mostly send hex values as commands to the LCD so the user we are helping has to read (from somewhere) and understand how these hex values are formatted.
- I did find a couple of exceptions to this. PhilHornby's SainsmartLCD2004.bas code from 2013 and Rolfi's SubsMacros_Serial_LCD.basinc code from 2015.​
- The user we are helping then has to understand what they have written and how to control LCDs well enough to be able to debug their new program.

It appears to me that the current situation is that it's easier for people who are relatively inexperienced with microcontrollers and/or programming to use compiled C on the Arduino to drive an LCD than it is for them to use interpreted BASIC on a PICAXE chip. To quote the character Jubal Early from the TV Series Firefly "Does that seem right to you?"

So woschx's question about why there are no libraries for the PICAXE struck me as a fair one now that PE has the #INCLUDE directive and PICAXE code can use include-only libraries .

This prompted me to:
1. Try writing a simple LCD library to find out if it could be done (it can) and to discover what some of the issues are in writing and using include-only libraries with PICAXE interpreted BASIC (this is a subject for another post)
2. The Arduino LiquidCrystal LCD library's API seemed to me to be a good one to standardize on so next I wanted see if I could port the entire library to the PICAXE (and with a couple of relatively minor syntax changes I could).
3. Having got this far I went ahead and generated two more versions of my library. One using 4-bit mode and one using the PCF8574-based I2C backpack.
- My three libraries implement the same set of commands with just two exceptions. The LCD_LIB() command is specific to each library and the PCF8574 backpack library supports turning LCD backlight off and on.​

Finally, here is the code needed to configure my 8-bit LCD_LIB library and initialize the LCD taken from my PICAXE version of the "Hello World" example program:
Code:
; Include the LCD_LIB configuration macro
#INCLUDE "LCD_LIB-8bit-Config-Macro.basinc"

; Configure all the LCD_LIB settings
;LCD_LIB(columns, rows, lcdEnablePin, lcdEnablePinDir, lcdRsPin, lcdRspinDir, lcdPort, lcdPortDir, wTempVar, bTempVar1, bTempVar2, bLcdSettings)
LCD_LIB(16, 2, outpinC.1, dirC.1, outpinC.2, dirC.2, outpinsB, dirsB, w4, b3, b4, b5)

; Include the LCD_LIB library code
#INCLUDE "LCD_LIB-8bit.basinc"

LCD_begin             ; initialise LCD
My intention was to try and make it as easy as possible to use character-based LCDs with the PICAXE chips and using LCDs with the 4-bit and 8-bit libraries will still require getting all the connections right, including the contrast pot.
However I hope that having a library could make it almost trivially easy to use LCDs with the PCF8574-based I2C backpack as there are only 4 wires to get right when connecting the LCD backpack to the PICAXE chip.

At the moment my project posts make this claim about how easy they are to use:
To use it:
1. Download the files
2. Extract the library files and example programs from the zip file into a directory.
3. Connect your PICAXE chip to the LCD. The manual has pictures showing how to connect each PICAXE chip to the LCD to work with the example programs.
4. Download one of the example programs into the PICAXE using Program Editor.
That's it!

but this is only tested by me. If anyone tests them and finds that this is not true then I would like to hear about it.

All bug reports for these libraries are welcome.
 
Last edited:

kfjl

Member
Hi,
Using linux, linaxepad and a 20X2, I can't get past the first #INCLUDE with the syntaxe check.

26036
 

Flenser

Senior Member
Using linux, linaxepad and a 20X2, I can't get past the first #INCLUDE with the syntaxe check.
kfil, as far as I'm aware the Rev-Ed PICAXE precompiler is only available on Windows. There is no Rev-Ed precompiler available for Linux so you won't be able to use any of the precompiler "#" directives.

The user pliser is maintaining a Python script that can process the PICAXE "#" directives on Linux & Mac OS and in this post https://picaxeforum.co.uk/threads/is-picaxe-support-for-mac-officially-dead.32196/post-336485 he describes the latest updates to his script and gives the link where you can download it.
I know that his Python precompiler script can be used with MS Visual Studio on Linux but I've never used linaxepad and I don't know whether it can be configured to use the precompiler script.
 

kfjl

Member
OK.
I got the same error using Winaxepad and I no longer have PE6 installed, so I'll leave it there.
 

erco

Senior Member
Flenser, you sir are a credit to the forum! Thanks for your dedication to rectifying this issue. It's true that cheap, abundant LCDs are a PITA with Picaxe. Surprising that Rev-Ed hasn't jumped on this problem. I don't have time to dive into this right now but hopefully folks will benefit from your excellent work. Full marks!
 
Top