使用STM32 HAL库实现LCD1602显示屏

首先在Cubemx里使能1602管脚

1.c文件

#include "LCD1602.h"

void LCD1602_Writecom(uint8_t com)
{
  GPIOA->ODR = 0x00FF; //初始化PA0--PA7 为低电平
  HAL_GPIO_WritePin(LCD_RS_GPIO_Port,LCD_RS_Pin,GPIO_PIN_RESET);
  HAL_GPIO_WritePin(LCD_RW_GPIO_Port,LCD_RW_Pin,GPIO_PIN_RESET);
  HAL_GPIO_WritePin(LCD_EN_GPIO_Port,LCD_EN_Pin,GPIO_PIN_RESET);
  HAL_Delay(1);
  GPIOA->ODR=(com|0xFF00);
  HAL_GPIO_WritePin(LCD_EN_GPIO_Port,LCD_EN_Pin,GPIO_PIN_SET);
  HAL_Delay(1);
  HAL_GPIO_WritePin(LCD_EN_GPIO_Port,LCD_EN_Pin,GPIO_PIN_RESET);

}

void LCD1602_Writedat(uint8_t dat)
{
  GPIOA->ODR = 0x00FF; //初始化PA0--PA7 为低电平
  HAL_GPIO_WritePin(LCD_RS_GPIO_Port,LCD_RS_Pin,GPIO_PIN_SET);
  HAL_GPIO_WritePin(LCD_RW_GPIO_Port,LCD_RW_Pin,GPIO_PIN_RESET);
  HAL_GPIO_WritePin(LCD_EN_GPIO_Port,LCD_EN_Pin,GPIO_PIN_RESET);
  HAL_Delay(1);
  GPIOA->ODR=(dat|0xFF00);
  HAL_GPIO_WritePin(LCD_EN_GPIO_Port,LCD_EN_Pin,GPIO_PIN_SET);
  HAL_Delay(1);
  HAL_GPIO_WritePin(LCD_EN_GPIO_Port,LCD_EN_Pin,GPIO_PIN_RESET);
}
void LCD1602_Display(uint8_t row, uint8_t col)
{
  uint8_t adder;
 if(row==1)
 {
   adder = 0x80+col;
 }
 if(row==2)
 {
   adder = 0xC0+col;
 }
   LCD1602_Writecom(adder);
}

void LCD_showstr(uint8_t row,uint8_t col,uint8_t *str)
{
  LCD1602_Display(row,col);
  while(*str!='\0')
  {
   LCD1602_Writedat(*str++);
  }
}

void LCD1602_init()
{
  LCD1602_Writecom(0x38);
  LCD1602_Writecom(0x0c); 
  LCD1602_Writecom(0x06);
  LCD1602_Writecom(0x01);
}

2..h文件

#ifndef LCD1602_LCD1602_H
#define LCD1602_LCD1602_H
#include "main.h"
void LCD1602_Writecom(uint8_t com);
void LCD1602_Writedat(uint8_t dat);
void LCD1602_Display(uint8_t row,uint8_t col);
void LCD1602_init();
void LCD_showstr(uint8_t row,uint8_t col,uint8_t *str);
#endif //LCD1602_LCD1602_H

3.初始化1602

4.在main()添加函数 

 

 第一次写博客。

物联沃分享整理
物联沃-IOTWORD物联网 » 使用STM32 HAL库实现LCD1602显示屏

发表评论