第十三届蓝桥杯物联网试题(省赛)




做后感悟:

OLED显示函数需要一直显示,所以在主函数中要一直循环,为了确保这个检错功能error只输出一次,最好用中断串口进行接收数据,数据收完后自动进入中断函数中,做一次数据检查就好了,该开灯开灯,检查后还可以更新计数器,要是用普通串口接收的话事情要麻烦的多

代码:

c 复制代码
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart){  // 中断串口
	Function_CheckUrxReceive(URxdata[0]);  //检查更新灯泡状态
	Function_OledShowError(Function_GetCheckflag());  // 显示error
}

void Function_CheckUrxReceive(unsigned char receive){
	if(receive == '@') Function_Ld5Toggle();
	else if(receive == '#') Function_Ld2Toggle();
	else if(receive == '$') Function_Ld3Toggle();
	else {
		checkflag = 0;
		return;
	}
	checkflag = 1;
}

unsigned char Function_GetCheckflag(){
	if(checkflag == 1){
		checkflag = 0;
		Rxnumber = Rxnumber + 1;
	  return 1;
	}
  return 0;
}

需要一个标志位查看是否显示错误信息,还能更新计数器


开中断串口也不难,USART2 异步通信 中断enable就可以了非常的好用

一定一定要注意的一点是串口中断函数内不要放延时函数,谁用谁知道,系统直接卡死!!!

按键:

对于按键来说,我个人感觉中断按键要比ReadPin按键要丝滑,中断按键点一下就有反应,普通按键要按一会儿再松开才行,点太快可能没反应,我建议用中断按键,中断按键需要点击外部中断,再选择中断方式,再enable一下即可


最后对于代码的排版,最好是能将所有代码放入一个.c文件内,全局变量,都写在里面,最后通过一到两个函数给主函数调用

发现了一个有趣的现象,当中断函数中有oledshowstring函数想显示一个字符串,主函数的while函数中也有一个显示函数的时候,这个时候触发中断,中断函数中的显示函数无法被执行,当主函数代码量增多的时候,中断函数的显示函数有可能被执行,我想原因大概是主函数在显示数据的时候写一半突然中断后,进入中断内的显示函数,显示标无法被调整,这可能是OLED函数的代码原因,解决方法,就是主函数的显示函数后加一些延时即可,在延时期间进入中断写字符串,避免主函数正在写字符串,突然被叫到中断中写字符串

问题代码:

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

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

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
unsigned char c[1];
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin){
	   OLED_ShowString(0, 2, "okokokok", 16);
		 char b = HAL_GPIO_ReadPin(KEY_USER_GPIO_Port, KEY_USER_Pin);
		 sprintf((char*)c, "%d",b);
     
	  //if(GPIO_Pin == KEY_USER_Pin)
	    
		//OLED_Clear();
	   
}
/* 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_I2C3_Init();
  /* USER CODE BEGIN 2 */
  Function_OledInit(50);
  /* USER CODE END 2 */
  unsigned char a[1];
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */
    char b = HAL_GPIO_ReadPin(KEY_USER_GPIO_Port, KEY_USER_Pin);
		//sprintf(a, "%d", b);
		OLED_ShowString(0, 0, "123", 16);
		 sprintf(a, "%d", b);
		 //HAL_Delay(1000);
    /* 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_PWR_VOLTAGESCALING_CONFIG(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.PLLMUL = RCC_PLLMUL_4;
  RCC_OscInitStruct.PLL.PLLDIV = RCC_PLLDIV_2;
  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_1) != HAL_OK)
  {
    Error_Handler();
  }
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_I2C3;
  PeriphClkInit.I2c3ClockSelection = RCC_I2C3CLKSOURCE_PCLK1;
  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 */
  __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 */
相关推荐
Hello_Embed4 小时前
STM32HAL 快速入门(二十):UART 中断改进 —— 环形缓冲区解决数据丢失
笔记·stm32·单片机·学习·嵌入式软件
亿坊电商6 小时前
物联网领域中PHP框架的最佳选择有哪些?
物联网·struts·php
御控工业物联网6 小时前
灌区泵站远程监控物联网网关解决方案
物联网·远程监控·物联网网关·泵站·灌区泵站
矢志不移7927 小时前
裸机开发 时钟配置,EPIT
单片机·嵌入式硬件
清风6666667 小时前
基于STM32的APP遥控视频水泵小车设计
stm32·单片机·mongodb·毕业设计·音视频·课程设计
前端小超超8 小时前
capacitor配置ios应用图标不同尺寸
ios·蓝桥杯·cocoa
BAGAE10 小时前
MODBUS 通信协议详细介绍
linux·嵌入式硬件·物联网·硬件架构·iot·嵌入式实时数据库·rtdbs
月阳羊12 小时前
【硬件-笔试面试题-95】硬件/电子工程师,笔试面试题(知识点:RC电路中的时间常数)
java·经验分享·单片机·嵌入式硬件·面试
小莞尔16 小时前
【51单片机】【protues仿真】基于51单片机数控直流稳压电源系统
c语言·stm32·单片机·嵌入式硬件·51单片机
小莞尔16 小时前
【51单片机】【protues仿真】基于51单片机密码锁系统
c语言·stm32·单片机·嵌入式硬件·51单片机