为了方便调试,在Data界面里面多显示了Vmax 和Vmin(现在注释掉了,调试时可以打开)
必要的注释在代码里有
bsp_led_key.c
本程序唯一分离出的子函数,有按键扫描和显示LED的作用
#include "bsp_led_key.h"
void LED_Disp(uint8_t LED)
{
HAL_GPIO_WritePin(GPIOC,0xFF00,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC,LED << 8,GPIO_PIN_RESET);
HAL_GPIO_WritePin(LE_GPIO_Port,LE_Pin,GPIO_PIN_SET);
HAL_GPIO_WritePin(LE_GPIO_Port,LE_Pin,GPIO_PIN_RESET);
}
uint8_t KEY_Scan(void)
{
if(HAL_GPIO_ReadPin(B1_GPIO_Port,B1_Pin) == GPIO_PIN_RESET)
return 1;
if(HAL_GPIO_ReadPin(B2_GPIO_Port,B2_Pin) == GPIO_PIN_RESET)
return 2;
if(HAL_GPIO_ReadPin(B3_GPIO_Port,B3_Pin) == GPIO_PIN_RESET)
return 3;
if(HAL_GPIO_ReadPin(B4_GPIO_Port,B4_Pin) == GPIO_PIN_RESET)
return 4;
return 0;
}
主函数部分(时间原因大部分子函数没有分出来)
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2022 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "adc.h"
#include "tim.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "lcd.h"
#include "stdio.h"
#include "string.h"
#include "bsp_led_key.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
typedef struct
{
float Vmax;
float Vmin;
} V_Number_Storage_Type;
V_Number_Storage_Type V_Number_Storage[2];//0存新的,1存原来的
/* 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 ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
//KEY专用变量
uint8_t KEY_VAL;
uint8_t KEY_OLD;
uint8_t KEY_DOWN;
//LCD专用变量
_Bool face = 0;
//LED专用变量
u8 ucLED = 0x00;
//题目需求变量.
float Vmax_Number = 3.0;
float Vmin_Number = 1.0;
int8_t Tim_number = 0;
char LCD_buf[20];
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void KEY_Proc(void);
void LCD_Proc(void);
void LED_Proc(void);
/* 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();
MX_TIM1_Init();
/* USER CODE BEGIN 2 */
LCD_Init();
LCD_Clear(Black);
LCD_SetBackColor(Black);
LCD_SetTextColor(White);
LED_Disp(0X00);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
KEY_Proc();
LCD_Proc();
LED_Disp(ucLED);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV1;
RCC_OscInitStruct.PLL.PLLN = 10;
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 buses 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_2) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
uint16_t getADC(void)
{
uint16_t adc = 0;
HAL_ADC_Start(&hadc2);
adc = HAL_ADC_GetValue(&hadc2);
return adc;
}
void KEY_Proc(void)
{
KEY_VAL = KEY_Scan();
KEY_DOWN = KEY_VAL&(KEY_VAL^KEY_OLD);
KEY_OLD = KEY_VAL;
switch(KEY_DOWN)
{
case 1:
face ^= 1;
LCD_Clear(Black);
if(face == 0)
{
if(V_Number_Storage[0].Vmax - V_Number_Storage[0].Vmin >= 1) //判断参数是否合法,合法就进入数据界面
{ //不合法,使用设置参数前的数据,再进入数据界面
Vmax_Number = V_Number_Storage[0].Vmax;
Vmin_Number = V_Number_Storage[0].Vmin;
}
else if(V_Number_Storage[0].Vmax - V_Number_Storage[0].Vmin < 1)//如果设置的参数不合法
{
Vmax_Number = V_Number_Storage[1].Vmax; //number使用翻页设置前的数据
Vmin_Number = V_Number_Storage[1].Vmin;
}
}
break;
case 2:
if(face == 1)
{
V_Number_Storage[0].Vmax = Vmax_Number;
V_Number_Storage[0].Vmin = Vmin_Number;
Vmax_Number = Vmax_Number + 0.1;
if(Vmax_Number > 3.3)
{
Vmax_Number = 0.0;
}
V_Number_Storage[0].Vmax = Vmax_Number; //存储设置数据,但后续要判断是否合法
if(V_Number_Storage[0].Vmax - V_Number_Storage[0].Vmin < 1)//如果设置的参数不合法
{
ucLED |= 0x02;
LED_Disp(ucLED);
}
else
{
ucLED &= ~0x02;
LED_Disp(ucLED); //熄灭小灯LD2
}
}
break;
case 3:
if(face == 1)
{
Vmin_Number = Vmin_Number + 0.1;
if(Vmin_Number > 3.3)
{
Vmin_Number = 0.0;
}
V_Number_Storage[0].Vmin = Vmin_Number;
if(V_Number_Storage[0].Vmax - V_Number_Storage[0].Vmin < 1)//如果设置的参数不合法
{
ucLED |= 0x02;
LED_Disp(ucLED);
}
else
{
ucLED &= ~0x02;
LED_Disp(ucLED); //熄灭小灯LD2
}
}
break;
}
}
void LED_Proc()
{
LED_Disp(ucLED);
}
void Start_TIM(void)
{
if((getADC()*3.3/4096)>=Vmin_Number)
{
Tim_number = 0;
HAL_TIM_Base_Start_IT(&htim1);
}
}
void LCD_Proc(void)
{
if(face == 0)
{
V_Number_Storage[1].Vmin = Vmin_Number; // 每次先存储正确的参数
V_Number_Storage[1].Vmax = Vmax_Number;
sprintf(LCD_buf," Data ");
LCD_DisplayStringLine(Line0,(uint8_t*)LCD_buf);
sprintf(LCD_buf," V:%.2fV ",getADC()*3.3/4096);
LCD_DisplayStringLine(Line2,(uint8_t*)LCD_buf);
sprintf(LCD_buf," T:%d ",Tim_number);
LCD_DisplayStringLine(Line3,(uint8_t*)LCD_buf);
// sprintf(LCD_buf," Vmax:%.1fV ",Vmax_Number);
// LCD_DisplayStringLine(Line5,(uint8_t*)LCD_buf);
// sprintf(LCD_buf," Vmin:%.1fV ",Vmin_Number); //调试专用
// LCD_DisplayStringLine(Line6,(uint8_t*)LCD_buf);
if((getADC()*3.3/4096)<Vmin_Number)
Start_TIM();
ucLED &= ~0x02;
}
else if(face == 1)
{
//number 会影响时间的计算
sprintf(LCD_buf," Para ");
LCD_DisplayStringLine(Line0,(uint8_t*)LCD_buf);
sprintf(LCD_buf," Vmax:%.1fV ",Vmax_Number);
LCD_DisplayStringLine(Line2,(uint8_t*)LCD_buf);
sprintf(LCD_buf," Vmin:%.1fV ",Vmin_Number);
LCD_DisplayStringLine(Line3,(uint8_t*)LCD_buf);
}
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef* htim)
{
if(htim ->Instance == TIM1)
{
if((getADC()*3.3/4096) <= Vmax_Number)
{
Tim_number++;
ucLED |= 0x01;
LED_Disp(ucLED);
}
else
{
HAL_TIM_Base_Stop_IT(&htim1);
ucLED &= ~0x01;
LED_Disp(ucLED);
}
}
}
/* 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 */
__disable_irq();
while (1)
{
}
/* 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,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
CUBMX配置如下:
1.时钟树
2.TIM1 (NVIC设置成update)
3.ADC2(PB15)
4.GPIO