本文主要是介绍第十五届蓝桥杯嵌入式模拟考试II,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
第十五届蓝桥杯嵌入式模拟考试II
模拟网址
这个题目就很简单,满分85分都拿到,就是评测系统有时会很抽搐,非显示说乱码
题目
程序
main.c
/* USER CODE BEGIN Header */
/********************************************************************************* @file : main.c* @brief : Main program body******************************************************************************* @attention** <h2><center>© 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"
#include "adc.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "lcd.h"
#include "key.h"
#include "led.h"
#include "usart1.h"
#include "myadc.h"
#include <stdio.h>
#include <string.h>
/* USER CODE END Includes *//* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
extern uint8_t pointer;
extern uint8_t buffer[3];
extern uint8_t rxdata[1];
extern Key key[4];
uint8_t menu = 0;
float temp,val;
uint8_t mode[5] = "Auto";
uint8_t m =0;// 0自动 1手动
uint8_t gear = 1;
uint8_t K = 10,b= 10;
uint8_t lcdtext[20];
uint32_t led4time;
uint8_t led4enable;
uint8_t led;
uint8_t printtext[20];
uint32_t BandRxtime;
/* 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 *//* USER CODE END PFP *//* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void key_process(void)
{if(key[0].key_flag){key[0].key_flag = 0;LCD_Clear(Black);LCD_SetBackColor(Black);LCD_SetTextColor(White);BandRxtime = uwTick;if(menu==0){if(m==0){strcpy((char *)mode,"Manu");}else{strcpy((char *)mode,"Auto");}m^=1;}if(menu == 1){menu = 0;}}if(key[1].key_flag){key[1].key_flag = 0;LCD_Clear(Black);LCD_SetBackColor(Black);LCD_SetTextColor(White);BandRxtime = uwTick;if(menu==0&&m==1){gear++;if(gear>3)gear = 3;}if(menu == 1){menu = 0;}}if(key[2].key_flag){key[2].key_flag = 0;LCD_Clear(Black);LCD_SetBackColor(Black);LCD_SetTextColor(White);BandRxtime = uwTick;if(menu==0&&m==1){gear--;if(gear<1)gear = 1;}if(menu == 1){menu = 0;}}
}void adc_process(void)
{temp = get_adc(&hadc2)*K+b;if(temp<20){temp = 20.0;}else if(temp>40){temp = 40.0;}if(m==0){if(temp<25){gear = 1;}else if(temp>=25&&temp<=30){gear = 2;}else if(temp>30){gear = 3;}}}void lcd_process(void)
{switch(menu){case 0:{sprintf((char *)lcdtext," DATA");LCD_DisplayStringLine(Line1,lcdtext);sprintf((char *)lcdtext," TEMP:%.1f",temp);LCD_DisplayStringLine(Line3,lcdtext);sprintf((char *)lcdtext," MODE:%s",mode);LCD_DisplayStringLine(Line4,lcdtext);sprintf((char *)lcdtext," GEAR:%d",gear);LCD_DisplayStringLine(Line5,lcdtext);}break;case 1:{sprintf((char *)lcdtext," SLEEPING");LCD_DisplayStringLine(Line4,lcdtext);sprintf((char *)lcdtext," TEMP:%.1f",temp);LCD_DisplayStringLine(Line5,lcdtext);}break;}
}void rx_process(void)
{if(pointer>0){led4enable = 1;BandRxtime = uwTick;led4time = uwTick;if(buffer[0]=='B'){if(buffer[1]=='1'){key[0].key_flag = 1;}else if(buffer[1]=='2'){key[1].key_flag = 1;}else if(buffer[1]=='3'){key[2].key_flag = 1;}else{sprintf((char *)printtext,"NULL");HAL_UART_Transmit(&huart1,printtext,sizeof(printtext),100);}}else{sprintf((char *)printtext,"NULL");HAL_UART_Transmit(&huart1,printtext,sizeof(printtext),100);}pointer = 0;memset(buffer,0,sizeof(buffer));}
}void pwm_process()
{switch(gear){case 1:{TIM2->CCR2 = (TIM2->ARR)*0.1f;}break;case 2:{TIM2->CCR2 = (TIM2->ARR)*0.4f;}break;case 3:{TIM2->CCR2 = (TIM2->ARR)*0.8f;}break;}
}void led_process(void)
{if(gear==1){led|=0x01;}else{led&=~0x01;}if(gear == 2){led|=0x02;}else{led&=~0x02;}if(gear==3){led|=0x04;}else{led&=~0x04;}if(led4enable){if(uwTick-led4time>=3000)led4enable = 0;led|=0x08;}else{led&=~0x08;}if(strcmp((char *)mode,"Auto")==0){led|=0x80;}else{led&=~0x80;}led_display(led);
}void BandRx_process(void)
{if(uwTick-BandRxtime>=5000){menu = 1;BandRxtime = uwTick;LCD_Clear(Black);LCD_SetBackColor(Black);LCD_SetTextColor(White);}
}
/* 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_TIM2_Init();MX_TIM4_Init();MX_USART1_UART_Init();/* USER CODE BEGIN 2 */LCD_Init();/* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */LCD_Clear(Black);LCD_SetBackColor(Black);LCD_SetTextColor(White);HAL_TIM_Base_Start_IT(&htim4);HAL_UART_Receive_IT(&huart1,rxdata,1);HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_2);led_display(0x00);while (1){/* USER CODE END WHILE *//* USER CODE BEGIN 3 */adc_process();lcd_process();key_process();led_process();pwm_process();rx_process();BandRx_process();}/* 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_ON;RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV2;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_USART1|RCC_PERIPHCLK_ADC12;PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;PeriphClkInit.Adc12ClockSelection = RCC_ADC12CLKSOURCE_SYSCLK;if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK){Error_Handler();}
}/* USER CODE BEGIN 4 *//* 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****/
这篇关于第十五届蓝桥杯嵌入式模拟考试II的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!