Interface LCD 16x2 with STM32 by using STM32CubeIDE
πππππππππππππππππππππππ
πππππππππππππππππππππππ
main.c dosyasΔ± :
#include "main.h"
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
/* USER CODE BEGIN PFP */
#include "LCD.h"
#include <stdio.h>
/* USER CODE END PFP */
int main(void)
{
HAL_Init();
MX_GPIO_Init();
/* USER CODE BEGIN 2 */
lcd_init(_LCD_4BIT, _LCD_FONT_5x8, _LCD_2LINE);
lcd_print(1, 1, "Merhaba"); //1.satΔ±rΔ±n 1. sΓΌtunu
lcd_print(2, 1, "DΓΌnya"); //2. satΔ±rΔ±n 1. sΓΌtunu
/* USER CODE END 2 */
char yazi[16] = " " ;//yazi isminde, char tipinde 16 elemanΔ± olan bir dizi
int i = 0;
HAL_Delay(2500);
lcd_clear();
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
i++;
lcd_print(1, 1, "SayΔ± : ");
sprintf(yazi,"%d",i);
lcd_print(2, 1, yazi);
HAL_Delay(1000);
lcd_clear();
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSE;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}
}
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
HAL_GPIO_WritePin(GPIOA, LCD_EN_Pin|LCD_RS_Pin|LCD_D4_Pin|LCD_D5_Pin
|LCD_D6_Pin|LCD_D7_Pin, GPIO_PIN_RESET);
GPIO_InitStruct.Pin = LCD_EN_Pin|LCD_RS_Pin|LCD_D4_Pin|LCD_D5_Pin
|LCD_D6_Pin|LCD_D7_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
void Error_Handler(void)
{
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
πππππππππππππππππππππππ
πππππππππππππππππππππππ
LCD.c dosyasΔ±
πππππππππππππππππππππππ
πππππππππππππππππππππππ
LCD.h dosyasΔ± :
#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);
πππππππππππππππππππππππ
πππππππππππππππππππππππ
Comments
Post a Comment