2022 Blue Bridge Cup 埋め込み型地方競技エンジニアリング コード

2022 Blue Bridge Cup 埋め込み型地方競技エンジニアリング コード

ファイルは合計 4 つだけで、他のファイルは変更されず、CUBEMX 構成によって生成されます。

1.main.c

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

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

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
 uint8_t Flag_Screen=1;
 uint8_t RxBuff[7];
/* 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_USART1_UART_Init();
  MX_TIM2_Init();
  /* USER CODE BEGIN 2 */
	LCD_Init();
	LED_Init();
	LCD_Clear(Black);
	LCD_SetBackColor(Black);
	LCD_SetTextColor(White);
	HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_2);
	HAL_UART_Receive_IT(&huart1,RxBuff,sizeof(RxBuff));
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    
    
		PSD_Init();
		while(Flag_Screen)
		{
    
    
			PSD_SET();
		}
		STA_Init();
    /* 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};
  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_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  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_HSI;
  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_0) != HAL_OK)
  {
    
    
    Error_Handler();
  }
  /** Initializes the peripherals clocks
  */
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1;
  PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    
    
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */
int fputc(int ch, FILE* fp)
{
    
    
	HAL_UART_Transmit(&huart1,(uint8_t*)&ch,1,1000);
	return ch;
}
/* 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****/

2.main.h

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.h
  * @brief          : Header for main.c file.
  *                   This file contains the common defines of the application.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2022 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 */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __MAIN_H
#define __MAIN_H

#ifdef __cplusplus
extern "C" {
    
    
#endif

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

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
extern uint8_t Flag_Screen;
extern uint8_t RxBuff[7];
/* USER CODE END Includes */

/* Exported types ------------------------------------------------------------*/
/* USER CODE BEGIN ET */

/* USER CODE END ET */

/* Exported constants --------------------------------------------------------*/
/* USER CODE BEGIN EC */

/* USER CODE END EC */

/* Exported macro ------------------------------------------------------------*/
/* USER CODE BEGIN EM */

/* USER CODE END EM */

/* Exported functions prototypes ---------------------------------------------*/
void Error_Handler(void);

/* USER CODE BEGIN EFP */

/* USER CODE END EFP */

/* Private defines -----------------------------------------------------------*/
/* USER CODE BEGIN Private defines */

/* USER CODE END Private defines */

#ifdef __cplusplus
}
#endif

#endif /* __MAIN_H */

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

3.user.h (提出の便宜上、メインコードはここにあります)

#include "user.h"
#include "gpio.h"
#include "lcd.h"
#include "stdio.h"
#include "string.h"
#include "usart.h"


char user_password[3] = {
    
    '1','2','3'};
char password[3]={
    
    '@','@','@'};
char data[20];
uint8_t Error_count = 0;
uint16_t fre = 0;

void LED_Setting(uint16_t LED,uint8_t STATE)
{
    
    
	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_SET);
	if(STATE == LED_ON)
		HAL_GPIO_WritePin(GPIOC,LED,GPIO_PIN_RESET);
	else
		HAL_GPIO_WritePin(GPIOC,LED,GPIO_PIN_SET);
	HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);
	HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);
}

void LED_Init(void)
{
    
    
	  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_SET);
		HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);
		HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);
}

void PSD_Init(void)
{
    
    
	LCD_DisplayStringLine(Line1,(unsigned char *)"       PSD          ");	
	sprintf(data,"    B1:%c            ",password[0]);
	LCD_DisplayStringLine(Line3 ,(unsigned char *)data);
	sprintf(data,"    B2:%c            ",password[1]);
	LCD_DisplayStringLine(Line4 ,(unsigned char *)data);
	sprintf(data,"    B3:%c            ",password[2]);
	LCD_DisplayStringLine(Line5 ,(unsigned char *)data);
}


void	PSD_Reset(void)
{
    
    
	memset(password,'@',3);
	sprintf(data,"    B1:%c            ",password[0]);
	LCD_DisplayStringLine(Line3 ,(unsigned char *)data);
	sprintf(data,"    B2:%c            ",password[1]);
	LCD_DisplayStringLine(Line4 ,(unsigned char *)data);
	sprintf(data,"    B3:%c            ",password[2]);
	LCD_DisplayStringLine(Line5 ,(unsigned char *)data);		
}


void PSD_SET(void)
{
    
    
	if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0)==GPIO_PIN_RESET)
	{
    
    
		while(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0)==GPIO_PIN_RESET);
		if(password[PASSWORD1]=='@'||password[PASSWORD1]=='9')
			password[PASSWORD1] = '0';
		else
			password[PASSWORD1]++;
		sprintf(data,"    B1:%c            ",password[PASSWORD1]);
		LCD_DisplayStringLine(Line3 ,(unsigned char *)data);
	}
	if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_1)==GPIO_PIN_RESET)
	{
    
    
		while(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_1)==GPIO_PIN_RESET);
		if(password[PASSWORD2]=='@'||password[PASSWORD2]=='9')
			password[PASSWORD2] = '0';
		else
			password[PASSWORD2]++;
		sprintf(data,"    B2:%c            ",password[PASSWORD2]);
		LCD_DisplayStringLine(Line4 ,(unsigned char *)data);
	}
	if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_2)==GPIO_PIN_RESET)
	{
    
    
		while(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_2)==GPIO_PIN_RESET);
		if(password[PASSWORD3]=='@'||password[PASSWORD3]=='9')
			password[PASSWORD3] = '0';
		else
			password[PASSWORD3]++;
		sprintf(data,"    B3:%c            ",password[PASSWORD3]);
		LCD_DisplayStringLine(Line5 ,(unsigned char *)data);
	}
}


void STA_Init(void)
{
    
    
	LCD_DisplayStringLine(Line1,(unsigned char *)"       STA          ");
	TIM2->ARR = 500-1;
	TIM2->CCR2 = 50-1;
	sprintf(data,"    F:2000Hz        ");
	LCD_DisplayStringLine(Line3 ,(unsigned char *)data);
	sprintf(data,"    D:10%c            ",'%');
	LCD_DisplayStringLine(Line4 ,(unsigned char *)data);
	LCD_DisplayStringLine(Line5 ,(unsigned char *)"                    ");
	LED_Setting(LED1,LED_ON);
	HAL_Delay(5000);
	LED_Setting(LED1,LED_OFF);
	TIM2->ARR = 1000-1;
	TIM2->CCR2 = 500-1;
	Flag_Screen = 1;
}

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
    
    
	if(RxBuff[PASSWORD1]==user_password[PASSWORD1]&&RxBuff[PASSWORD2]==user_password[PASSWORD2]&&RxBuff[PASSWORD3]==user_password[PASSWORD3])
	{
    
    
		user_password[PASSWORD1] = RxBuff[4];
		user_password[PASSWORD2] = RxBuff[5];
		user_password[PASSWORD3] = RxBuff[6];
	}
	HAL_UART_Receive_IT(&huart1,RxBuff,sizeof(RxBuff));
}

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
    
    
	uint8_t i = 0;
	while(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0)==GPIO_PIN_RESET);
	if(user_password[PASSWORD1]==password[PASSWORD1]&&user_password[PASSWORD2]==password[PASSWORD2]&&user_password[PASSWORD3]==password[PASSWORD3])
	{
    
    
		memset(password,'@',3);
		Flag_Screen = 0;
		Error_count = 0;
	}
	else
	{
    
    
		PSD_Reset();
		Error_count++;
		if(Error_count == 3)
		{
    
    
			Error_count = 0;
			for(i=0;i<25;i++)
			{
    
    
			LED_Setting(LED2,LED_ON);
			HAL_Delay(100);
			LED_Setting(LED2,LED_OFF);
			HAL_Delay(100);
			}
		}
	}
}


4.user.h

#ifndef __USER_H__
#define __USER_H__

#include "main.h"

#define LED1 GPIO_PIN_8
#define LED2 GPIO_PIN_9
#define LED3 GPIO_PIN_10
#define LED4 GPIO_PIN_11
#define LED5 GPIO_PIN_12
#define LED6 GPIO_PIN_13
#define LED7 GPIO_PIN_14
#define LED8 GPIO_PIN_15
#define LED_ON 0
#define LED_OFF 1
#define PASSWORD1 0
#define PASSWORD2 1
#define PASSWORD3 2


void LED_Setting(uint16_t LED,uint8_t STATE);
void PSD_SET(void);
void PSD_Init(void);
void STA_SET(void);
void STA_Init(void);
void LED_Init(void);
void PSD_Reset(void);

#endif


おすすめ

転載: blog.csdn.net/YuHWEI/article/details/124084428