当前位置:   article > 正文

第十五届蓝桥杯嵌入式模拟题二_蓝桥杯嵌入式第十五届真题

蓝桥杯嵌入式第十五届真题

一、基本要求


使用大赛组委会提供的嵌入式竞赛实训平台(基于STM32G431RBT6微控制器设计),完成本试题的程序设计与调试。程序编写、调试完成后,选手需通过考试系统提交以准考证号命名的hex文件。

hex文件是唯一成绩评测依据,不符合以上文件提交要求的作品将被评为零分或者被酌情扣分。

二、硬件框图


图1 系统硬件框图

三、功能描述   


3.1 基本功能

1)通过微控制器的ADC功能,检测电位器R37上输出的模拟电压信号。

2)通过PA1引脚输出频率、占空比可调节的脉冲信号。

3)通过串口实现远程控制功能。

4)根据试题要求,通过LED和LCD完成数据显示和报警功能。

5)根据试题要求,通过按键实现切换界面和控制等功能。

3.2 显示功能

1)数据界面

数据界面如图2所示,显示要素包括界面名称(DATA)、温度值(TEMP)、当前控制模式(MODE)和档位(GEAR)。

图2 数据界面

2)睡眠界面

睡眠界面如图3所示,显示要素包括界面名称(SLEEPING)和温度值(TEMP)。

图3 睡眠界面

5s内按键无动作,或串口未下发命令,自动进入“睡眠界面”。

备注:

温度值(TEMP)单位为℃,保留小数点后1位有效数字;

当前控制模式(MODE):Auto表示自动控制模式;Manu表示手动控制模式;

自动控制模式:

温度值 < 25℃时,档位为1档。

25℃ ≤ 温度值 ≤ 30℃,档位为2档。

温度值 > 30℃时,档位为3档。

显示说明

(1)显示背景颜色(BackColor):黑色。

(2)显示字体颜色(TextColor):白色。

(3)请严格按照图示要求设计各个信息项的名称(区分字母大小写)和相对行列位置。

3.3 模拟功能

通过检测电位器R37输出的模拟电压信号VR37模拟温度,VR37和与温度的关系如图4所示。

图4 VR37和温度关系图

3.3 按键功能

1)B1:定义为“模式”按键,在数据界面下,按下B1按键,切换自动控制模式和手动控制模式。切换顺序如图5所示。

图5 模式切换

2)B2:定义为“档位加”按键,在“手动控制”模式下,按下B2按键,调整当前档位。切换顺序如图6所示。

图5 档位加顺序

3)B3:定义为“档位减”按键,在“手动控制”模式下,按下B3按键,调整当前档位。切换顺序如图6所示。

图5 档位减顺序

注意:

(1)按键B2和B3仅在“手动控制”模式下有效。

(2)按键应进行有效的防抖处理,避免出现一次按键动作触发多次功能等情形。

(3)按键动作不应影响数据采集过程和屏幕显示效果。

(4)在“睡眠界面”下,按下按键B1、B2或B3切换到“数据界面”,且当前按键动作仅作唤醒“数据界面”使用,不触发按键功能。

3.5 串口功能

串口作远程遥控使用,串口功能如下:

串口发送‘B1’定义为按键B1,实现按键B1切换模式功能;

串口发送‘B2’,定义为按键B2,实现B2“档位加”功能;

串口发送‘B3’,定义为按键B3,实现B3“档位减”功能;

串口发送其他非法字符,返回“NULL”。

注意:

串口通信波特率9600;

除命令‘B1’、‘B2’和‘B3’外,其他字符均为非法字符。

在“睡眠界面”下,发送命令‘B1’、‘B2’和‘B3’界面切换到“数据界面”,且当前命令仅作唤醒“数据界面”使用,不触发功能。

3.6 PWM功能

通过PA1输出2kHz,占空比连续可调的频率信号,占空比和档位关系:

档位为1,占空比10%;

档位为2,占空比40%;

档位为3,占空比80%。

3.7 LED指示灯功能

1)在自动控制模式下,指示灯LD8点亮,否则熄灭;

2)档位为1时,指示灯LD1点亮,否则熄灭;

3)档位为2时,指示灯LD2点亮,否则熄灭;

4)档位为3时,指示灯LD3点亮,否则熄灭;

5)串口发送命令,同时指示灯LD4点亮,3秒后熄灭;

5)指示灯LD5~LD7始终处于熄灭状态。

3.8 初始状态说明

设备上电后,初始状态如下:

1)自动控制模式;

2)处于数据界面。

备注:请严格按照此初始状态设计功能。

具体代码如下:

  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2024 STMicroelectronics.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by ST under BSD 3-Clause license,
  13. * the "License"; You may not use this file except in compliance with the
  14. * License. You may obtain a copy of the License at:
  15. * opensource.org/licenses/BSD-3-Clause
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "main.h"
  22. #include "adc.h"
  23. #include "tim.h"
  24. #include "usart.h"
  25. #include "gpio.h"
  26. /* Private includes ----------------------------------------------------------*/
  27. /* USER CODE BEGIN Includes */
  28. #include "stdio.h"
  29. #include "string.h"
  30. #include "led.h"
  31. #include "myadc.h"
  32. #include "lcd.h"
  33. #include "interrupt.h"
  34. /* USER CODE END Includes */
  35. /* Private typedef -----------------------------------------------------------*/
  36. /* USER CODE BEGIN PTD */
  37. extern struct key_t key[4];
  38. extern struct adc2_t adc2;
  39. extern struct interface_t interface;
  40. extern struct uart1_t uart1;
  41. /* USER CODE END PTD */
  42. /* Private define ------------------------------------------------------------*/
  43. /* USER CODE BEGIN PD */
  44. /* USER CODE END PD */
  45. /* Private macro -------------------------------------------------------------*/
  46. /* USER CODE BEGIN PM */
  47. void lcd_process(void)
  48. {
  49. char buf[30];
  50. if(interface.flg == 0)//DATA
  51. {
  52. if(interface.clean_flg)
  53. {
  54. LCD_Clear(Black);
  55. interface.clean_flg = 0;
  56. }
  57. sprintf(buf," DATA");
  58. LCD_DisplayStringLine(Line1,(uint8_t*)buf);
  59. sprintf(buf," TEMP:%.1f",adc2.temp);
  60. LCD_DisplayStringLine(Line3,(uint8_t*)buf);
  61. if(interface.mode == 0)
  62. {
  63. sprintf(buf," MODE:Auto");
  64. }
  65. else if(interface.mode == 1)
  66. {
  67. sprintf(buf," MODE:Manu");
  68. }
  69. LCD_DisplayStringLine(Line4,(uint8_t*)buf);
  70. sprintf(buf," GEAR:%d",interface.gears);
  71. LCD_DisplayStringLine(Line5,(uint8_t*)buf);
  72. }
  73. if(interface.flg == 1)//sleep
  74. {
  75. if(interface.sleep_clean)
  76. {
  77. LCD_Clear(Black);
  78. interface.sleep_clean = 0;
  79. }
  80. sprintf(buf," SLEEPING");
  81. LCD_DisplayStringLine(Line4,(uint8_t*)buf);
  82. sprintf(buf," TEMP:%.1f",adc2.temp);
  83. LCD_DisplayStringLine(Line5,(uint8_t*)buf);
  84. }
  85. }
  86. void key_process(void)
  87. {
  88. //B1
  89. if(key[0].short_flg == 1 && interface.flg == 0)
  90. {
  91. interface.sleep_time = 500;
  92. interface.mode++;
  93. if(interface.mode > 1)
  94. {
  95. interface.mode = 0;
  96. }
  97. key[0].short_flg = 0;
  98. }
  99. if(key[0].short_flg == 1 && interface.flg == 1)
  100. {
  101. interface.clean_flg = 1;
  102. interface.sleep_time = 500;
  103. key[0].short_flg = 0;
  104. }
  105. //B2
  106. if(key[1].short_flg == 1 && interface.mode == 1 && interface.flg == 0)
  107. {
  108. interface.sleep_time = 500;
  109. interface.gears++;
  110. if(interface.gears >= 3)
  111. {
  112. interface.gears = 3;
  113. }
  114. key[1].short_flg = 0;
  115. }
  116. if(key[1].short_flg == 1&& interface.flg == 1)
  117. {
  118. interface.clean_flg = 1;
  119. interface.sleep_time = 500;
  120. key[1].short_flg = 0;
  121. }
  122. if(key[1].short_flg == 1&& interface.flg == 0)
  123. {
  124. interface.sleep_time = 500;
  125. key[1].short_flg = 0;
  126. }
  127. //B3
  128. if(key[2].short_flg == 1 && interface.mode == 1&& interface.flg == 0)
  129. {
  130. interface.sleep_time = 500;
  131. interface.gears--;
  132. if(interface.gears <= 1)
  133. {
  134. interface.gears = 1;
  135. }
  136. key[2].short_flg = 0;
  137. }
  138. if(key[2].short_flg == 1 && interface.flg == 1)
  139. {
  140. interface.clean_flg = 1;
  141. interface.sleep_time = 500;
  142. key[2].short_flg = 0;
  143. }
  144. if(key[2].short_flg == 1 && interface.flg == 0)
  145. {
  146. interface.sleep_time = 500;
  147. key[2].short_flg = 0;
  148. }
  149. }
  150. void adc_process(void)
  151. {
  152. adc2.vol = get_adc();
  153. if(adc2.vol <= 1.0f)
  154. {
  155. adc2.temp = 20.0f;
  156. }
  157. else if(adc2.vol >= 3.0f)
  158. {
  159. adc2.temp = 40.0f;
  160. }
  161. else
  162. {
  163. adc2.temp = 10.f*adc2.vol +10;
  164. }
  165. if(interface.mode == 0)
  166. {
  167. if(adc2.temp < 25)
  168. {
  169. interface.gears = 1;
  170. }
  171. else if(adc2.temp <= 30)
  172. {
  173. interface.gears = 2;
  174. }
  175. else
  176. {
  177. interface.gears = 3;
  178. }
  179. }
  180. }
  181. void pwm_process(void)
  182. {
  183. if(interface.gears == 1)
  184. {
  185. __HAL_TIM_SetCompare(&htim2,TIM_CHANNEL_2,10);
  186. }
  187. else if(interface.gears == 2)
  188. {
  189. __HAL_TIM_SetCompare(&htim2,TIM_CHANNEL_2,40);
  190. }
  191. else if(interface.gears == 3)
  192. {
  193. __HAL_TIM_SetCompare(&htim2,TIM_CHANNEL_2,80);
  194. }
  195. }
  196. void myprintf(uint8_t *buf)
  197. {
  198. while(*buf != 0)
  199. {
  200. HAL_UART_Transmit(&huart1,buf++,1,0xffff);
  201. }
  202. }
  203. void uart_process(void)
  204. {
  205. uint8_t str[10];
  206. if(uart1.place > 0)
  207. {
  208. uart1.lenght = uart1.place;
  209. HAL_Delay(1);
  210. if(uart1.lenght == uart1.place)
  211. {
  212. uart1.led_flg = 1;
  213. if(uart1.place == 4)
  214. {
  215. sscanf((char*)uart1.buf,"%2s",str);
  216. if(!strcmp((char*)str,"B1"))
  217. {
  218. //B1
  219. if(interface.flg == 0)
  220. {
  221. interface.sleep_time = 500;
  222. interface.mode++;
  223. if(interface.mode > 1)
  224. {
  225. interface.mode = 0;
  226. }
  227. }
  228. else if(interface.flg == 1)
  229. {
  230. interface.clean_flg = 1;
  231. interface.sleep_time = 500;
  232. }
  233. else if(interface.flg == 0)
  234. {
  235. interface.sleep_time = 500;
  236. }
  237. }
  238. else if (!strcmp((char*)str,"B2"))
  239. {
  240. //B1
  241. if(interface.flg == 0 && interface.mode== 1)
  242. {
  243. interface.sleep_time = 500;
  244. interface.gears++;
  245. if(interface.gears >= 3)
  246. {
  247. interface.gears = 3;
  248. }
  249. }
  250. else if(interface.flg == 1)
  251. {
  252. interface.clean_flg = 1;
  253. interface.sleep_time = 500;
  254. }
  255. else if(interface.flg == 0)
  256. {
  257. interface.sleep_time = 500;
  258. }
  259. }
  260. else if (!strcmp((char*)str,"B3"))
  261. {
  262. //B1
  263. if(interface.flg == 0 && interface.mode== 1)
  264. {
  265. interface.sleep_time = 500;
  266. interface.gears--;
  267. if(interface.gears <= 1)
  268. {
  269. interface.gears = 1;
  270. }
  271. }
  272. else if(interface.flg == 1)
  273. {
  274. interface.clean_flg = 1;
  275. interface.sleep_time = 500;
  276. }
  277. else if(interface.flg == 0)
  278. {
  279. interface.sleep_time = 500;
  280. }
  281. }
  282. else
  283. {
  284. myprintf((uint8_t*)"NULL\r\n");
  285. }
  286. }
  287. else
  288. {
  289. myprintf((uint8_t*)"NULL\r\n");
  290. }
  291. uart1.place = 0;
  292. memset(uart1.buf,0,sizeof(uart1.buf));
  293. }
  294. }
  295. }
  296. void led_process(void)
  297. {
  298. uint8_t mode = 0;
  299. if(interface.mode == 0)
  300. {
  301. mode |= 0x80;
  302. }
  303. else
  304. {
  305. mode &= 0x7f;
  306. }
  307. if(interface.gears == 1)
  308. {
  309. mode |= 0x01;
  310. }
  311. else
  312. {
  313. mode &= 0xfe;
  314. }
  315. if(interface.gears == 2)
  316. {
  317. mode |= 0x02;
  318. }
  319. else
  320. {
  321. mode &= 0xfd;
  322. }
  323. if(interface.gears == 3)
  324. {
  325. mode |= 0x04;
  326. }
  327. else
  328. {
  329. mode &= 0xfb;
  330. }
  331. if(uart1.led_flg)
  332. {
  333. mode |= 0x08;
  334. }
  335. else
  336. {
  337. mode &= 0xf7;
  338. }
  339. LED_Disp(mode);
  340. }
  341. /* USER CODE END PM */
  342. /* Private variables ---------------------------------------------------------*/
  343. /* USER CODE BEGIN PV */
  344. /* USER CODE END PV */
  345. /* Private function prototypes -----------------------------------------------*/
  346. void SystemClock_Config(void);
  347. /* USER CODE BEGIN PFP */
  348. /* USER CODE END PFP */
  349. /* Private user code ---------------------------------------------------------*/
  350. /* USER CODE BEGIN 0 */
  351. /* USER CODE END 0 */
  352. /**
  353. * @brief The application entry point.
  354. * @retval int
  355. */
  356. int main(void)
  357. {
  358. /* USER CODE BEGIN 1 */
  359. /* USER CODE END 1 */
  360. /* MCU Configuration--------------------------------------------------------*/
  361. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  362. HAL_Init();
  363. /* USER CODE BEGIN Init */
  364. /* USER CODE END Init */
  365. /* Configure the system clock */
  366. SystemClock_Config();
  367. /* USER CODE BEGIN SysInit */
  368. /* USER CODE END SysInit */
  369. /* Initialize all configured peripherals */
  370. MX_GPIO_Init();
  371. MX_ADC2_Init();
  372. MX_TIM2_Init();
  373. MX_TIM4_Init();
  374. MX_USART1_UART_Init();
  375. /* USER CODE BEGIN 2 */
  376. interface.sleep_time = 500;
  377. interface.mode = 0;
  378. LCD_Init();
  379. LCD_Clear(Black);
  380. LCD_SetBackColor(Black);
  381. LCD_SetTextColor(White);
  382. HAL_TIM_Base_Start_IT(&htim4);
  383. HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_2);
  384. HAL_UART_Receive_IT(&huart1,&uart1.data,1);
  385. /* USER CODE END 2 */
  386. /* Infinite loop */
  387. /* USER CODE BEGIN WHILE */
  388. while (1)
  389. {
  390. /* USER CODE END WHILE */
  391. /* USER CODE BEGIN 3 */
  392. lcd_process();
  393. key_process();
  394. uart_process();
  395. adc_process();
  396. pwm_process();
  397. led_process();
  398. }
  399. /* USER CODE END 3 */
  400. }
  401. /**
  402. * @brief System Clock Configuration
  403. * @retval None
  404. */
  405. void SystemClock_Config(void)
  406. {
  407. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  408. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  409. RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  410. /** Configure the main internal regulator output voltage
  411. */
  412. HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);
  413. /** Initializes the CPU, AHB and APB busses clocks
  414. */
  415. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  416. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  417. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  418. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  419. RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV3;
  420. RCC_OscInitStruct.PLL.PLLN = 20;
  421. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  422. RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
  423. RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
  424. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  425. {
  426. Error_Handler();
  427. }
  428. /** Initializes the CPU, AHB and APB busses clocks
  429. */
  430. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  431. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  432. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  433. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  434. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  435. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  436. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK)
  437. {
  438. Error_Handler();
  439. }
  440. /** Initializes the peripherals clocks
  441. */
  442. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1|RCC_PERIPHCLK_ADC12;
  443. PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
  444. PeriphClkInit.Adc12ClockSelection = RCC_ADC12CLKSOURCE_SYSCLK;
  445. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  446. {
  447. Error_Handler();
  448. }
  449. }
  450. /* USER CODE BEGIN 4 */
  451. /* USER CODE END 4 */
  452. /**
  453. * @brief This function is executed in case of error occurrence.
  454. * @retval None
  455. */
  456. void Error_Handler(void)
  457. {
  458. /* USER CODE BEGIN Error_Handler_Debug */
  459. /* User can add his own implementation to report the HAL error return state */
  460. /* USER CODE END Error_Handler_Debug */
  461. }
  462. #ifdef USE_FULL_ASSERT
  463. /**
  464. * @brief Reports the name of the source file and the source line number
  465. * where the assert_param error has occurred.
  466. * @param file: pointer to the source file name
  467. * @param line: assert_param error line source number
  468. * @retval None
  469. */
  470. void assert_failed(uint8_t *file, uint32_t line)
  471. {
  472. /* USER CODE BEGIN 6 */
  473. /* User can add his own implementation to report the file name and line number,
  474. tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  475. /* USER CODE END 6 */
  476. }
  477. #endif /* USE_FULL_ASSERT */
  478. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/705751
推荐阅读
相关标签
  

闽ICP备14008679号