/**
******************************************************************************
* File Name : LCD.c
* Description : This file provides code for the configuration
* of the LCD instances.
******************************************************************************
* @attention
*
* <h2><center>© Copyright (c) 2020 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "lcd.h"
/* USER CODE BEGIN 0 */
static void ConvertNum(uint8_t buff[4],int InputNum);
static void Disp_PW(uint8_t num);
static void Disp_GW(uint8_t num);
static void Disp_SW(uint8_t num);
static void Disp_BW(uint8_t num);
/* USER CODE END 0 */
LCD_HandleTypeDef hlcd;
/* LCD init function */
void MX_LCD_Init(void)
{
hlcd.Instance = LCD;
hlcd.Init.Prescaler = LCD_PRESCALER_2;
hlcd.Init.Divider = LCD_DIVIDER_31;
hlcd.Init.Duty = LCD_DUTY_1_4;
hlcd.Init.Bias = LCD_BIAS_1_3;
hlcd.Init.VoltageSource = LCD_VOLTAGESOURCE_INTERNAL;
hlcd.Init.Contrast = LCD_CONTRASTLEVEL_5;
hlcd.Init.DeadTime = LCD_DEADTIME_2;
hlcd.Init.PulseOnDuration = LCD_PULSEONDURATION_7;
hlcd.Init.HighDrive = LCD_HIGHDRIVE_0;
hlcd.Init.BlinkMode = LCD_BLINKMODE_OFF;
hlcd.Init.BlinkFrequency = LCD_BLINKFREQUENCY_DIV8;
hlcd.Init.MuxSegment = LCD_MUXSEGMENT_DISABLE;
if (HAL_LCD_Init(&hlcd) != HAL_OK)
{
Error_Handler();
}
}
void HAL_LCD_MspInit(LCD_HandleTypeDef* lcdHandle)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(lcdHandle->Instance==LCD)
{
/* USER CODE BEGIN LCD_MspInit 0 */
/* USER CODE END LCD_MspInit 0 */
/* LCD clock enable */
__HAL_RCC_LCD_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/**LCD GPIO Configuration
PC3 ------> LCD_SEG21
PA1 ------> LCD_SEG0
PA2 ------> LCD_SEG1
PA3 ------> LCD_SEG2
PA6 ------> LCD_SEG3
PA7 ------> LCD_SEG4
PC4 ------> LCD_SEG22
PB0 ------> LCD_SEG5
PB1 ------> LCD_SEG6
PB10 ------> LCD_SEG10
PB11 ------> LCD_SEG11
PB12 ------> LCD_SEG12
PB14 ------> LCD_SEG14
PB15 ------> LCD_SEG15
PA8 ------> LCD_COM0
PA9 ------> LCD_COM1
PA10 ------> LCD_COM2
PB9 ------> LCD_COM3
*/
GPIO_InitStruct.Pin = GPIO_PIN_3|GPIO_PIN_4;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF1_LCD;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_6
|GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF1_LCD;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_10|GPIO_PIN_11
|GPIO_PIN_12|GPIO_PIN_14|GPIO_PIN_15|GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF1_LCD;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* USER CODE BEGIN LCD_MspInit 1 */
/* USER CODE END LCD_MspInit 1 */
}
}
void HAL_LCD_MspDeInit(LCD_HandleTypeDef* lcdHandle)
{
if(lcdHandle->Instance==LCD)
{
/* USER CODE BEGIN LCD_MspDeInit 0 */
/* USER CODE END LCD_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_LCD_CLK_DISABLE();
/**LCD GPIO Configuration
PC3 ------> LCD_SEG21
PA1 ------> LCD_SEG0
PA2 ------> LCD_SEG1
PA3 ------> LCD_SEG2
PA6 ------> LCD_SEG3
PA7 ------> LCD_SEG4
PC4 ------> LCD_SEG22
PB0 ------> LCD_SEG5
PB1 ------> LCD_SEG6
PB10 ------> LCD_SEG10
PB11 ------> LCD_SEG11
PB12 ------> LCD_SEG12
PB14 ------> LCD_SEG14
PB15 ------> LCD_SEG15
PA8 ------> LCD_COM0
PA9 ------> LCD_COM1
PA10 ------> LCD_COM2
PB9 ------> LCD_COM3
*/
HAL_GPIO_DeInit(GPIOC, GPIO_PIN_3|GPIO_PIN_4);
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_6
|GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10);
HAL_GPIO_DeInit(GPIOB, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_10|GPIO_PIN_11
|GPIO_PIN_12|GPIO_PIN_14|GPIO_PIN_15|GPIO_PIN_9);
/* USER CODE BEGIN LCD_MspDeInit 1 */
/* USER CODE END LCD_MspDeInit 1 */
}
}
/* USER CODE BEGIN 1 */
const uint16_t mask[4]={0xF000,0x0F00,0x00F0,0x000F};
const uint8_t offset[4]={12,8,4,0};
const uint16_t Num_Map[15]={0x1323,0x0220,0x1132,0x1232,0x0231,0x1213,0x1313,0x0222,0x1333,0x1233,0x0010,0x0331,0x0200,0x1101,0x1310};
// 0 1 2 3 4 5 6 7 8 9 ---- H
/*******************************************************************************
* Function Name : ConvertNum
* Description : 查找输入参数对应的字符编码
* Inputs : InputNum--显示字符
* Outputs : None
* Return : None
* Notes : None
*******************************************************************************/
static void ConvertNum(uint8_t buff[4],int InputNum)
{
uint16_t Num, tmp = 0;
uint8_t i;
if((InputNum>=0)&&(InputNum<=15))
{
Num=Num_Map[InputNum];
}
else
{
Num=0;
}
for(i = 0;i < 4; i++)
{
tmp = Num & mask[i];
buff[i] = (uint8_t)(tmp >> offset[i]);
}
}
/*******************************************************************************
* Function Name : Disp_BW(uint8_t num)
* Description : 显示百位数字
* Inputs : 数字
* Outputs : None
* Return : None
* Notes : None
*******************************************************************************/
void Disp_BW(uint8_t num)
{
uint8_t num_buf[4];
ConvertNum(num_buf,num);
HAL_LCD_Write(&hlcd, COM1, Mask_B_COM1, num_buf[0]<<SEG_B_COM1);
HAL_LCD_Write(&hlcd, COM2, Mask_B, num_buf[1]<<SEG_B);
HAL_LCD_Write(&hlcd, COM3, Mask_B, num_buf[2]<<SEG_B);
HAL_LCD_Write(&hlcd, COM4, Mask_B, num_buf[3]<<SEG_B);
HAL_LCD_UpdateDisplayRequest(&hlcd);
}
/*******************************************************************************
* Function Name : Disp_SW(uint8_t num)
* Description : 显示十位数字
* Inputs : 数字
* Outputs : None
* Return : None
* Notes : None
*******************************************************************************/
void Disp_SW(uint8_t num)
{
uint8_t num_buf[4];
ConvertNum(num_buf,num);
HAL_LCD_Write(&hlcd, COM1, Mask_S_COM1, num_buf[0]<<SEG_S_COM1);
HAL_LCD_Write(&hlcd, COM2, Mask_S, num_buf[1]<<SEG_S);
HAL_LCD_Write(&hlcd, COM3, Mask_S, num_buf[2]<<SEG_S);
HAL_LCD_Write(&hlcd, COM4, Mask_S, num_buf[3]<<SEG_S);
HAL_LCD_UpdateDisplayRequest(&hlcd);
}
/*******************************************************************************
* Function Name : Disp_GW(uint8_t num)
* Description : 显示个位数字
* Inputs : 数字
* Outputs : None
* Return : None
* Notes : 由于BT3和个位显示有冲突,所以在此处理
*******************************************************************************/
void Disp_GW(uint8_t num)
{
uint8_t num_buf[4];
ConvertNum(num_buf,num);
HAL_LCD_Write(&hlcd, COM1, Mask_G_COM1, num_buf[0]<<SEG_G_COM1);
HAL_LCD_Write(&hlcd, COM2, Mask_G, num_buf[1]<<SEG_G);
HAL_LCD_Write(&hlcd, COM3, Mask_G, num_buf[2]<<SEG_G);
HAL_LCD_Write(&hlcd, COM4, Mask_G, num_buf[3]<<SEG_G);
HAL_LCD_UpdateDisplayRequest(&hlcd);
}
/*******************************************************************************
* Function Name : Disp_PW(uint8_t num)
* Description : 显示十分位数字
* Inputs : 数字
* Outputs : None
* Return : None
* Notes : 由于十分位的两个段并不是连续的,所以在此再次对十分位要显示的数据进行处理
*******************************************************************************/
void Disp_PW(uint8_t num)
{
uint8_t num_buf[4];
uint8_t Num_For_Point=0;//再次处理十分位数据所用
ConvertNum(num_buf,num);
/*由于十分位的两个段并不是连续的,所以在此再次对十分位要显示的数据进行处理*/
for(Num_For_Point=1;Num_For_Point<4;Num_For_Point++)
{
if(num_buf[Num_For_Point]==0x3)
{
num_buf[Num_For_Point]=0x5;
}
else if(num_buf[Num_For_Point]==0x2)
{
num_buf[Num_For_Point]=0x4;
}
}
HAL_LCD_Write(&hlcd, COM1, Mask_P_COM1, num_buf[0]<<SEG_P_COM1);
HAL_LCD_Write(&hlcd, COM2, Mask_P, num_buf[1]<<SEG_P);
HAL_LCD_Write(&hlcd, COM3, Mask_P, num_buf[2]<<SEG_P);
HAL_LCD_Write(&hlcd, COM4, Mask_P, num_buf[3]<<SEG_P);
HAL_LCD_UpdateDisplayRequest(&hlcd);
}
/*******************************************************************************
* Function Name : Display_Num(float temp)
* Description : 显示小数
* Inputs : 小数
* Outputs : None
* Return : None
* Notes :
*******************************************************************************/
void Display_Num(float temp)
{
uint16_t Num=0;
uint8_t Bai=0,Shi=0,Ge=0,Point=0;
Num=(int)(temp*10.0);
if(Num<1000)
{
Shi=Num/100;
Ge=Num%100/10;
Point=Num%10;
Disp_SW(Shi);
Disp_GW(Ge);
Disp_PW(Point);
Diaplay_Icon(POINT,1);//显示小数点
}
else
{
Bai=Num/1000;
Shi=Num%1000/100;
Ge=Num%100/10;
Point=Num%10;
Disp_BW(Bai);
Disp_SW(Shi);
Disp_GW(Ge);
Disp_PW(Point);
Diaplay_Icon(POINT,1);//显示小数点
}
}
/*******************************************************************************
* Function Name : Diaplay_Icon(ICON icon)
* Description : 显示图标
* icon : 枚举类型ICON中的图标名称
* status : 枚举类型ICON中的图标显示状态
* Outputs : None
* Return : None
* Notes : None
*******************************************************************************/
void Diaplay_Icon(ICON icon,uint8_t status)
{
if(icon==Temp_C)
{
HAL_LCD_Write(&hlcd, COM_Temp_C, Mask_Temp_C, status<<SEG_Temp_C);
HAL_LCD_UpdateDisplayRequest(&hlcd);
}
else if(icon==Temp_F)
{
HAL_LCD_Write(&hlcd, COM_Temp_F, Mask_Temp_F, status<<SEG_Temp_F);
HAL_LCD_UpdateDisplayRequest(&hlcd);
}
else if(icon==Buzzer)
{
HAL_LCD_Write(&hlcd, COM_Buzzer, Mask_Buzzer, status<<SEG_Buzzer);
HAL_LCD_UpdateDisplayRequest(&hlcd);
}
else if(icon==Body)
{
HAL_LCD_Write(&hlcd, COM_Body, Mask_Body, status<<SEG_Body);
HAL_LCD_UpdateDisplayRequest(&hlcd);
}
else if(icon==WuTi)
{
HAL_LCD_Write(&hlcd, COM_WuTi, Mask_WuTi, status<<SEG_WuTi);
HAL_LCD_UpdateDisplayRequest(&hlcd);
}
else if(icon==BLUE)
{
HAL_LCD_Write(&hlcd, COM_BLUE, Mask_BLUE, status<<SEG_BLUE);
HAL_LCD_UpdateDisplayRequest(&hlcd);
}
else if(icon==**)
{
HAL_LCD_Write(&hlcd, COM_**, Mask_**, status<<SEG_**);
HAL_LCD_UpdateDisplayRequest(&hlcd);
}
else if(icon==POINT)
{
HAL_LCD_Write(&hlcd, COM_POINT, Mask_Point, status<<SEG_Point);
HAL_LCD_UpdateDisplayRequest(&hlcd);
}
else if(icon==HIGH)
{
if(status)
{
Disp_SW(11);//显示H
Disp_GW(12);//显示I
}
else
{
Disp_SW(21);//清除显示HI
Disp_GW(22);
}
}
else if(icon==LOW)
{
if(status)
{
Disp_SW(13);//显示L
Disp_GW(14);//显示O
}
else
{
Disp_SW(21);//清除显示LO
Disp_GW(22);
}
}
}
/*******************************************************************************
* Function Name : Diaplay_Icon(ICON icon)
* Description : 显示图标
* Inputs : 电量等级:0/1/2/3
* Outputs : None
* Return : None
* Notes : None
*******************************************************************************/
void Display_Power(uint8_t grade)
{
switch(grade)
{
case 0:
HAL_LCD_Write(&hlcd, COM_Battery, Mask_Battery, 1<<SEG_Battery);
HAL_LCD_Write(&hlcd, COM_Battery1, Mask_Battery1, 0<<SEG_Battery1);
HAL_LCD_Write(&hlcd, COM_Battery2, Mask_Battery2, 0<<SEG_Battery2);
HAL_LCD_Write(&hlcd, COM_Battery3, Mask_Battery3, 0<<SEG_Battery3);
HAL_LCD_UpdateDisplayRequest(&hlcd);
break;
case 1:
HAL_LCD_Write(&hlcd, COM_Battery, Mask_Battery, 1<<SEG_Battery);
HAL_LCD_Write(&hlcd, COM_Battery1, Mask_Battery1, 1<<SEG_Battery1);
HAL_LCD_Write(&hlcd, COM_Battery2, Mask_Battery2, 0<<SEG_Battery2);
HAL_LCD_Write(&hlcd, COM_Battery3, Mask_Battery3, 0<<SEG_Battery3);
HAL_LCD_UpdateDisplayRequest(&hlcd);
break;
case 2:
HAL_LCD_Write(&hlcd, COM_Battery, Mask_Battery, 1<<SEG_Battery);
HAL_LCD_Write(&hlcd, COM_Battery1, Mask_Battery1, 1<<SEG_Battery1);
HAL_LCD_Write(&hlcd, COM_Battery2, Mask_Battery2, 1<<SEG_Battery2);
HAL_LCD_Write(&hlcd, COM_Battery3, Mask_Battery3, 0<<SEG_Battery3);
HAL_LCD_UpdateDisplayRequest(&hlcd);
break;
case 3:
HAL_LCD_Write(&hlcd, COM_Battery, Mask_Battery, 1<<SEG_Battery);
HAL_LCD_Write(&hlcd, COM_Battery1, Mask_Battery1, 1<<SEG_Battery1);
HAL_LCD_Write(&hlcd, COM_Battery2, Mask_Battery2, 1<<SEG_Battery2);
HAL_LCD_Write(&hlcd, COM_Battery3, Mask_Battery3, 1<<SEG_Battery3);
HAL_LCD_UpdateDisplayRequest(&hlcd);
break;
case 4:
HAL_LCD_Write(&hlcd, COM_Battery, Mask_Battery, 0<<SEG_Battery);
HAL_LCD_Write(&hlcd, COM_Battery1, Mask_Battery1, 0<<SEG_Battery1);
HAL_LCD_Write(&hlcd, COM_Battery2, Mask_Battery2, 0<<SEG_Battery2);
HAL_LCD_Write(&hlcd, COM_Battery3, Mask_Battery3, 0<<SEG_Battery3);
HAL_LCD_UpdateDisplayRequest(&hlcd);
break;
default:
break;
}
}
/*******************************************************************************
* Function Name : Clear_Temp_C(float temp)
* Description : 清除温度显示
* Inputs : None
* Outputs : None
* Return : None
* Notes : 传送非(0-10)的ID即显示为空,ID=10为"- - - -"
*******************************************************************************/
void Clear_Temp_C(void)
{
Disp_BW(20);
Disp_SW(20);
Disp_GW(20);
Disp_PW(20);
}
/*******************************************************************************
* Function Name : Display_All(void)
* Description : 显示全屏
* Inputs : None
* Outputs : None
* Return : None
* Notes : NONE
*******************************************************************************/
void Display_All(void)
{
Display_Power(3);//显示电量
Diaplay_Icon(Temp_C,1);//显示所有图标
Diaplay_Icon(Temp_F,1);
Diaplay_Icon(Buzzer,1);
Diaplay_Icon(Body,1);
Diaplay_Icon(WuTi,1);
Diaplay_Icon(BLUE,1);
Diaplay_Icon(**,1);
Disp_BW(8); //显示四个8
Disp_SW(8);
Disp_GW(8);
Disp_PW(8);
}
/* USER CODE END 1 */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/