Monday, February 18, 2013

STM32F4 Character LCD


Finally another post about electronics! (and yeah, haven't been blogging as much as I'd like, life has been busy busy busy)

Code can be found here.

I recently just completed a project (as a precursor to another project) to interface a 4 line by 20 character HD44780 LCD with my STM32F4 Discovery(link to digikey, ST site is being updated right now). The project is fairly simple, but still decently educational (particularly if you have little experience with strings in C). Overall it took about three weekends to complete, though would have taken much much less had I not hit a wall partway through due to a typo in the code, but it all works now! (I may make some minor changes to it at some point, but it is feature complete as of now, with the exception of custom characters which will come in a week or so).

The library itself is not particularly well optimized, in order to achieve the slow communication speed required by the LCD I used delays in the code, rather than a timer on the chip, which results in a huge number of lost cycles. I plan on doing another, more optimized version of this at a later date using timers, though I will leave this intact as it is far easier to understand and adapt to other projects/controllers. The timings used in the delays will also probably be reduced as I find more time to experiment with them, but for now they are still pretty quick (see the second half of the video below for the real speeds).

The video was taken on my phone so I know quality isn't great,
watch it on youtube though at 480p to get the best out of it (not much I know)

It should be simple enough to adapt the library to your needs, though I do plan on adding some comments to the code and could write a tutorial if people want. The code is designed to work well no matter how many lines or characters per line your LCD has, the only thing that would need additional work is if you wanted to use 4-bit communication, which would require a bit of rewriting for sending data (in "nibbles" as opposed to all at once), as well as the initialization, but the rest should work fine. I am always willing to answer any questions that people have as well!

If you want to use the same LCD that I used, you can get it from seeed studio here, though they do take awhile to ship. This LCD doesn't use an HD44780 driver, but the one it uses is pin compatible. The library should be pretty easy to adapt to a 2-line display as well, most of the code is there and all of it should adapt well to other numbers of lines and characters per line.

As for the electronics side, I hooked it up like this (taken from pinout.txt in the repository):

LCD     Function      STM32F4
1       Ground        -------
2       VCC           -------
3       Contrast      -(pot)- [may use GPIOD 6 at a later date]
4       RS            GPIOD 7
5       R/W           GPIOD 8
6       Clock(enable) GPIOD 9
7       Bit 0         GPIOE 4
8       Bit 1         GPIOE 5
9       Bit 2         GPIOE 6
10      Bit 3         GPIOE 7
11      Bit 4         GPIOE 8
12      Bit 5         GPIOE 9
13      Bit 6         GPIOE10
14      Bit 7         GPIOE11
15      Backlight(+)  GPIOD10 [potentially]
16      Backlight(-)  -------

GPIOE4-11 were chosen for the data as they are the only contiguous(code-wise) bank of 8 pins I could find on the F4 Discovery board that are not attached to an external peripheral. Choosing them like this allows for a nifty bit of code for writing the data, seen here:

GPIOE->ODR=((GPIOE->ODR&0xF00F) | (data << 4));

This sets the ODR (Output Data Register) on GPIOE to either the original state for non-data pins (so as to not interrupt other functions of the controller when using the LCD), or the state for our data (which is shifted 4 bits("pins") up so as to not disturb GPIOE0-3).

Please feel free to let me know if this helps you in your project!

Enjoy!

(and a few more detailed pictures:)

 A very pretty display module from seeed

An absolute mess of wires, but it works!