Youtube'da başlatmış olduğum CubeMX ile STM32F103 programlama serisinde kullandığım LCD HAL kütüphanesini burada paylaşıyorum.
Öncelikle kütüphanenin orijinalinin bulunduğu blog sayfasının linkini paylaşmak istiyorum.
Kütüphanenin Orijinal Halinin bulunduğu Blog Sayfası
LCD.C Kodları
/*
Karakter LCD Kütüphanesi
Hüseyin Cemre Yilmaz
hcemreyilmaz@gmail.com
Düzenleyen
Bahadir Aydinoglu 19.02.19
bahadirayd@gmail.com
Kullanim : CubeMX ile proje olustururken 6 adet GPIO pini
asagidaki gibi tanimlanmalidir.
No. | Pin Name| Type | Alt. Func. | Label
----------------------------------------------
xx | Pxx | I/O | GPIO_Output | LCD_RS
xx | Pxx | I/O | GPIO_Output | LCD_EN
xx | Pxx | I/O | GPIO_Output | LCD_D4
xx | Pxx | I/O | GPIO_Output | LCD_D5
xx | Pxx | I/O | GPIO_Output | LCD_D6
xx | Pxx | I/O | GPIO_Output | LCD_D7
Pin ve port farketmeksizin, Output olarak tanimlanan pinler
yukaridaki gibi isimlendirilmeli ve baglanti gerektigi gibi
yapilmalidir.
Init Örnegi:
lcd_init(_LCD_4BIT, _LCD_FONT_5x8, _LCD_2LINE);
4-Bit, 5x8 Font'lu, 2 satirli display kullanilacagi belirtiliyor.
Farkli varyasyonlara LCD.h dosyasindan bakabilirsiniz.
Kontrast ayari için pot kullanmak istemiyorsaniz baglantiniz asagidaki
gibi olmalidir.
VDD-------------LCD VDD
|
10K
|
|---------------LCD VEE
|
1K
|
GND-------------LCD VSS
*/
#include "stm32f1xx_hal.h"
#include "main.h"
#include "LCD.h"
void Delay(uint32_t nCount)
{
for(; nCount != 0; nCount--);
}
void lcd_delay(void)
{
Delay(1000);
}
void lcd_cmd(char out_char)
{
LCD_RS_GPIO_Port->BRR = LCD_RS_Pin;
LCD_EN_GPIO_Port->BRR = LCD_EN_Pin;
LCD_D4_GPIO_Port->BRR = LCD_D4_Pin;
LCD_D5_GPIO_Port->BRR = LCD_D5_Pin;
LCD_D6_GPIO_Port->BRR = LCD_D6_Pin;
LCD_D7_GPIO_Port->BRR = LCD_D7_Pin;
lcd_delay();
LCD_EN_GPIO_Port->ODR |= LCD_EN_Pin;
if((out_char & 0x10)>>4) LCD_D4_GPIO_Port->BSRR = LCD_D4_Pin; else LCD_D4_GPIO_Port->BSRR = (uint32_t)LCD_D4_Pin << 16;
if((out_char & 0x20)>>5) LCD_D5_GPIO_Port->BSRR = LCD_D5_Pin; else LCD_D5_GPIO_Port->BSRR = (uint32_t)LCD_D5_Pin << 16;
if((out_char & 0x40)>>6) LCD_D6_GPIO_Port->BSRR = LCD_D6_Pin; else LCD_D6_GPIO_Port->BSRR = (uint32_t)LCD_D6_Pin << 16;
if((out_char & 0x80)>>7) LCD_D7_GPIO_Port->BSRR = LCD_D7_Pin; else LCD_D7_GPIO_Port->BSRR = (uint32_t)LCD_D7_Pin << 16;
lcd_delay();
LCD_EN_GPIO_Port->BRR = LCD_EN_Pin;
LCD_D4_GPIO_Port->BRR = LCD_D4_Pin;
LCD_D5_GPIO_Port->BRR = LCD_D5_Pin;
LCD_D6_GPIO_Port->BRR = LCD_D6_Pin;
LCD_D7_GPIO_Port->BRR = LCD_D7_Pin;
lcd_delay();
LCD_EN_GPIO_Port->ODR |= LCD_EN_Pin;
if(out_char & 0x01) LCD_D4_GPIO_Port->BSRR = LCD_D4_Pin; else LCD_D4_GPIO_Port->BSRR = (uint32_t)LCD_D4_Pin << 16;
if((out_char & 0x02)>>1) LCD_D5_GPIO_Port->BSRR = LCD_D5_Pin; else LCD_D5_GPIO_Port->BSRR = (uint32_t)LCD_D5_Pin << 16;
if((out_char & 0x04)>>2) LCD_D6_GPIO_Port->BSRR = LCD_D6_Pin; else LCD_D6_GPIO_Port->BSRR = (uint32_t)LCD_D6_Pin << 16;
if((out_char & 0x08)>>3) LCD_D7_GPIO_Port->BSRR = LCD_D7_Pin; else LCD_D7_GPIO_Port->BSRR = (uint32_t)LCD_D7_Pin << 16;
lcd_delay();
LCD_EN_GPIO_Port->BRR = LCD_EN_Pin;
LCD_D4_GPIO_Port->BRR = LCD_D4_Pin;
LCD_D5_GPIO_Port->BRR = LCD_D5_Pin;
LCD_D6_GPIO_Port->BRR = LCD_D6_Pin;
LCD_D7_GPIO_Port->BRR = LCD_D7_Pin;
}
void lcd_char_cp(char out_char)
{
LCD_RS_GPIO_Port->ODR |= LCD_RS_Pin;
LCD_EN_GPIO_Port->BRR = LCD_EN_Pin;
LCD_D4_GPIO_Port->BRR = LCD_D4_Pin;
LCD_D5_GPIO_Port->BRR = LCD_D5_Pin;
LCD_D6_GPIO_Port->BRR = LCD_D6_Pin;
LCD_D7_GPIO_Port->BRR = LCD_D7_Pin;
lcd_delay();
LCD_EN_GPIO_Port->ODR |= LCD_EN_Pin;
if((out_char & 0x10)>>4) LCD_D4_GPIO_Port->BSRR = LCD_D4_Pin; else LCD_D4_GPIO_Port->BSRR = (uint32_t)LCD_D4_Pin << 16;
if((out_char & 0x20)>>5) LCD_D5_GPIO_Port->BSRR = LCD_D5_Pin; else LCD_D5_GPIO_Port->BSRR = (uint32_t)LCD_D5_Pin << 16;
if((out_char & 0x40)>>6) LCD_D6_GPIO_Port->BSRR = LCD_D6_Pin; else LCD_D6_GPIO_Port->BSRR = (uint32_t)LCD_D6_Pin << 16;
if((out_char & 0x80)>>7) LCD_D7_GPIO_Port->BSRR = LCD_D7_Pin; else LCD_D7_GPIO_Port->BSRR = (uint32_t)LCD_D7_Pin << 16;
lcd_delay();
LCD_EN_GPIO_Port->BRR = LCD_EN_Pin;
LCD_D4_GPIO_Port->BRR = LCD_D4_Pin;
LCD_D5_GPIO_Port->BRR = LCD_D5_Pin;
LCD_D6_GPIO_Port->BRR = LCD_D6_Pin;
LCD_D7_GPIO_Port->BRR = LCD_D7_Pin;
lcd_delay();
LCD_EN_GPIO_Port->ODR |= LCD_EN_Pin;
if(out_char & 0x01) LCD_D4_GPIO_Port->BSRR = LCD_D4_Pin; else LCD_D4_GPIO_Port->BSRR = (uint32_t)LCD_D4_Pin << 16;
if((out_char & 0x02)>>1) LCD_D5_GPIO_Port->BSRR = LCD_D5_Pin; else LCD_D5_GPIO_Port->BSRR = (uint32_t)LCD_D5_Pin << 16;
if((out_char & 0x04)>>2) LCD_D6_GPIO_Port->BSRR = LCD_D6_Pin; else LCD_D6_GPIO_Port->BSRR = (uint32_t)LCD_D6_Pin << 16;
if((out_char & 0x08)>>3) LCD_D7_GPIO_Port->BSRR = LCD_D7_Pin; else LCD_D7_GPIO_Port->BSRR = (uint32_t)LCD_D7_Pin << 16;
lcd_delay();
LCD_EN_GPIO_Port->BRR = LCD_EN_Pin;
LCD_D4_GPIO_Port->BRR = LCD_D4_Pin;
LCD_D5_GPIO_Port->BRR = LCD_D5_Pin;
LCD_D6_GPIO_Port->BRR = LCD_D6_Pin;
LCD_D7_GPIO_Port->BRR = LCD_D7_Pin;
}
void lcd_out_cp(char *out_char)
{
while(*out_char)
{
lcd_delay();
lcd_char_cp(*out_char++);
}
lcd_delay();
}
void lcd_init(char bits, char font, char lines)
{
HAL_Delay(250);
lcd_cmd(_RETURN_HOME);
HAL_Delay(50);
lcd_cmd(0x20 | bits | font | lines);
HAL_Delay(50);
lcd_cmd(_LCD_INIT);
HAL_Delay(50);
lcd_cmd(0x0E);
HAL_Delay(50);
lcd_cmd(0x0C);
HAL_Delay(50);
lcd_cmd(0x01);
HAL_Delay(100);
}
void lcd_gotoxy(unsigned char row, unsigned char column)
{
if(row == 1)
{
lcd_cmd(0x80 + (column - 1));
}
else if(row == 2)
{
lcd_cmd(0xC0 + (column - 1));
}
}
void lcd_char(unsigned char row, unsigned char column, char out_char)
{
lcd_gotoxy(row, column);
lcd_char_cp(out_char);
}
void lcd_print(unsigned char row, unsigned char column, char *out_char)
{
lcd_gotoxy(row, column);
lcd_out_cp(out_char);
}
void lcd_clear(void) {
lcd_cmd(_CLEAR);
HAL_Delay(2);
}
void lcd_line1(void) {
lcd_cmd(0x80);
}
void lcd_line2(void) {
lcd_cmd(0xC0);
}
LCD.h Kodları
#define _TURN_ON 0x0C // Turn Lcd display on
#define _TURN_OFF 0x08 // Turn Lcd display off
#define _FIRST_ROW 0x80 // Move cursor to the 1st row
#define _SECOND_ROW 0xC0 // Move cursor to the 2nd row
#define _CLEAR 0x01 // Clear display
#define _RETURN_HOME 0x02 // Return cursor to home position, returns a shifted display to its original position. Display data RAM is unaffected.
#define _CURSOR_OFF 0x0C // Turn off cursor
#define _UNDERLINE_ON 0x0E // Underline cursor on
#define _BLINK_CURSOR_ON 0x0F // Blink cursor on
#define _MOVE_CURSOR_LEFT 0x10 // Move cursor left without changing display data RAM
#define _MOVE_CURSOR_RIGHT 0x14 // Move cursor right without changing display data RAM
#define _SHIFT_LEFT 0x18 // Shift display left without changing display data RAM
#define _SHIFT_RIGHT 0x1C // Shift display right without changing display data RAM
#define _LCD_4BIT 0x00
#define _LCD_8BIT 0x10
#define _LCD_FONT_5x8 0x00
#define _LCD_FONT_5x10 0x04
#define _LCD_1LINE 0x00
#define _LCD_2LINE 0x08
#define _LCD_INIT 0x06
//#define _LCD_INIT 0x04
//#define _LCD_INIT 0x05
//#define _LCD_INIT 0x07
void lcd_delay(void);
void lcd_cmd(char out_char);
void lcd_char_cp(char out_char);
void lcd_char(unsigned char row, unsigned char column, char out_char);
void lcd_out_cp(char *out_char);
void lcd_print(unsigned char row, unsigned char column, char *out_char);
void lcd_gotoxy(unsigned char row, unsigned char column);
void lcd_init(char bits, char font, char lines);
void lcd_clear(void);
void lcd_line1(void);
void lcd_line2(void);
https://youtu.be/QLZogGTzIeE