飞道的博客

第九届蓝桥杯嵌入式省赛(STM32G4及HAL库)

962人阅读  评论(0)

题目要求

硬件框图

功能描述

LCD显示

按键功能

PWM输出和LED显示

定时时间存储

功能实现

为了方便提交,这里将所有代码放在main.c中

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2021 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
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "string.h"
#include "lcd.h"
#include "i2c - hal.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
#define Key_Short_OK 1
#define Key_Long_OK 2
#define Key_Err 3
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
TIM_HandleTypeDef htim2;
TIM_HandleTypeDef htim3;
TIM_HandleTypeDef htim4;

/* USER CODE BEGIN PV */
uint8_t Zoom = 1;
uint16_t Times = 0;
uint16_t TIME_Key = 0;
char Lcd_temp[30];
uint8_t Key_Flag = 0;
uint8_t Key_Val_2;
uint8_t Key_Val_3;
uint8_t Key_Val_4;
uint8_t EE = 1;
uint8_t Seconds = 0;
uint8_t Minutes= 0;
uint8_t Hours = 0;

uint8_t Key4_Flag = 0;
uint8_t Key4_Flags = 0;
uint8_t Key2_Flag = 0;

uint8_t Sate = 0;
uint8_t temp = 0;

uint8_t LED_Flag = 0;
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_TIM2_Init(void);
static void MX_TIM3_Init(void);
static void MX_TIM4_Init(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
uint8_t Key_Scan(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
{
   


	if(HAL_GPIO_ReadPin(GPIOx, GPIO_Pin) == RESET){
   

		TIME_Key = 0;
		
		while(HAL_GPIO_ReadPin(GPIOx, GPIO_Pin) == RESET);
		
		Key_Flag = 1;
		
		
	}
	
	if(TIME_Key <= 8 && Key_Flag == 1){
   
			Key_Flag = 0;

			return Key_Short_OK;
		}
		
		if(TIME_Key > 8 && Key_Flag == 1){
   
			Key_Flag = 0;
			return Key_Long_OK;
		}
	
	return Key_Err;
	
}

void LED_Start()
{
   
	
	
		if(Sate == 2){
   
			
	if(LED_Flag < 5){
   
	HAL_GPIO_WritePin(GPIOC, GPIO_PIN_All, GPIO_PIN_SET);
	HAL_GPIO_WritePin(GPIOC, GPIO_PIN_8, GPIO_PIN_RESET);
	HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_SET);
	HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_RESET);
	}
	if(LED_Flag >= 5 ){
   
	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_2, GPIO_PIN_RESET);
	}
	if(LED_Flag >= 10){
   
	LED_Flag = 0;
	}
		}
	
}

void LED_Stop()
{
   
	

	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_2, GPIO_PIN_RESET);
	

	
}



uint8_t Read_M2402(unsigned char address)
{
   
	uint8_t val;
	
		I2CStart();
		I2CSendByte(0xa0);
		I2CWaitAck();
	
		I2CSendByte(address);
		I2CWaitAck();
	
		I2CStart();
		I2CSendByte(0xa1);
		I2CWaitAck();
		val = I2CReceiveByte();
		I2CWaitAck();
		I2CStop();
	
	return val;
}

void Write_M2402(unsigned char address, unsigned char info)
{
   
	
		I2CStart();
		I2CSendByte(0xa0);
		I2CWaitAck();
	
		I2CSendByte(address);
		I2CWaitAck();
	
		I2CSendByte(info);
		I2CWaitAck();
	
		I2CStop();
	
}

void Save_Time()
{
   
	
		Write_M2402(Zoom *10 + 0x01, Hours);
		HAL_Delay(5);
		Write_M2402(Zoom *10 + 0x02, Minutes);
		HAL_Delay(5);
		Write_M2402(Zoom *10 + 0x03, Seconds);
	
}

void Restar_Time()
{
   
	Hours =  Read_M2402(10 + 0x01);
	HAL_Delay(5);
	Minutes =  Read_M2402(10 + 0x02);
	HAL_Delay(5);
	Seconds =  Read_M2402(10 + 0x03);
	HAL_Delay(5);
	Times = 3600 * Hours + 60 * Minutes + Seconds;
}

void Key4()
{
   
	
	
	if(Key_Val_4 == Key_Short_OK){
   
			Key4_Flag = !Key4_Flag;
	}
	
	if(Key_Val_4 == Key_Long_OK){
   
			HAL_TIM_Base_Stop_IT(&htim4);
		HAL_TIM_PWM_Stop_IT(&htim3,TIM_CHANNEL_1);
		LED_Stop();
			Key4_Flags = 1;
			Sate = 0;
	}

	if(Key4_Flag != temp){
   
	
		
	
	if(Key4_Flag == 1){
   
		//__HAL_TIM_CLEAR_FLAG(&htim4,TIM_FLAG_UPDATE);
		HAL_TIM_Base_Start_IT(&htim4);
		HAL_TIM_PWM_Start_IT(&htim3,TIM_CHANNEL_1);

		Sate = 2;
		Key4_Flags = 0;
	

	}
	if(Key4_Flag == 0 && Key4_Flags == 0){
   
		//__HAL_TIM_CLEAR_FLAG(&htim4,TIM_FLAG_UPDATE);
		HAL_TIM_Base_Stop_IT(&htim4);
		HAL_TIM_PWM_Stop_IT(&htim3,TIM_CHANNEL_1);
		
		LED_Stop();
		
		
		Sate = 3;
		
		
	}
	
}
	temp = Key4_Flag;


}


void Key2()
{
   
	if(Key_Val_2 == Key_Short_OK){
   
			Key2_Flag++;
		if(Key2_Flag > 4){
   
			Key2_Flag = 0;
		}
	}
	
	//进入设置状态
	if(Key2_Flag == 1){
   
		Sate = 1;
		
	}
	//选中时
	if(Key2_Flag == 2){
   
		
		LCD_SetTextColor(Red);
		
		if(Key_Val_3 == Key_Short_OK){
   
			
			Hours += 1; 
		}
		if(Key_Val_3 == Key_Long_OK){
   
			
			Hours += 5; 
		}

	}else
		{
   
		LCD_SetTextColor(Blue);
	}
		LCD_DisplayChar(Line5,210, Hours / 10 + 0x30); 
		LCD_DisplayChar(Line5,195, Hours % 10 + 0x30); 


	//选中分
	if(Key2_Flag == 3){
   
			LCD_SetTextColor(Red);
		
			if(Key_Val_3 == Key_Short_OK){
   
			
			Minutes += 1; 
		}
			if(Key_Val_3 == Key_Long_OK){
   
			
			Minutes += 5; 
		}
	}else
		{
   
		LCD_SetTextColor(Blue);
	}
		LCD_DisplayChar(Line5,165, Minutes / 10 + 0x30);
		LCD_DisplayChar(Line5,150, Minutes % 10 + 0x30); 
		

	//选中秒
	if(Key2_Flag == 4){
   
			LCD_SetTextColor(Red);
		
		if(Key_Val_3 == Key_Short_OK){
   
			
			Seconds += 1; 
		}
		if(Key_Val_3 == Key_Long_OK){
   
			
			Seconds += 5; 
		}
	}else
		{
   
		LCD_SetTextColor(Blue);
	}
		LCD_DisplayChar(Line5,120, Seconds / 10 + 0x30);
		LCD_DisplayChar(Line5,105, Seconds % 10 + 0x30);
		LCD_SetTextColor(Blue);
	
		if(Key_Val_2 == Key_Long_OK){
   
			Save_Time();
			Sate = 0;
			
			//存储当前位置
			
			
		}
		
		
		if(Seconds >= 60){
   				
				Seconds = 0;
		}
		
		if(Minutes >= 60){
   				
				Minutes = 0;
		}
		
		if(Hours >= 4){
   
				Times = 0;
				Seconds = 0;
				Minutes = 0;
		}
		
		if(Sate == 1)
		Times = 3600 * Hours + 60 * Minutes + Seconds;
}
	


void SateMode()
{
   
	if(Sate == 0){
   
		
		LCD_DisplayStringLine(Line7,(uint8_t*)"       Standby   ");
		
	}
	
		if(Sate == 1){
   
			
			
		
		LCD_DisplayStringLine(Line7,(uint8_t*)"       Setting   ");
		
	}
		
		if(Sate == 2){
   
		
		LCD_DisplayStringLine(Line7,(uint8_t*)"       Running   ");
		
	}
		
	if(Sate == 3){
   
		
		LCD_DisplayStringLine(Line7,(uint8_t*)"       Pause   ");
		
	}
	
	
	
}




/* 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_TIM2_Init();
  MX_TIM3_Init();
	MX_TIM4_Init();
  /* USER CODE BEGIN 2 */
	LCD_Init();
	LCD_Clear(White);
	LCD_SetBackColor(White);
	
	HAL_TIM_Base_Start_IT(&htim2);
	
	//HAL_TIM_Base_Start_IT(&htim4);
	I2CInit();
	
	Restar_Time();
	
	LED_Stop();
	
  /* USER CODE END 2 */
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
   
    /* USER CODE END WHILE */
		
    /* USER CODE BEGIN 3 */
		

		Key_Val_2 = Key_Scan(GPIOB, GPIO_PIN_1);
		Key_Val_3 = Key_Scan(GPIOB, GPIO_PIN_2);
		Key_Val_4 = Key_Scan(GPIOA, GPIO_PIN_0);
		
		LCD_SetTextColor(Blue);
		sprintf( Lcd_temp, " NO %2d  ", Zoom);
		LCD_DisplayStringLine(Line1,(uint8_t*) Lcd_temp);

		
		
		Key2();
		
		
		
		
		Seconds = Times %3600 % 60;
		Minutes = Times %3600 / 60;
		Hours = Times / 3600;
		
		
		
		
		
		if(Sate != 1){
   
		Key4();
		
		

		//显示时间
		
		LCD_SetTextColor(Blue);
		LCD_DisplayChar(Line5,210, Hours / 10 + 0x30); 
		LCD_DisplayChar(Line5,195, Hours % 10 + 0x30);  
		LCD_DisplayChar(Line5,180, ':');	
		LCD_DisplayChar(Line5,165, Minutes / 10 + 0x30);
		LCD_DisplayChar(Line5,150, Minutes % 10 + 0x30); 
		LCD_DisplayChar(Line5,135, ':');
		LCD_DisplayChar(Line5,120, Seconds / 10 + 0x30);
		LCD_DisplayChar(Line5,105, Seconds % 10 + 0x30);
		//LCD_DisplayChar(Line4,135, ':');
		
		}
		//当前状态
		SateMode();
		
		LED_Start();
		

		

		
  }
  /* 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 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();
  }
}

/**
  * @brief TIM2 Initialization Function
  * @param None
  * @retval None
  */
static void MX_TIM2_Init(void)
{
   

  /* USER CODE BEGIN TIM2_Init 0 */

  /* USER CODE END TIM2_Init 0 */

  TIM_ClockConfigTypeDef sClockSourceConfig = {
   0};
  TIM_MasterConfigTypeDef sMasterConfig = {
   0};

  /* USER CODE BEGIN TIM2_Init 1 */

  /* USER CODE END TIM2_Init 1 */
  htim2.Instance = TIM2;
  htim2.Init.Prescaler = 79;
  htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim2.Init.Period = 999;
  htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
  {
   
    Error_Handler();
  }
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
  {
   
    Error_Handler();
  }
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
  {
   
    Error_Handler();
  }
  /* USER CODE BEGIN TIM4_Init 2 */

  /* USER CODE END TIM4_Init 2 */

}

/**
  * @brief TIM3 Initialization Function
  * @param None
  * @retval None
  */
static void MX_TIM3_Init(void)
{
   

  /* USER CODE BEGIN TIM3_Init 0 */

  /* USER CODE END TIM3_Init 0 */

  TIM_MasterConfigTypeDef sMasterConfig = {
   0};
  TIM_OC_InitTypeDef sConfigOC = {
   0};

  /* USER CODE BEGIN TIM3_Init 1 */

  /* USER CODE END TIM3_Init 1 */
  htim3.Instance = TIM3;
  htim3.Init.Prescaler = 79;
  htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim3.Init.Period = 999;
  htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_PWM_Init(&htim3) != HAL_OK)
  {
   
    Error_Handler();
  }
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
  {
   
    Error_Handler();
  }
  sConfigOC.OCMode = TIM_OCMODE_PWM1;
  sConfigOC.Pulse = 800;
  sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
  {
   
    Error_Handler();
  }
  /* USER CODE BEGIN TIM3_Init 2 */

  /* USER CODE END TIM3_Init 2 */
  HAL_TIM_MspPostInit(&htim3);

}

/**
  * @brief TIM4 Initialization Function
  * @param None
  * @retval None
  */
static void MX_TIM4_Init(void)
{
   

  /* USER CODE BEGIN TIM4_Init 0 */

  /* USER CODE END TIM4_Init 0 */

  TIM_ClockConfigTypeDef sClockSourceConfig = {
   0};
  TIM_MasterConfigTypeDef sMasterConfig = {
   0};

  /* USER CODE BEGIN TIM4_Init 1 */

  /* USER CODE END TIM4_Init 1 */
  htim4.Instance = TIM4;
  htim4.Init.Prescaler = 79;
  htim4.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim4.Init.Period = 999;
  htim4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim4.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_Base_Init(&htim4) != HAL_OK)
  {
   
    Error_Handler();
  }
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  if (HAL_TIM_ConfigClockSource(&htim4, &sClockSourceConfig) != HAL_OK)
  {
   
    Error_Handler();
  }
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim4, &sMasterConfig) != HAL_OK)
  {
   
    Error_Handler();
  }
  /* USER CODE BEGIN TIM4_Init 2 */

  /* USER CODE END TIM4_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 pin Output Level */
  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6|GPIO_PIN_7, 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 pin : PB0 */
  GPIO_InitStruct.Pin = GPIO_PIN_0;
  GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

  /*Configure GPIO pins : PB1 PB2 */
  GPIO_InitStruct.Pin = GPIO_PIN_1|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);

  /*Configure GPIO pins : PB6 PB7 */
  GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

  /* EXTI interrupt init*/
  HAL_NVIC_SetPriority(EXTI0_IRQn, 8, 0);
  HAL_NVIC_EnableIRQ(EXTI0_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 ){
   
		
		Zoom++;
		if(Zoom > 5){
   
			Zoom = 1;
		}
		HAL_Delay(5);
		
		 __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 TIM2 global interrupt.
  */
void TIM2_IRQHandler(void)
{
   
  /* USER CODE BEGIN TIM2_IRQn 0 */

  /* USER CODE END TIM2_IRQn 0 */
  HAL_TIM_IRQHandler(&htim2);
  /* USER CODE BEGIN TIM2_IRQn 1 */

  /* USER CODE END TIM2_IRQn 1 */
}


/**
  * @brief This function handles TIM4 global interrupt.
  */
void TIM4_IRQHandler(void)
{
   
  /* USER CODE BEGIN TIM4_IRQn 0 */

  /* USER CODE END TIM4_IRQn 0 */
  HAL_TIM_IRQHandler(&htim4);
  /* USER CODE BEGIN TIM4_IRQn 1 */

  /* USER CODE END TIM4_IRQn 1 */
}

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
   
static uint16_t TIM2_Cnt1 = 0;
static uint16_t TIM4_Cnt1 = 0;
	
	
		if(htim->Instance == TIM2){
   
		TIM2_Cnt1++;
		
		if(TIM2_Cnt1 >= 100){
   
			 TIM2_Cnt1 = 0;
			
			TIME_Key++;
			LED_Flag ++;
		
			
		}
		
	}
	
	
	
	if(htim->Instance == TIM4){
   
		TIM4_Cnt1++;
		
		if(TIM4_Cnt1 >= 1000){
   
			 TIM4_Cnt1 = 0;
			
			Times++;
			
		}
		
	}
	

	
}



/* 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****/


最后

笔者才疏学浅,文中颇有不足,其中按键判断时长是依据松开按键起,这里做的不是很好。若还有其它不足,请多多指教!


转载:https://blog.csdn.net/qq_45628620/article/details/115751503
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场