第十五届蓝桥杯第三期模拟赛(嵌入式)编程题

一、前言

本以为3月10日模拟赛才开始,昨天才发现是3月10日截止!恰好今日没课,抽出四小时时间做了一下今年的模拟题,编程题难度较小,个人认为省赛难度会比这大。由于没学习过选择,所以感觉最难的是选择题......所以就不放选择了,只放编程题。

二、题目

发现交卷之后无法再次查看试卷了......等模拟赛结束之后再贴上来吧。

三、知识储备

1、STM32G431RBT6国信长天产品手册

2、STM32各模块基本配置(网上教程很多)

3、KEIL5使用方法

4、C语言基本知识

四、代码

main.c

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

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "led.h"
#include "interrupt.h"
#include "lcd.h"
#include "string.h"
#include "stdlib.h"
#include "stdio.h"
#include "myadc.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
extern struct keys key[];
unsigned char page;
float R37_V;
float R38_V;
float SR37_lower=1.2;
float SR37_upper=2.2;
float SR38_lower=1.4;
float SR38_upper=3.0;
float PR37;
float PR38;
unsigned char choose_set;
bool jiance_flag_R37;
bool jiance_flag_R38;
bool clear_flag;
float R37_hege_num;
float R37_buhege_num;
float R38_hege_num;
float R38_buhege_num;
extern unsigned char BufIndex;
extern char RxBuffer[];
extern unsigned char Rxdata;
/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
void lcd_func(void);
void key_func(void);
void adc_func(void);
void jiance(void);
void rx_func(void);
void rx_judge(void);
/* 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 */

/* 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_ADC1_Init();
  MX_ADC2_Init();
  MX_TIM3_Init();
  MX_USART1_UART_Init();
  /* USER CODE BEGIN 2 */
led_disp(0x00);
HAL_TIM_Base_Start_IT(&htim3);
HAL_UART_Receive_IT(&huart1,&Rxdata,1);

LCD_Init();
LCD_Clear(Black);
LCD_SetBackColor(Black);
LCD_SetTextColor(White);

  /* USER CODE END 2 */

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

    /* USER CODE BEGIN 3 */
	  lcd_func();
	  key_func();
	  adc_func();
	  jiance();
	  rx_judge();
  }
  /* 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_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();
  }
}

/* USER CODE BEGIN 4 */

int fputc(int ch,FILE *f)
{
	HAL_UART_Transmit(&huart1,(unsigned char*)&ch,1,HAL_MAX_DELAY);
	return ch;
}

void rx_judge()
{
	if(BufIndex!=0)
	{
		unsigned char temp = BufIndex;
		HAL_Delay(1);
		if(temp==BufIndex)
		{
			rx_func();
		}
	}
}
void rx_func()
{
	if(BufIndex==3)
	{
		if(strcmp(RxBuffer,"R37")==0)
		{
			printf("R37:%d:%d:%.1f\r\n",(int8_t)(R37_hege_num+R37_buhege_num),(int8_t)(R37_hege_num),PR37);
		}
		else if(strcmp(RxBuffer,"R38")==0)
		{
			printf("R38:%d:%d:%.1f\r\n",(int8_t)(R38_hege_num+R38_buhege_num),(int8_t)(R38_hege_num),PR38);
		}
		memset(RxBuffer,0,30);
		BufIndex = 0;
	}
}

void jiance()
{
	if(jiance_flag_R37 == 1)
	{
		if(R37_V<=SR37_upper&&R37_V>=SR37_lower)
		{
			R37_hege_num++;
			led_disp(0x01);
			HAL_Delay(1000);
			led_disp(0x00);
		}
		else R37_buhege_num++;
		PR37 = R37_hege_num/(R37_buhege_num+R37_hege_num)*100;
		jiance_flag_R37 = 0;
	}
	if(jiance_flag_R38 == 1)
	{
		if(R38_V<=SR38_upper&&R38_V>=SR38_lower)
		{
			R38_hege_num++;
			led_disp(0x02);
			HAL_Delay(1000);
			led_disp(0x00);
		}
		else R38_buhege_num++;
		PR38 = R38_hege_num/(R38_buhege_num+R38_hege_num)*100;
		jiance_flag_R38 = 0;
	}
	if(clear_flag==1)
	{
		R37_hege_num = 0;
		R37_buhege_num = 0;
		R38_hege_num = 0;
		R38_buhege_num = 0;
		clear_flag=0;
	}
}
void adc_func()
{
	R37_V = getadc(&hadc2);
	R38_V = getadc(&hadc1);
}
void lcd_func()
{
	if(page==0)
	{
		char text[30];
		sprintf(text,"       GOODS  ");
		LCD_DisplayStringLine(Line1,(uint8_t*)text);
		sprintf(text,"     R37:%.2fV  ",R37_V);
		LCD_DisplayStringLine(Line3,(uint8_t*)text);
		sprintf(text,"     R38:%.2fV  ",R38_V);
		LCD_DisplayStringLine(Line4,(uint8_t*)text);
		led_disp(0x04);
	}
	else if(page==1)
	{
		char text[30];
		sprintf(text,"      STANDARD  ");
		LCD_DisplayStringLine(Line1,(uint8_t*)text);
		sprintf(text,"    SR37:%.1f-%.1f  ",SR37_lower,SR37_upper);
		LCD_DisplayStringLine(Line3,(uint8_t*)text);
		sprintf(text,"    SR38:%.1f-%.1f  ",SR38_lower,SR38_upper);
		LCD_DisplayStringLine(Line4,(uint8_t*)text);
		led_disp(0x08);
	}
	else if(page==2)
	{
		char text[30];
		sprintf(text,"        PASS  ");
		LCD_DisplayStringLine(Line1,(uint8_t*)text);
		sprintf(text,"     PR37:%.1f%%  ",PR37);
		LCD_DisplayStringLine(Line3,(uint8_t*)text);
		sprintf(text,"     PR38:%.1f%%  ",PR38);
		LCD_DisplayStringLine(Line4,(uint8_t*)text);
		led_disp(0x10);
	}
}

void key_func()
{
	if(key[0].single_sta == 1)
	{
		page++;
		if(page>2) page=0;
		LCD_Clear(Black);
		choose_set = 0;
		clear_flag = 0;
		jiance_flag_R38 = 0;
		jiance_flag_R37 = 0;
		key[0].single_sta = 0;
	}
	if(key[1].single_sta == 1)
	{
		if(page==0)
		{
			jiance_flag_R37 = 1;
		}
		else if(page==1)
		{
			choose_set++;
			if(choose_set>4) choose_set = 1;
		}
		key[1].single_sta = 0;
	}
	if(key[2].single_sta == 1)
	{
		if(page==0)
		{
			jiance_flag_R38 = 1;
		}
		else if(page==1)
		{
			switch(choose_set)
			{
				case 1:
				{
					SR37_upper = SR37_upper + 0.2;
					if(SR37_upper>=3.2) SR37_upper = 2.2;
					break;
				}
				case 2:
				{
					SR37_lower = SR37_lower +0.2;
					if(SR37_lower>=2.2) SR37_lower = 1.2;
					break;
				}
				case 3:
				{
					SR38_upper = SR38_upper + 0.2;
					if(SR38_upper>=3.2) SR38_upper = 2.2;
					break;
				}
				case 4:
				{
					SR38_lower = SR38_lower +0.2;
					if(SR38_lower>=2.2) SR38_lower = 1.2;
					break;
				}
			}
		}
		key[2].single_sta = 0;
	}
	if(key[3].single_sta == 1)
	{
		if(page==2)
		{
			clear_flag = 1;
		}
		else if(page==1)
		{
			switch(choose_set)
			{
				case 1:
				{
					SR37_upper = SR37_upper - 0.2;
					if(SR37_upper<=2.0) SR37_upper = 3.0;
					break;
				}
				case 2:
				{
					SR37_lower = SR37_lower - 0.2;
					if(SR37_lower<=1.0) SR37_lower = 2.0;
					break;
				}
				case 3:
				{
					SR38_upper = SR38_upper - 0.2;
					if(SR38_upper<=2.0) SR38_upper = 3.0;
					break;
				}
				case 4:
				{
					SR38_lower = SR38_lower - 	0.2;
					if(SR38_lower<=1.0) SR38_lower = 2.0;
					break;
				}
			}
		}
		key[3].single_sta = 0;
	}
}



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

led.c

#include "led.h"

void led_disp(unsigned char disp)
{
	HAL_GPIO_WritePin(GPIOC,GPIO_PIN_All,GPIO_PIN_SET);
	HAL_GPIO_WritePin(GPIOC,disp<<0x08,GPIO_PIN_RESET);
	HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);
	HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);
}



led.h

#ifndef __LED_H_
#define __LED_H_

#include "main.h"
void led_disp(unsigned char disp);



#endif


interrupt.c

#include "interrupt.h"
#include "USART.h"
struct keys key[4]={0,0,0,0,0};
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
	if(htim->Instance==TIM3)
	{
		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;
					}
					else key[i].judge_sta = 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)
					{
						if(key[i].key_time<=80)
						{
							key[i].single_sta = 1;
						}
						key[i].judge_sta = 0;
					}
					else if(key[i].key_sta==0)
					{
						key[i].key_time++;
						if(key[i].key_time>80)
						{
							key[i].long_sta = 1;
						}
					}
					break;
				}
			}
		}
	}
}

unsigned char BufIndex = 0;
char RxBuffer[30];
unsigned char Rxdata;
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
	if(huart->Instance==USART1)
	{
		RxBuffer[BufIndex++] = Rxdata;
		HAL_UART_Receive_IT(&huart1,&Rxdata,1);
	}
}

interrupt.h

#ifndef __INTERRUPT_H_
#define __INTERRUPT_H_

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

struct keys
{
	bool key_sta;
	bool single_sta;
	bool long_sta;
	unsigned char key_time;
	unsigned char judge_sta;
};




#endif



 myadc.c

#include "myadc.h"

double getadc(ADC_HandleTypeDef *hadc)
{
	if(hadc->Instance==ADC1)
	{
		unsigned int value1;
		HAL_ADC_Start(hadc);
		value1 = HAL_ADC_GetValue(hadc);
		return value1*3.3/4096;
	}
	if(hadc->Instance==ADC2)
	{
		unsigned int value2;
		HAL_ADC_Start(hadc);
		value2 = HAL_ADC_GetValue(hadc);
		return value2*3.3/4096;
	}
}


myadc.h

#ifndef __MYADC_H_
#define __MYADC_H_

#include "main.h"
double getadc(ADC_HandleTypeDef *hadc);

#endif



五、运行结果

第十五届蓝桥杯嵌入式模拟

  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值