B2: 定义为“选择”按键,在参数设置界面按下此按键, 切换选择参数项(电压的上限值、电压的下限值、 电压超过上限的提醒指示灯、 低于下限的提醒指示灯),被选择的参数项“高亮”显示。
B3:定义为“加”按键。
进行电压参数设置时,当前选择的电压参数加 0.3V。
进行 LED 提醒指示灯参数设置时, LED 指示灯序号加 1。
B4:定义为“减”按键。
进行电压参数设置时,当前选择的电压参数减 0.3V。
进行 LED 提醒指示灯参数设置时, LED 指示灯序号减 1。
备注:
- B2、 B3、 B4 仅在设置界面下有效。
- 参数设置功能需保护数据边界,电压参数可设置范围 0-3.3V, LED提醒指示灯可设置范围 LD1-LD8。
为了方便提交,这里将所有代码放在main.c中
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* © Copyright (c) 2021 STMicroelectronics.
* All rights reserved.
*
* 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
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "string.h"
#include "lcd.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
ADC_HandleTypeDef hadc2;
/* USER CODE BEGIN PV */
uint8_t KEY1_Flag = 0;
uint8_t KEY2_Flag = 0;
uint8_t KEY3_Flag = 0;
uint8_t KEY4_Flag = 0;
uint16_t adc;
double V;
char ADC_Text[50];
char lcd_Text[50];
double Max_Volt = 2.4;
double Min_Volt = 1.2;
char LD_Text[30];
uint16_t Upper = 1;
uint16_t Lower = 2;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_ADC2_Init(void);
uint8_t KeyScan(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
uint8_t KeyScan(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
{
if( HAL_GPIO_ReadPin(GPIOx, GPIO_Pin) == RESET){
while(HAL_GPIO_ReadPin(GPIOx, GPIO_Pin) == RESET);
return 2;
}
else
return 1;
}
double Get_ADC()
{
HAL_ADC_Start(&hadc2);
adc = HAL_ADC_GetValue(&hadc2);
return (adc*3.3/4096);
}
void Key34(double *Volt)
{
if(KeyScan(GPIOB,GPIO_PIN_2) == 2){
*Volt += 0.3;
if(*Volt >= 3.3)
*Volt = 3.3;
}
if(KeyScan(GPIOA,GPIO_PIN_0) == 2){
*Volt -= 0.3;
if(*Volt <= 0)
*Volt = 0;
}
}
void Key_LD(uint16_t *LD)
{
if(KeyScan(GPIOB,GPIO_PIN_2) == 2){
*LD += 1;
if(*LD >= 8)
*LD = 8;
}
if(KeyScan(GPIOA,GPIO_PIN_0) == 2){
*LD -= 1;
if(*LD <= 1)
*LD = 1;
}
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
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_ADC2_Init();
/* USER CODE BEGIN 2 */
LCD_Init();
LCD_Clear(White);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_All, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_All, GPIO_PIN_RESET);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
//数据显示页面
if(KEY1_Flag == 0){
LCD_SetBackColor(White);
LCD_SetTextColor(Blue);
LCD_DisplayStringLine(Line1,(uint8_t*)" Main ");
LCD_DisplayStringLine(Line5,(uint8_t*)" ");
LCD_DisplayStringLine(Line6,(uint8_t*)" ");
LCD_DisplayStringLine(Line7,(uint8_t*)" ");
V = Get_ADC();
sprintf(ADC_Text," Volt: %.2f ",V);
LCD_DisplayStringLine(Line3,(uint8_t*)ADC_Text);
if(V > Max_Volt){
LCD_DisplayStringLine(Line4,(uint8_t*)" Status:Upper ");
GPIOC->ODR = ~(0x0001 << (Upper + 7));
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_RESET);
HAL_Delay(200);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_All, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_All, GPIO_PIN_RESET);
}
if(V >= Min_Volt && V <= Max_Volt){
LCD_DisplayStringLine(Line4,(uint8_t*)" Status:Normal ");
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_All, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_All, GPIO_PIN_RESET);
}
if(V < Min_Volt){
LCD_DisplayStringLine(Line4,(uint8_t*)" Status:Lower ");
GPIOC->ODR = ~(0x0001 << (Lower + 7));
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_RESET);
HAL_Delay(200);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_All, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_All, GPIO_PIN_RESET);
}
}
//参数配置页面
else{
LCD_SetBackColor(White);
LCD_SetTextColor(Blue);
LCD_DisplayStringLine(Line1,(uint8_t*)" Setting ");
sprintf(lcd_Text," Max Volt:%.2fV ",Max_Volt);
if(KEY2_Flag == 0){
LCD_SetBackColor(Green);
LCD_DisplayStringLine(Line3,(uint8_t*)lcd_Text);
LCD_SetBackColor(White);
Key34(&Max_Volt);
}
else{
LCD_DisplayStringLine(Line3,(uint8_t*)lcd_Text);
}
sprintf(lcd_Text," Min Volt:%.2fV ",Min_Volt);
if(KEY2_Flag == 1){
LCD_SetBackColor(Green);
LCD_DisplayStringLine(Line4,(uint8_t*)lcd_Text);
LCD_SetBackColor(White);
Key34(&Min_Volt);
}
else{
LCD_DisplayStringLine(Line4,(uint8_t*)lcd_Text);
}
sprintf(LD_Text," Upper:LD%d ",Upper);
if(KEY2_Flag == 2){
LCD_SetBackColor(Green);
LCD_DisplayStringLine(Line5,(uint8_t*)LD_Text);
LCD_SetBackColor(White);
Key_LD(&Upper);
}
else{
LCD_DisplayStringLine(Line5,(uint8_t*)LD_Text);
}
sprintf(LD_Text," Lower:LD%d ",Lower);
if(KEY2_Flag == 3){
LCD_SetBackColor(Green);
LCD_DisplayStringLine(Line6,(uint8_t*)LD_Text);
LCD_SetBackColor(White);
Key_LD(&Lower);
}
else{
LCD_DisplayStringLine(Line6,(uint8_t*)LD_Text);
}
}
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {
0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {
0};
RCC_PeriphCLKInitTypeDef PeriphClkInit = {
0};
/** Configure the main internal regulator output voltage
*/
HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the CPU, AHB and APB busses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV3;
RCC_OscInitStruct.PLL.PLLN = 20;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB busses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
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_3) != HAL_OK)
{
Error_Handler();
}
/** Initializes the peripherals clocks
*/
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC12;
PeriphClkInit.Adc12ClockSelection = RCC_ADC12CLKSOURCE_SYSCLK;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief ADC2 Initialization Function
* @param None
* @retval None
*/
static void MX_ADC2_Init(void)
{
/* USER CODE BEGIN ADC2_Init 0 */
/* USER CODE END ADC2_Init 0 */
ADC_ChannelConfTypeDef sConfig = {
0};
/* USER CODE BEGIN ADC2_Init 1 */
/* USER CODE END ADC2_Init 1 */
/** Common config
*/
hadc2.Instance = ADC2;
hadc2.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
hadc2.Init.Resolution = ADC_RESOLUTION_12B;
hadc2.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc2.Init.GainCompensation = 0;
hadc2.Init.ScanConvMode = ADC_SCAN_DISABLE;
hadc2.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
hadc2.Init.LowPowerAutoWait = DISABLE;
hadc2.Init.ContinuousConvMode = DISABLE;
hadc2.Init.NbrOfConversion = 1;
hadc2.Init.DiscontinuousConvMode = DISABLE;
hadc2.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc2.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc2.Init.DMAContinuousRequests = DISABLE;
hadc2.Init.Overrun = ADC_OVR_DATA_PRESERVED;
hadc2.Init.OversamplingMode = DISABLE;
if (HAL_ADC_Init(&hadc2) != HAL_OK)
{
Error_Handler();
}
/** Configure Regular Channel
*/
sConfig.Channel = ADC_CHANNEL_15;
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_2CYCLES_5;
sConfig.SingleDiff = ADC_SINGLE_ENDED;
sConfig.OffsetNumber = ADC_OFFSET_NONE;
sConfig.Offset = 0;
if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN ADC2_Init 2 */
/* USER CODE END ADC2_Init 2 */
}
/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {
0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOF_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15|GPIO_PIN_8
|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12, GPIO_PIN_RESET);
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_RESET);
/*Configure GPIO pins : PC13 PC14 PC15 PC8
PC9 PC10 PC11 PC12 */
GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15|GPIO_PIN_8
|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
/*Configure GPIO pin : PA0 */
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/*Configure GPIO pins : PB0 PB1 */
GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/*Configure GPIO pin : PB2 */
GPIO_InitStruct.Pin = GPIO_PIN_2;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/*Configure GPIO pin : PD2 */
GPIO_InitStruct.Pin = GPIO_PIN_2;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
/* EXTI interrupt init*/
HAL_NVIC_SetPriority(EXTI0_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(EXTI0_IRQn);
HAL_NVIC_SetPriority(EXTI1_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(EXTI1_IRQn);
}
/* USER CODE BEGIN 4 */
/**
* @brief This function handles EXTI line0 interrupt.
*/
void EXTI0_IRQHandler(void)
{
/* USER CODE BEGIN EXTI0_IRQn 0 */
if(__HAL_GPIO_EXTI_GET_IT(GPIO_PIN_0) != RESET ){
KEY1_Flag = !KEY1_Flag;
__HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_0);
}
/* USER CODE END EXTI0_IRQn 0 */
/* USER CODE BEGIN EXTI0_IRQn 1 */
/* USER CODE END EXTI0_IRQn 1 */
}
/**
* @brief This function handles EXTI line1 interrupt.
*/
void EXTI1_IRQHandler(void)
{
/* USER CODE BEGIN EXTI1_IRQn 0 */
if(__HAL_GPIO_EXTI_GET_IT(GPIO_PIN_1) != RESET ){
if(KEY1_Flag == 1){
KEY2_Flag += 1;
if(KEY2_Flag == 4)
KEY2_Flag = 0;
}
__HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_1);
}
/* USER CODE END EXTI1_IRQn 0 */
/* USER CODE BEGIN EXTI1_IRQn 1 */
/* USER CODE END EXTI1_IRQn 1 */
}
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
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,
tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
一直有一个疑惑,板子所带的LCD屏幕经常下方出现黑线,具体产生原因还未找到,若有大佬知道原因,还请指明,在此表感谢。
笔者能力有限,文中若有不足,还请多多指教!