【STM32G431RBTx】备战蓝桥杯嵌入式→省赛试题→第八届

前言

学完了基本模块,就要开始刷题了,先把省赛过了再考虑国赛的事情吧。

一、题目

在这里插入图片描述
在这里插入图片描述

二、模块初始化

这里就简单描述一下模块的配置方法,具体方法到基本模块中有描述。
1.LCD这里不用配置,直接使用提供的资源包就行
2.KEY, 四个按键IO口都要配置,分别是PB0, PB1,PB2,PA0依次是B1,B2,B3,B4不要弄错了
3.PWM:这里使用TIM16的CH1和TIM17的CH1作为发生通道,TIM16→PSC:800-1,ARR:100-1,Pulse:80
TIM17→PSC:400-1,ARR:100-1,Pulse:60
4.LED:开启PC8,9,10,11,12,13,14,15输出模式就行了
5.RTC:详见RTC模块部分,配置方法一样的
6.定时器:由于题目的逻辑比较复杂(笔者个人认为),所以除了开TIM16,TIM17外还要开TIM2,3,4,6,7,8,15,而且中断要全部打开。

三、代码实现

1.void DisposeKey(void); //按键响应函数

void DisposeKey(void)
{

		if(key[0].single_flag)
		{
			if(Dispfloor - 1 != 0)
			{
				LED |= 0x01;
				LED_Disp(LED);
				floor_want[0] = 1;
				__HAL_TIM_SetCounter(&htim4, 0);
				last_key_time = 0;
				HAL_TIM_Base_Start_IT(&htim4);
			}
			key[0].single_flag = 0;
		}
		if(key[1].single_flag)
		{
			if(Dispfloor - 1 != 1)
			{
				LED |= 0x02;
				LED_Disp(LED);
				floor_want[1] = 1;
				LCD_DisplayStringLine(Line5, "     ");
				__HAL_TIM_SetCounter(&htim4, 0);
				last_key_time = 0;
				HAL_TIM_Base_Start_IT(&htim4);
			}
			key[1].single_flag = 0;
		}
		if(key[2].single_flag)
		{
			if(Dispfloor - 1 != 2)
			{
				LED |= 0x04;
				LED_Disp(LED);
				floor_want[2] = 1;
				__HAL_TIM_SetCounter(&htim4, 0);
				last_key_time = 0;
				HAL_TIM_Base_Start_IT(&htim4);
			}
			key[2].single_flag = 0;
		}
		if(key[3].single_flag)
		{
			if(Dispfloor - 1 != 3)
			{
				LED |= 0x08;
				LED_Disp(LED);
				floor_want[3] = 1;
				__HAL_TIM_SetCounter(&htim4, 0);
				last_key_time = 0;
				HAL_TIM_Base_Start_IT(&htim4);

			}
			key[3].single_flag = 0;
		}
}

2.void Gate_Control(unsigned char command); //控制门开关

void Gate_Control(unsigned char command)
{
	if(command == 0)
	{
		HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
		GateCommand = 0;
	}
	else
	{
		HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);
		GateCommand = 1;
	}
}

3.void Gate_Run(unsigned char GateCommand); //执行门开关

void Gate_Run(unsigned char GateCommand)
{
	if(GateCommand == 0)
	{
		__HAL_TIM_SetCompare(&htim17, TIM_CHANNEL_1, 60);
		HAL_TIM_PWM_Start(&htim17, TIM_CHANNEL_1);
	}
	if(GateCommand == 1)
	{
		__HAL_TIM_SetCompare(&htim17, TIM_CHANNEL_1, 50);
		HAL_TIM_PWM_Start(&htim17, TIM_CHANNEL_1);
	}
}

4.void UporDown_Control(unsigned char command); //控制上下

void UporDown_Control(unsigned char command)
{
	if(command)
	{
		HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);
		FloorCommand = 0;
	}
	else
	{
		HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);
		FloorCommand = 1;
	}
}

5.void UporDown_Run(unsigned char FloorCommand); //上下执行

void UporDown_Run(unsigned char FloorCommand)
{
	if(FloorCommand == 0)
	{
		__HAL_TIM_SetCompare(&htim16, TIM_CHANNEL_1, 60);
		HAL_TIM_PWM_Start(&htim16, TIM_CHANNEL_1);
	}
	else
	{
		__HAL_TIM_SetCompare(&htim16, TIM_CHANNEL_1, 80);
		HAL_TIM_PWM_Start(&htim16, TIM_CHANNEL_1);		
	}
}

6.void Judge_upordown(void); //上下行判断

void Judge_upordown(void)
{
	for(unsigned char i = 0; i < Dispfloor - 1; i++)
	{
		if(floor_want[i] == 1)
		{
			isupdown = DOWN;
		}
	}
	for(unsigned char i = 3; i > Dispfloor - 1; i--)
	{
		if(floor_want[i] == 1)
		{
			isupdown = UP;
		}
	}
}

7.unsigned char Judge_LowestFloor(void); //判断目标楼层中的最低层

unsigned char Judge_LowestFloor(void)
{
	unsigned char i = 1;
	for(i = 1; i < 4; i++)
	{
		if(floor_want[i - 1] == 1)
		{
			break;
		}
	}
	return i;
}

8.unsigned char Judge_HighestFloor(void); //判断目标楼层中的最高层

unsigned char Judge_HighestFloor(void)
{
	unsigned char i = 4;
	for(i = 4; i > 1; i--)
	{
		if(floor_want[i - 1] == 1)
		{
			break;
		}
	}
	return i;
}

9.unsigned char judge_NextUpFloor(void); //判断上升的下一目标

unsigned char judge_NextUpFloor(void)
{
	unsigned char i = Dispfloor + 1;
	for(i = Dispfloor + 1; i < 4; i++)
	{
		if(floor_want[i - 1] == 1)
		{
			break;
		}
	}
	return i;
}

10.unsigned char judge_NextDownFloor(void); //判断下降的下一目标

unsigned char judge_NextDownFloor(void)
{
	unsigned char i = Dispfloor - 1;
	for(i = Dispfloor - 1; i >= 1; i--)
	{
		if(floor_want[i - 1] == 1)
		{
			break;
		}
	}
	return i;
}

11.interrupt.h以及interrupt.c的全部代码部分

interrupt.h:

#ifndef __INTERRUPT_H__
#define __INTERRUPT_H__

#include "main.h"
#include "stdbool.h"

struct keys
{
	unsigned char judge_sta;
	bool key_sta;
	bool single_flag;
	unsigned int key_time;
	bool long_flag;
};



#endif

interrupt.c:

#include "interrupt.h"
#include "lcd.h"
#include "tim.h"
#include "stdio.h"
#include "led.h"

struct keys key[4] = {0, 0, 0, 0, 0};
extern unsigned int last_key_time;
unsigned char should_close_flag = 0;
extern unsigned int closeGate_time;
extern unsigned char should_up_flag;
extern unsigned int upfloor_time;
extern unsigned char upordownRunningflag;
extern unsigned char UpFloorNum;
extern unsigned char Dispfloor;
extern unsigned char NextUpFloor;
extern char text[30];
extern unsigned char isupdown;
extern unsigned char should_open_flag;
extern unsigned char openordownTypeflag;
extern unsigned int openGate_time;
extern unsigned char HighestFloor;
extern unsigned char LowestFloor;
extern unsigned char should_wait_flag;
extern unsigned char waitGate_time;
extern unsigned char LED;
extern unsigned char should_lcdflash_flag;
extern unsigned int lcdflash_time;
extern unsigned char should_lcdflash_flag;
extern unsigned int lcdflash_time;
extern unsigned char floor_want[4];
extern unsigned char should_down_flag;
extern unsigned int downfloor_time;
extern unsigned char DownFloorNum;
extern unsigned char NextDownFloor;
extern unsigned char openorcloseRunningflag;
extern unsigned char waiting;
extern unsigned int ledflash_time;
extern unsigned char index;
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
	if(htim->Instance == TIM3)
	{
		if((upordownRunningflag || openorcloseRunningflag || waiting) == 0)
		{
			key[0].key_sta = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_0);
			key[1].key_sta = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_1);
			key[2].key_sta = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_2);
			key[3].key_sta = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0);
			for(unsigned char i = 0; i < 4; i++)
			{
				switch(key[i].judge_sta)
				{
					case 0:
					{
						if(key[i].key_sta == 0)
						{
							key[i].judge_sta = 1;
							key[i].key_time = 0;
						}
						break;
					}
					case 1:
					{
						if(key[i].key_sta == 0)
						{
							key[i].judge_sta = 2;
						}
						else
						{
							key[i].judge_sta = 0;
						}
						break;
					}
					case 2:
					{
						if(key[i].key_sta == 1)
						{
							key[i].judge_sta = 0;
							if(key[i].key_time <= 70)
							{
								key[i].single_flag = 1;
							}
						}
						else
						{
							key[i].key_time++;
							if(key[i].key_time > 70)
							{
								key[i].long_flag = 1;
							}
						}
						break;
					}
				}
			}
		}

	}
	if(htim->Instance == TIM4) //按键1s后响应
	{
			last_key_time++;
			if(last_key_time == 100)
			{
				last_key_time = 0;
				HAL_TIM_Base_Stop(htim);
				should_close_flag = 1;
			}
	}
	if(htim->Instance == TIM6)//关门4s
	{
		if(openordownTypeflag == CLOSE)
		{
			closeGate_time++;
			if(closeGate_time == 400)
			{
				closeGate_time = 0;
				HAL_TIM_Base_Stop_IT(htim);
				HAL_TIM_PWM_Stop(&htim17, TIM_CHANNEL_1);
				if(isupdown == UP)
				{
					if(Dispfloor != HighestFloor)
					{
						should_up_flag = 1;
					}
				}
				if(isupdown == DOWN)
				{
					if(Dispfloor != LowestFloor)
					{
						should_down_flag = 1;
					}
				}
			}
		}
		if(openordownTypeflag == OPEN)
		{
			openGate_time++;
			if(openGate_time == 400)
			{
				openGate_time = 0;
				HAL_TIM_Base_Stop_IT(htim);
				HAL_TIM_PWM_Stop(&htim17, TIM_CHANNEL_1);
				openorcloseRunningflag = 0;
				if(isupdown == UP)
				{
					if(Dispfloor != HighestFloor)
					{
						should_wait_flag = 1;
					}
				}
				else
				{
					if(Dispfloor != LowestFloor)
					{
						should_wait_flag = 1;
					}
				}
			}
		}

	}
	if(htim->Instance == TIM7) //上楼6s * 层数
	{
		if(isupdown == UP)
		{
			upfloor_time++;
			if(upfloor_time == 600 * UpFloorNum)
			{
				upfloor_time = 0;
				HAL_TIM_Base_Stop_IT(htim);
				HAL_TIM_PWM_Stop(&htim16, TIM_CHANNEL_1);
				upordownRunningflag = 0;
				Dispfloor = NextUpFloor;
				floor_want[Dispfloor - 1] = 0;
				LED = LED & (~(0x01 << (Dispfloor - 1)));
				LED_Disp(LED);
				sprintf(text, "            ");
				LCD_DisplayStringLine(Line2, text);
				should_open_flag = 1;
				should_lcdflash_flag = 1;
			}
		}
		if(isupdown == DOWN)
		{
			downfloor_time++;
			if(downfloor_time >= 600 * DownFloorNum)
			{
				downfloor_time = 0;
				HAL_TIM_Base_Stop_IT(htim);
				HAL_TIM_PWM_Stop(&htim16, TIM_CHANNEL_1);
				upordownRunningflag = 0;
				Dispfloor = NextDownFloor;
				floor_want[Dispfloor - 1] = 0;
				LED = LED & (~(0x01 << (Dispfloor - 1)));
				LED_Disp(LED);
				sprintf(text, "            ");
				LCD_DisplayStringLine(Line2, text);
				should_open_flag = 1;
				should_lcdflash_flag = 1;
			}
		}
		
	}
	if(htim->Instance == TIM8)
	{
		waitGate_time++;
		if(waitGate_time == 200)
		{
			waitGate_time = 0;
			HAL_TIM_Base_Stop_IT(htim);
			waiting = 0;
			should_close_flag = 1;
		}
	}
	if(htim->Instance == TIM15)
	{
		lcdflash_time++;
		if(lcdflash_time == 25)
		{
			sprintf(text, "         %d   ", Dispfloor);
			LCD_DisplayStringLine(Line2, text);
		}
		else if(lcdflash_time == 50)
		{
			sprintf(text, "              ");
			LCD_DisplayStringLine(Line2, text);
		}
		else if(lcdflash_time == 75)
		{
			sprintf(text, "         %d   ", Dispfloor);
			LCD_DisplayStringLine(Line2, text);
		}
		else if(lcdflash_time == 100)
		{
			lcdflash_time = 0;
			HAL_TIM_Base_Stop_IT(htim);
		}
	}
	if(htim->Instance == TIM2)
	{
		ledflash_time++;
		if(ledflash_time == 10)
		{
			ledflash_time = 0;
			index++;
			index %= 4;
		}
	}
}

12.main.c的全部代码:

main.c:

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2023 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"
#include "rtc.h"
#include "tim.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "lcd.h"
#include "interrupt.h"
#include "stdio.h"
#include "led.h"
#include "stdlib.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 ---------------------------------------------------------*/

/* USER CODE BEGIN PV */
extern struct keys key[4];
char text[30];

unsigned char Dispfloor = 1;
RTC_TimeTypeDef T;
RTC_DateTypeDef D;
unsigned char second;
unsigned char LED = 0x00;
unsigned int last_key_time = 0;
extern unsigned char should_close_flag;
unsigned char GateCommand;
unsigned int closeGate_time;
unsigned char FloorCommand;
unsigned char should_up_flag;
unsigned int upfloor_time;
unsigned char floor_want[4] = {0, 0, 0, 0};
unsigned char isupdown = UP;
unsigned char HighestFloor = 1;
unsigned char LowestFloor = 1;
unsigned char should_judge_flag = 1;
unsigned char upordownRunningflag;
unsigned char NextUpFloor;
unsigned char UpFloorNum;
unsigned char should_open_flag;
unsigned char openordownTypeflag;
unsigned int openGate_time;
unsigned char should_wait_flag;
unsigned char waitGate_time;
unsigned char should_lcdflash_flag;
unsigned int lcdflash_time;
unsigned char NextDownFloor;
unsigned char DownFloorNum;
unsigned char should_down_flag;
unsigned int downfloor_time;
unsigned char openorcloseRunningflag;
unsigned char waiting;
unsigned int ledflash_time;
unsigned char index;
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
void DisposeKey(void);
void Gate_Control(unsigned char command);
void Gate_Run(unsigned char GateCommand);
void UporDown_Control(unsigned char command);
void UporDown_Run(unsigned char FloorCommand);
void Judge_upordown(void);
unsigned char Judge_HighestFloor(void);
unsigned char Judge_LowestFloor(void);
unsigned char judge_NextUpFloor(void);
unsigned char judge_NextDownFloor(void);
/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* 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_TIM3_Init();
  MX_TIM16_Init();
  MX_TIM17_Init();
  MX_RTC_Init();
  MX_TIM4_Init();
  MX_TIM6_Init();
  MX_TIM7_Init();
  MX_TIM8_Init();
  MX_TIM15_Init();
  MX_TIM2_Init();
  /* USER CODE BEGIN 2 */
	LCD_Init();
	LCD_Clear(Black);
	LCD_SetBackColor(Black);
	LCD_SetTextColor(White);
	HAL_TIM_Base_Start_IT(&htim3);
	LCD_DisplayStringLine(Line1, "        floor");
	sprintf(text, "         %d", Dispfloor);
	LCD_DisplayStringLine(Line2, text);
	LED_Disp(0x00);
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
		HAL_RTC_GetTime(&hrtc, &T, RTC_FORMAT_BIN);
		HAL_RTC_GetDate(&hrtc, &D, RTC_FORMAT_BIN);
		if(second != T.Seconds)
		{
			second = T.Seconds;
			sprintf(text, "      %02d:%02d:%02d", T.Hours, T.Minutes, T.Seconds);
			LCD_DisplayStringLine(Line4, text);
		}
/*************************以上是RTC*********************************************************/
		DisposeKey();
		if(floor_want[0] + floor_want[1] + floor_want[2] + floor_want[3] != 0)
		{
			if(isupdown == UP)
			{
				HighestFloor = Judge_HighestFloor();
			}
			if(isupdown == DOWN)
			{
				LowestFloor = Judge_LowestFloor();
			}
		}
		if(isupdown == UP)
		{
			if(Dispfloor == HighestFloor)
			{
				should_judge_flag = 1;
			}
		}
		if(isupdown == DOWN)
		{
			if(Dispfloor == LowestFloor)
			{
				should_judge_flag = 1;
			}
		}
		if(should_judge_flag)
		{
			if(floor_want[0] + floor_want[1] + floor_want[2] + floor_want[3] != 0)
			{
				unsigned char temp_Highest = Judge_HighestFloor();
				unsigned char temp_Lowest = Judge_LowestFloor();
				if(Dispfloor > temp_Lowest)
				{
					isupdown = DOWN;
				}
				if(Dispfloor < temp_Highest)
				{
					isupdown = UP;
				}

			}
		}
/***********************以上是判断上行还是下行******************************************************/
		if(upordownRunningflag)
		{

			HAL_TIM_Base_Start_IT(&htim2);
			if(isupdown == UP)
			{
				LED = ((LED & 0x0f) | (0x01 << (4 + index)));
				LED_Disp(LED);
			}
			else
			{
				LED = ((LED & 0x0f) | (0x08 << (4 - index)));
				LED_Disp(LED);
			}
		}
		else
		{
			__HAL_TIM_SetCounter(&htim2, 0);
			ledflash_time = 0;
			HAL_TIM_Base_Stop_IT(&htim2);
			LED = LED & 0x0f;
			LED_Disp(LED);
		}
/**********************以上是LED流动***************************************************************/

		if(isupdown == UP)
		{
			NextUpFloor = judge_NextUpFloor();
			UpFloorNum = NextUpFloor - Dispfloor;
		}
		else
		{
			NextDownFloor = judge_NextDownFloor();
			DownFloorNum = Dispfloor - NextDownFloor;
		}
		if(should_close_flag)
		{
			should_close_flag = 0;
			Gate_Control(CLOSE);
			Gate_Run(CLOSE);
			openordownTypeflag = CLOSE;
			openorcloseRunningflag = 1;
			__HAL_TIM_SetCounter(&htim6, 0);
			closeGate_time = 0;
			HAL_TIM_Base_Start_IT(&htim6);
		}
		if(should_up_flag)
		{
			should_up_flag = 0;
			UporDown_Control(UP);
			UporDown_Run(UP);
			upordownRunningflag = 1;
			__HAL_TIM_SetCounter(&htim7, 0);
			upfloor_time = 0;
			HAL_TIM_Base_Start_IT(&htim7);
		}
		if(should_down_flag)
		{
			should_down_flag = 0;
			UporDown_Control(DOWN);
			UporDown_Run(DOWN);
			upordownRunningflag = 1;
			__HAL_TIM_SetCounter(&htim7, 0);
			downfloor_time = 0;
			HAL_TIM_Base_Start_IT(&htim7);
		}
		if(should_open_flag)
		{
			should_open_flag = 0;
			Gate_Control(OPEN);
			Gate_Run(OPEN);
			openordownTypeflag = OPEN;
			openorcloseRunningflag = 1;
			__HAL_TIM_SetCounter(&htim6, 0);
			openGate_time = 0;
			HAL_TIM_Base_Start_IT(&htim6);
		}
		if(should_wait_flag)
		{
			should_wait_flag = 0;
			waiting = 1;
			__HAL_TIM_SetCounter(&htim8, 0);
			waitGate_time = 0;
			HAL_TIM_Base_Start_IT(&htim8);
		}
		if(should_lcdflash_flag)
		{
			should_lcdflash_flag = 0;
			__HAL_TIM_SetCounter(&htim15, 0);
			lcdflash_time = 0;
			HAL_TIM_Base_Start_IT(&htim15);
		}
		
/*****************************************************以上是控制区******************************/		

  }
  /* 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 RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  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 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();
  }
  /** Initializes the peripherals clocks
  */
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC;
  PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_HSE_DIV32;

  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */
void DisposeKey(void)
{

		if(key[0].single_flag)
		{
			if(Dispfloor - 1 != 0)
			{
				LED |= 0x01;
				LED_Disp(LED);
				floor_want[0] = 1;
				__HAL_TIM_SetCounter(&htim4, 0);
				last_key_time = 0;
				HAL_TIM_Base_Start_IT(&htim4);
			}
			key[0].single_flag = 0;
		}
		if(key[1].single_flag)
		{
			if(Dispfloor - 1 != 1)
			{
				LED |= 0x02;
				LED_Disp(LED);
				floor_want[1] = 1;
				LCD_DisplayStringLine(Line5, "     ");
				__HAL_TIM_SetCounter(&htim4, 0);
				last_key_time = 0;
				HAL_TIM_Base_Start_IT(&htim4);
			}
			key[1].single_flag = 0;
		}
		if(key[2].single_flag)
		{
			if(Dispfloor - 1 != 2)
			{
				LED |= 0x04;
				LED_Disp(LED);
				floor_want[2] = 1;
				__HAL_TIM_SetCounter(&htim4, 0);
				last_key_time = 0;
				HAL_TIM_Base_Start_IT(&htim4);
			}
			key[2].single_flag = 0;
		}
		if(key[3].single_flag)
		{
			if(Dispfloor - 1 != 3)
			{
				LED |= 0x08;
				LED_Disp(LED);
				floor_want[3] = 1;
				__HAL_TIM_SetCounter(&htim4, 0);
				last_key_time = 0;
				HAL_TIM_Base_Start_IT(&htim4);

			}
			key[3].single_flag = 0;
		}
	
	
}

void Gate_Control(unsigned char command)
{
	if(command == 0)
	{
		HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
		GateCommand = 0;
	}
	else
	{
		HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);
		GateCommand = 1;
	}
}

void Gate_Run(unsigned char GateCommand)
{
	if(GateCommand == 0)
	{
		__HAL_TIM_SetCompare(&htim17, TIM_CHANNEL_1, 60);
		HAL_TIM_PWM_Start(&htim17, TIM_CHANNEL_1);
	}
	if(GateCommand == 1)
	{
		__HAL_TIM_SetCompare(&htim17, TIM_CHANNEL_1, 50);
		HAL_TIM_PWM_Start(&htim17, TIM_CHANNEL_1);
	}
}

void UporDown_Control(unsigned char command)
{
	if(command)
	{
		HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);
		FloorCommand = 0;
	}
	else
	{
		HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);
		FloorCommand = 1;
	}
}

void UporDown_Run(unsigned char FloorCommand)
{
	if(FloorCommand == 0)
	{
		__HAL_TIM_SetCompare(&htim16, TIM_CHANNEL_1, 60);
		HAL_TIM_PWM_Start(&htim16, TIM_CHANNEL_1);
	}
	else
	{
		__HAL_TIM_SetCompare(&htim16, TIM_CHANNEL_1, 80);
		HAL_TIM_PWM_Start(&htim16, TIM_CHANNEL_1);		
	}
}

void Judge_upordown(void)
{
	for(unsigned char i = 0; i < Dispfloor - 1; i++)
	{
		if(floor_want[i] == 1)
		{
			isupdown = DOWN;
		}
	}
	for(unsigned char i = 3; i > Dispfloor - 1; i--)
	{
		if(floor_want[i] == 1)
		{
			isupdown = UP;
		}
	}
}

unsigned char Judge_HighestFloor(void)
{
	unsigned char i = 4;
	for(i = 4; i > 1; i--)
	{
		if(floor_want[i - 1] == 1)
		{
			break;
		}
	}
	return i;
}

unsigned char Judge_LowestFloor(void)
{
	unsigned char i = 1;
	for(i = 1; i < 4; i++)
	{
		if(floor_want[i - 1] == 1)
		{
			break;
		}
	}
	return i;
}

unsigned char judge_NextUpFloor(void)
{
	unsigned char i = Dispfloor + 1;
	for(i = Dispfloor + 1; i < 4; i++)
	{
		if(floor_want[i - 1] == 1)
		{
			break;
		}
	}
	return i;
}

unsigned char judge_NextDownFloor(void)
{
	unsigned char i = Dispfloor - 1;
	for(i = Dispfloor - 1; i >= 1; i--)
	{
		if(floor_want[i - 1] == 1)
		{
			break;
		}
	}
	return i;
}


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

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

四、完成效果

蓝桥杯嵌入式第八届省赛试题实现效果

五、总结

其实说本篇文章只是为了存放我的代码,所以看不懂很正常,如果需要代码可以找我私信。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值