STM32Cube配置LCD1602液晶屏

  1. 实验目的:
    • 了解LCD1602的使用方法.
    • 掌握编写LCD1602显示程序的方法.
  2. 实验环境:
    • 硬件:STM32F103C8T6核心板,PC,USB线.
    • 软件:Windows10,STM32CubeIDE,HAL库.
  3. 实验原理:
    • LCD1602可以显示两行,每行16个字符.
    • LCD1602一共有11根线用于与MCU通信,具体如下:
      RS: 0–状态/命令 1–数据
      RW: 0–写 1–读
      E: 0–使能 1–失能
      D0-D7: 数据命令总线
    • LCD1602的基本操作可以分为以下4种:
      读状态: 输入RS=0, RW=1, E=0
      读数据: 输入RS=1, RW=1, E=0
      写命令: 输入RS=0, RW=0, E=0
      写数据: 输入RS=1, RW=0, E=0
      详见LCD1602时序图及其对照表.
      //在这个实验当中,我们只需要配置E,RS,RW,D4-D7这7个引脚即可.
  4. 程序设计:
  • 这里我们选择外部高速时钟(HSE),需要在System Core中配置RCC;
  • 配置GPIO引脚
    E--------------------PB0
    RS------------------PB1
    RW-----------------PB2
    D4------------------PB12
    D5------------------PB13
    D6------------------PB14
    D7------------------PB15
  • 配置SYS
    STM32Cube配置LCD1602液晶屏_第1张图片
  • 配置时钟树
    STM32Cube配置LCD1602液晶屏_第2张图片
  • 配置TIM1
    STM32Cube配置LCD1602液晶屏_第3张图片
    STM32Cube配置LCD1602液晶屏_第4张图片
  • 生成代码
  • 编写用户代码
    • 编写LCD1602驱动程序
    • LCD1602.C文件
/*
        LCD1602.C
         
*/
#include 
#include "stm32f1xx_hal.h"

/***********Define the LCD PINS below*********/

#define RS_Pin GPIO_PIN_1
#define RS_GPIO_Port GPIOB
#define RW_Pin GPIO_PIN_2
#define RW_GPIO_Port GPIOB
#define E_Pin GPIO_PIN_0
#define E_GPIO_Port GPIOB
#define D4_Pin GPIO_PIN_12
#define D4_GPIO_Port GPIOB
#define D5_Pin GPIO_PIN_13
#define D5_GPIO_Port GPIOB
#define D6_Pin GPIO_PIN_14
#define D6_GPIO_Port GPIOB
#define D7_Pin GPIO_PIN_15
#define D7_GPIO_Port GPIOB     //�������� 
 
/********define the timer handler beloe**********/
#define timer htim1


extern TIM_HandleTypeDef timer;
void delay (uint16_t us)
{
     
	__HAL_TIM_SET_COUNTER(&timer, 0);
	while(__HAL_TIM_GET_COUNTER(&timer) < us);
}

/*************************************************/

void send_to_lcd (char data, int rs)
{
     
	HAL_GPIO_WritePin(RS_GPIO_Port, RS_Pin, rs);
	/*write the data to the respective pin*/
	HAL_GPIO_WritePin(D7_GPIO_Port, D7_Pin, ((data>>3)&0x01));
	HAL_GPIO_WritePin(D6_GPIO_Port, D6_Pin, ((data>>2)&0x01));
	HAL_GPIO_WritePin(D5_GPIO_Port, D5_Pin, ((data>>1)&0x01));
	HAL_GPIO_WritePin(D4_GPIO_Port, D4_Pin, ((data>>0)&0x01));
	/*toggle EN PIN to send the data
	 *if the HCLK > 100HZ,use the 20us delay
	 *if LCD still doesn't work,increase the delay to 50, 80 or 100..
	 */
	HAL_GPIO_WritePin(E_GPIO_Port, E_Pin, 1);
    delay (20);
    HAL_GPIO_WritePin(E_GPIO_Port, E_Pin, 0);
    delay (20);

}

void lcd_send_cmd (char cmd)
{
     
	char datatosend;
	
	/*send upper nibble first*/
	datatosend = ((cmd>>4)&0x0f);
	send_to_lcd(datatosend,0);  //RS must be while sending command
	
	/*send Lower Nibble*/
	datatosend = ((cmd)&0x0f);
	send_to_lcd(datatosend,0);
}

void lcd_send_data (char data)
{
     
	char datatosend;
	
	/*send upper nibble first*/
	datatosend = ((data>>4)&0x0f);
	send_to_lcd(datatosend, 1);  //rs = 1 for sending data
	
	/*send Lower Nibble*/
	datatosend = ((data)&0x0f);
	send_to_lcd(datatosend, 1);
}

void lcd_clear (void)
{
     
	lcd_send_cmd(0x01);
	HAL_Delay(2);
}
void lcd_put_cur(int row, int col)
{
     
	switch (row)
	{
     
		case 0:
			col |= 0x80;
			break;
		case 1:
			col |=0xc0;
			break;
	}
	lcd_send_cmd (col);
}

void lcd_init (void)
{
     
	//4 bit initialisation
	HAL_Delay(50); //wait for >40ms
	lcd_send_cmd (0x30);
	HAL_Delay(5); //wait for >4.1ms
	lcd_send_cmd (0x30);
	HAL_Delay(1); //wait for >100us
	lcd_send_cmd (0x30);
	HAL_Delay(10);
	lcd_send_cmd (0x20); //4 bit mode
	HAL_Delay(10);
	
	//dislay initialisation
	lcd_send_cmd (0x28); //function set -->DL=0 (4 bit mode), N = 1(2 lioe display) F = 0(5X8 characters)
	HAL_Delay(1); 
	lcd_send_cmd (0x08);
	HAL_Delay(1);
	lcd_send_cmd (0x01);
	HAL_Delay(1);
	HAL_Delay(1);
	lcd_send_cmd (0x06);
	HAL_Delay(1);
	lcd_send_cmd (0x0c);
}

void lcd_send_string (char *str)
{
     
	while (*str) lcd_send_data (*str++);
}

  • LCD1602.h文件
#ifndef INC__LCD1602_H_
#define INC__LCD1602_H_ 
 

void lcd_init(void);  //initialize lcd

void lcd_send_cmd(char cmd);  //send command to the lcd

void lcd_send_data(char data);  //send data to the lcd

void lcd_send_string(char *str);  //send string to the lcd

void lcd_put_cur(int row, int col);  //put cursor at the entered position row (0 or 1), col (0-15)

void lcd_clear(void);

 
#endif /*INC_LCD1602_H_*/

main.c中编写简单的用户代码

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
int row=0;
int col=0;
/* USER CODE END PFP */

int main(void)
{
     
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */
  

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_TIM1_Init();
  /* USER CODE BEGIN 2 */
  HAL_TIM_Base_Start(&htim1);

  lcd_init ();
  lcd_put_cur(0, 0);
  lcd_send_string("HELLO ");
  lcd_send_string("STM32 ");
  lcd_send_string("CUBE");

  lcd_put_cur(1,0);
  lcd_send_string("hello ");
  lcd_send_string("world");
  HAL_Delay(3000);
  lcd_clear();
  /* USER CODE END 2 */
  1. 编译并下载;
  2. 实验现象;
    • LCD1602第一行显示HELLO STM32 CUBE.
    • LCD1602第二行显示hello world.

以上就是全部内容,希望对同是STM32Cube小白的人有所帮助!!!

你可能感兴趣的:(STM32Cube,stm32,嵌入式,单片机)