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!

5 comments:

  1. assalou alaykom
    Did you connect the GND of your LCD with the GND of your STM32F4 and the VCC also ? because you didn't announce in your publication
    thank you ...

    ReplyDelete
    Replies
    1. Hi there, yes, both GND are connected together and the VCC is connected to 5V on the discovery. The contrast is connected to the output of a potentiometer that sits between 5V and GND, though there may be other ways of connecting them.

      The LED backlight is also connected to the 5V and GND on the discovery.

      Let me know if you need any more help or have more questions.

      Delete
    2. điều khó khăn lắm, chẳng qua Long Đồ này vẫn có thứ để dụng, bắt sống mới là mục đích của Nhạc Thành.

      Ở trên bầu trời, Long Đồ lại cuồng tiếu nói ra:

      - Còn nhỏ tuổi như vậy, có thể gan dạ đối chiến đúng là không dễ, chỉ mong thực lực của ngươi không để cho ta quá thất vọng.

      Tuy nhiên Long Đồ cũng không nhìn thấy rõ thực lực của Nhạc Thành, trên người của Nhạc Thành tràn ngập một khí thế, đại biểu cho thực lực bất phàm, Ldongtam
      mu private
      tim phong tro
      http://nhatroso.com/
      nhac san cuc manh
      tổng đài tư vấn luật
      http://dichvu.tuvanphapluattructuyen.com/
      văn phòng luật
      tổng đài tư vấn pháp luật
      thành lập công ty
      http://we-cooking.com/
      chém gió
      trung tâm ngoại ngữong Đồ cũng thong thả toát ra một khí tức cường hãn, tuôn ra dữ dội, tựa hồ như hắn không muốn cho Nhạc Thành nhiều cơ hội muốn sớm giải quyết cuộc chiến.

      Nhạc Thành nhìn Long Đồ trên bầu trời, hắn ngửa mặt trên bầu trời cười dìa một tiếng, Âm Dương Kiếm phát ra nhưng thanh âm on gong không ngừng, khí thế cũng trở nên tăng vọt, khuếch tán ở trên không trung, sau đó quát lạnh về phía Long Đồ:

      Delete
  2. Hi;
    I have 2-16 LCD NHD and i want to connect it with STM32F407VG.
    Do you have any code or idea to write on this LCD ?

    ReplyDelete
  3. Hi;
    I have 2-16 LCD NHD and i want to connect it with STM32F407VG.
    Do you have any code or idea to write on this LCD ?

    ReplyDelete