Library 16- Interfacing HD44780 LCD controller with STM32F4
16th library is here. We will interfacing HD44780 (and its compatible) driver for alpha-numeric LCDs. Library supports up to 20 x 4 LCD size. It was tested with 20 x 4 (on picture) and with 16 x 2.
HD44780 Library
Features
- 4bit operation mode
- Minimum GPIOs used (6)
- Supports different LCD sizes
- Supports up to 8 custom characters
- Enable/disable cursor blinking
- Show/hide cursor
- Shift content in ram left/right
- Connection pins to board are user selectable
- Automatically jumps to new line when you reach max X on LCD
- Strings with \n, \r or \n\r
- With \n in string LCD jumps to lower line, but X position stays the same
- With \r in string LCD jumps to the beginning of the line
- With \n\rĀ in string LCD jumps to the beginning of a new line
Library dependencies
- CMSIS
- STM32F4xx
- STM32F4xx RCC
- STM32F4xx GPIO
- TM
- TM DELAY
- TM GPIO
- defines.h
- TM DELAY
LCD | STM32F4xx | DESCRIPTION |
---|---|---|
GND | GND | Ground |
VCC | +5V | Power supply for LCD |
V0 | Potentiometer | Contrast voltage. Connect to potentiometer |
RS | PB2 | Register select, can be overwritten in your project’s defines.h file |
RW | GND | Read/write |
E | PB7 | Enable pin, can be overwritten in your project’s defines.h file |
D0 | Data 0 – doesn’tĀ care | |
D1 | Data 1Ā – doesn’tĀ care | |
D2 | Data 2Ā – doesn’tĀ care | |
D3 | Data 3Ā – doesn’tĀ Ā care | |
D4 | PC12 | Data 4, can be overwritten in your project’s defines.h file |
D5 | PC13 | Data 5, can be overwritten in your project’s defines.h file |
D6 | PB12 | Data 6, can be overwritten in your project’s defines.h file |
D7 | PB13 | Data 7, can be overwritten in your project’s defines.h file |
A | +3V3 | Backlight positive power |
K | GND | Ground for backlight |
If you need to change your pins, open defines.h files, add lines below and edit them.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
/* Control pins */ /* RS - Register select pin */ //#define HD44780_RS_PORT GPIOB //#define HD44780_RS_PIN GPIO_Pin_2 /* E - Enable pin */ //#define HD44780_E_PORT GPIOB //#define HD44780_E_PIN GPIO_Pin_7 /* D4 - Data 4 pin */ //#define HD44780_D4_PORT GPIOC //#define HD44780_D4_PIN GPIO_Pin_12 /* D5 - Data 5 pin */ //#define HD44780_D5_PORT GPIOC //#define HD44780_D5_PIN GPIO_Pin_13 /* D6 - Data 6 pin */ //#define HD44780_D6_PORT GPIOB //#define HD44780_D6_PIN GPIO_Pin_12 /* D7 - Data 7 pin */ //#define HD44780_D7_PORT GPIOB //#define HD44780_D7_PIN GPIO_Pin_13 |
Functions and enumerations
All possible functions are listed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
/** * Initialize LCD * * Parameters: * - uint8_t cols: width of lcd * - uint8_t rows: height of lcd * * No return */ extern void TM_HD44780_Init(uint8_t cols, uint8_t rows); /** * Turn display on * * No return */ extern void TM_HD44780_DisplayOn(void); /** * Turn display off * * No return */ extern void TM_HD44780_DisplayOff(void); /** * Clear entire LCD * * No return */ extern void TM_HD44780_Clear(void); /** * Put string on lcd * * Parameters: * - uint8_t x: x location * - uint8_t y: y location * - char* str: pointer to string * * No return */ extern void TM_HD44780_Puts(uint8_t x, uint8_t y, char* str); /** * Enable cursor blink * * No return */ extern void TM_HD44780_BlinkOn(void); /** * Disable cursor blink * * No return */ extern void TM_HD44780_BlinkOff(void); /** * Show cursor * * No return */ extern void TM_HD44780_CursorOn(void); /** * Hide cursor * * No return */ extern void TM_HD44780_CursorOff(void); /** * Scroll display to the left * * No return */ extern void TM_HD44780_ScrollLeft(void); /** * Scroll display to the right * * No return */ extern void TM_HD44780_ScrollRight(void); /** * Create custom character * * Parameters: * - uint8_t location: * Location where to save character on LCD. LCD supports up to 8 custom characters, so locations are 0 - 7 * - uint8_t *data * Pointer to 8bytes of data for one character * * No return */ extern void TM_HD44780_CreateChar(uint8_t location, uint8_t* data); /** * Put custom created character on LCD * * Parameters: * - uint8_t location * Location on LCD where character is stored, 0 - 7 * * No return */ extern void TM_HD44780_PutCustom(uint8_t x, uint8_t y, uint8_t location); |
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
/** * Keil project example for HD44780 LCD driver * * @author Tilen Majerle * @email tilen@majerle.eu * @website http://stm32f4-discovery.net * @ide Keil uVision 5 */ #include "defines.h" #include "stm32f4xx.h" #include "tm_stm32f4_delay.h" #include "tm_stm32f4_hd44780.h" int main(void) { //Rectangle for custom character //xxx means doesn't care, lower 5 bits are important for LCD uint8_t customChar[] = { 0x1F, // xxx 11111 0x11, // xxx 10001 0x11, // xxx 10001 0x11, // xxx 10001 0x11, // xxx 10001 0x11, // xxx 10001 0x11, // xxx 10001 0x1F // xxx 11111 }; //Initialize system SystemInit(); //Initialize LCD 20 cols x 4 rows TM_HD44780_Init(20, 4); //Save custom character on location 0 in LCD TM_HD44780_CreateChar(0, &customChar[0]); //Put string to LCD TM_HD44780_Puts(0, 0, "STM32F4/29 Discovery"); TM_HD44780_Puts(2, 1, "20x4 HD44780 LCD"); TM_HD44780_Puts(0, 2, "stm32f429-\n\r discovery.com"); //Wait a little Delayms(3000); //Clear LCD TM_HD44780_Clear(); //Show cursor TM_HD44780_CursorOn(); //Write new text TM_HD44780_Puts(6, 2, "CLEARED!"); //Wait a little Delayms(1000); //Enable cursor blinking TM_HD44780_BlinkOn(); //Show custom character at x = 1, y = 2 from RAM location 0 TM_HD44780_PutCustom(1, 2, 0); while (1) { } } |
Project isĀ available onĀ Github, download library below.
HD44780 (and compatible) LCD driver controller
Recent comments