HD44780 Library and more!

Well in light of my recent hardware addition, I’ve decided to convert the basis of my HD44780 code into a standalone library. I’ve also added the ability to print signed integer numbers with padding with my whole code including examples compiling to < 3.5k at O0 optimization! Snazzy or what, with O3 optimization, I’ve managed to get it down to 2.4k, further decreasing to 2.22k with O2 optimization and finally settling at 2.188k with O1 optimization. I don’t really see why you’d need to optimize it this heavily but for proof that you can, voila! I’ve only tested these optimizations with the example projects though I can’t see anything that would get optimized out accidentally. Saying that however, Os optimization compiles but doesn’t correctly run on my STM32F0 platform.

My completely engineering styled example, completely un-aesthetically pleasing and simple functionality!

And finally, moving onto the millisecond counter.

I never was one for aesthetics but hopefully the example program offers a few examples of what the code can do! The library I’ve moved the code into is really simple and only offers a few functions, of which I will explain below:

void H_HWInit(void)
Really simple function, its one use is to initialize all of the GPIO pins and clocks required to use the HD44780 library.

void H_LEDPWM(void)
Once again, another simple function that needs to be placed in the SysTick interrupt handler. This produces the PWM waveform output required to change the brightness of the LED. My article for this can be found at: https://hsel.co.uk/2014/08/15/hd44780-lcd-with-pwm-led-backlight-control/

void H_ChargePump(void)
Again, a simple function used to toggle the charge pump pin as required to create a negative voltage to drive the LCD contrast pin. My article on this can be found at: https://hsel.co.uk/2014/08/14/stm32f0-discovery-board-with-hd44780-and-integrated-charge-pump/

void H_W8b(uint8_t Dat, uint8_t RD)
The function used to send the two nibbles one after another to the LCD. The first value passed is the 8 bit piece of data and the second value passed denotes whether the data should be written to a command register (RD value of 0) or to the DDRam (RD value of 1).

int8_t PStr(const char* Str, uint8_t X, uint8_t Y)
The beginning of the cool functions! A pretty simple function used to print a string. the parameters passed are the string, the X position on the screen and the Row (1 or 2) that the text is displayed on. Text is best passed to this function as per example: Printing the string “Hi LCD!” on row 1 at X position 0 would be best done by writing PStr(“Hi LCD!”, 0, 1); as the magical language known as C will automatically append the null terminator after your string. This string returns the values -3, -2, -1 or 0. A return of -3 will mean that the string was too long to be printed. A return of -2 will mean an inaccessible row was entered for the row parameter. A return of -1 states that the string will go off the screen if printed and is dependent on the length of the string for example if the string “Hi LCD!” (string length 7) was printed at X position 15, the string would go off the screen (7+15 >16) whereas if the string was printed at X position 4, the string will print fine. If 0 is returned, the function executed correctly and your string should now be displayed on screen!

int8_t PChar(char C, uint8_t X, uint8_t Y)
Another simple function used to print a single character onto the screen. Uses the same return system as PStr, except for the -3 value. If the character is placed out of bounds with respect to the X value, -1 will be returned. If the character is placed on a non existent row, -2 will be returned. If all is good, 0 will be returned!

int8_t PNum(int32_t Num, uint8_t X, uint8_t Y, uint8_t Pad)
My favourite of all the printing functions! This function prints a signed integer number to the LCD with the option of additional number padding. Padding will insert zeros before the useful digits and after the – sign. For example, printing the number “123” with a pad of 0 will literally print “123” at the respective X and Y positions. Printing that same number with a pad of 2 however will print “00123” at the respective position. This may seem useless to some but its good for displaying things like time to ensure the places that digits are displayed to is always constant for example 1:2:3 with a pad of 1 on each number will be equal to 01:02:03. The PNum function follows the same return system as PStr where string length is replaced with number length.

void ClrDisp(void)
The simplest of all functions! It clears the display. A delay of 1ms is added to ensure the LCD has time to recover after a screen clear. This might not be required however I’ve included it just in case.

As of now, the library is blocking and uses delays to ensure the LCD has enough time to process all commands. The HD44780 series of LCD’s do support a busy flag though I find that implementing this would be more trouble than its worth as you need to start including edge triggered interrupts and the whole library suddenly expands drastically!

I’ve updated the library a few times on Github and if previous revisions are wanted, you can just look through my past commits! To find the library as of now: https://github.com/pyrohaz/STM32F0-HD44780LCD

Hopefully somebody finds it useful and stay tuned for more STM32F0 device implementations!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s