STM32 and LCD via I2C


    For future use, it was necessary to connect, using the I2C STM32 microcontroller with a 2004 screen. Having not found a similar solution on the network, I publish it here. This recipe is also suitable for screens 1602. Further under the cut. (Caution, pictures).

    The toy box purchased by the son turned out to be defective, and worked every other time. There was an idea to remake its internals, and the moment of choosing the microcontroller coincided with the publication of RaJa's article about STM32 [ 1 ]. Having estimated and compared the prices a bit: STM32 + LCD2004 + I2C = ArduinoMega (the reason was that you had to implement a keyboard, speaker, barcode input device and screen, so each output of the microcontroller counts) I chose the first set.

    Purchases were made and it was time to wait. I also bought a USB-USART adapter for firmware.
    What and where to buy.
    1. STM32F103C8T6
    2. 2004 LCD HD44780 . It turned out without the Cyrillic alphabet. Pay attention to this feature when searching if you need Russian on the screen.
    3. IIC / I2C / TWI / SP I Serial Interface Board Module Port For Arduino 1602LCD Display The description is compatible with 2004. But I think any similar will do.
    4. USB to UART TTL CP2012 for firmware and debugging. You can use other supported methods of firmware and debugging, but this option is the cheapest.


    Means for programming, firmware and debugging used by me:
    1. EmBlocks .
    2. Flasher from the original site: STM32 and STM8 Flash loader demonstrator .
    3. Terminal for reading signals from MK via USB2UART: Terminal v1.91b . But Putty (Connection-> Serial) is also suitable.

    After receiving the microcontroller, I tried to play with the LEDs, it turned out. And then there were several hours of trying to connect the screen with MK. It’s boring to describe all this, I’ll try to recall the rake that I ran into.

    The first to describe the connection. It’s strange when describing the use of STM32 in few places they draw circuits, mostly code, you will guess what and how to connect.
    I will connect the connection in the photo (by clicking - larger). This connection is relevant for STM32F103C8. For other MK boards, check the I2C1 connection pins for a datasheet.


    USART adapter to USB. It’s clear here. Next - connect USART to STM32 to the output near miniUSB connector USART1. TX to RX and accordingly RX to TX. I have a 3v3 pin on USART, and I powered MK from it. I connected the earth separately, for conveniently turning it off during the switching of the firmware and operation modes. I soldered I2C to the screen (there are also screens with soldered I2C on ebay). Power for I2C and the screen is taken from 3v3 MK or 5V from USART. Below I wrote about the contrast setting for various supply voltages. Next: SCL from I2C connects to PB6, SDA from I2C to PB7. It is not necessary to draw SCL and SDA to power when using this device alone.

    The first rake was USART. I used it for debugging, in the code given here the lines of work with it are commented out. But he did not solve the problem. It seems that there is no synchronization between the computer and the microcontroller before sending the first character. Moreover, if you use the code from example [ 4 ], then MK perfectly duplicates the resulting text, but cannot write it. I achieved the most acceptable for debugging line output by adding Delay (500) after each character.

    Then I tried to implement work with I2C. I took the code from the example [ 3 ], drew attention to the comments about the suspension of MK, having analyzed the source, I saw that, like the author of the comments, I need to shift the device address to the left:

    //http://microtechnics.ru/stm32-ispolzovanie-i2c/#comment-8109
    I2C_Send7bitAddress(I2Cx, slaveAddress<<1, transmissionDirection);
    

    I inserted the code and tried to run it. The program hung on the moment of waiting for the release of the bus:

    while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
    

    There is a rake in the address of the I2C device. Judging from the description of the seller, I had the address 0x20. Here I lost 15 minutes in vain, but after reading the description of different models of I2C adapters, the link [ 6 ] to which romanvl cited in my article [ 5 ] , drew attention to the latest model and tried to change the address to 0x27. Everything worked. The conclusion is this: if you have A0 A1 A2 soldered on the adapter, the address is 0x20, not soldered - 0x27. Compare:


    Next is the screen. It turned out that it also works fine from 3.3 Volts, like the I2C adapter (in the adapter chip datasheets - from 2.5 to 6 V). But first, I checked it from 5V. And the contrast was twisted to the maximum. As a result, as a result of starting the program, the screen was completely full. I was upset and continued to pick the code. But after half an hour I woke up and the culprit of the development ran up, I showed him the screen and accidentally saw from an angle from the side that something was written there. The reason for this is incorrect contrast adjustment. (Sorry, if I described the obvious things here, there may be those who did not know this.)
    Nothing is visible
    The same, but at an angle

    With 5V power, contrast needs to be slightly reduced. And at 3.3V set to maximum, nothing is visible on the setting from 5V. The result is presented in the first picture in the post. My turned out to be without the Russian language, I saw it by scrolling through the characters. I tried to draw a blot, not knowing that it is possible to define a maximum of 8 of my own characters, I wrote for blot 12. I picked up similar ones from Chinese, it seemed to work out.

    The code is presented on the gihab, because to achieve the result, I rewrote the library from Arduina: STM32_LCD_I2C .

    Used materials:
    1. Reason for choosing a microcontroller: STM32 vs Arduino .
    2. The Delay implementation came from here: STM32 I2C EEPROM 24СXX .
    3. Article about I2C STM32. Using I2C. microtechnics.ru/stm32-ispolzovanie-i2c . There is also a comment about the shift, without which I would probably catch the rake just like the ones discussing.
    4. An example of working with USART STM32. USART Part 4 - The Final .
    5. Reduce the number of wires in Arduino - I2C LCD screen and RTC clock on two wires .
    6. LCD Displays (Blue and YELLOW) with I2C / TWI Interface .
    7. To understand the logic of the STM32 with external devices Quick Start Guide for working with peripherals STM32F10x .

    Thanks for attention.

    Also popular now: