第六届蓝桥杯嵌入式省赛程序设计题(HAL库版)
第九届蓝桥杯嵌入式省赛程序设计题(HAL库版)
第十届蓝桥杯嵌入式省赛程序设计题(HAL库版)
第十一届蓝桥杯嵌入式省赛程序设计题(HAL库版)
明天就要正式省赛了,今天在做一套简单的模拟题练练手!
需要其他届题目的朋友可以从我上面的目录里直达喔!
配置工程这一步省略过去了,需要看的小伙伴可以去我之前的文章看一下嘿嘿(主要是备赛比较紧张,想去再多做点东西增加熟练度)
在原来的基础上又添加了EEPROM的读写,练个手,不需要的小伙伴可以在代码中删掉
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2022 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 "lcd.h"
#include "i2c_hal.h"
#include "stdio.h"
#include "string.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 */
#define ON GPIO_PIN_RESET
#define OFF GPIO_PIN_SET
#define LED1(a) HAL_GPIO_WritePin(GPIOC,GPIO_PIN_8,a)
#define LED2(a) HAL_GPIO_WritePin(GPIOC,GPIO_PIN_9,a)
#define LED3(a) HAL_GPIO_WritePin(GPIOC,GPIO_PIN_10,a)
#define LED4(a) HAL_GPIO_WritePin(GPIOC,GPIO_PIN_11,a)
#define LED5(a) HAL_GPIO_WritePin(GPIOC,GPIO_PIN_12,a)
#define LED6(a) HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,a)
#define LED7(a) HAL_GPIO_WritePin(GPIOC,GPIO_PIN_14,a)
#define LED8(a) HAL_GPIO_WritePin(GPIOC,GPIO_PIN_15,a)
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
ADC_HandleTypeDef hadc1;
ADC_HandleTypeDef hadc2;
TIM_HandleTypeDef htim2;
/* USER CODE BEGIN PV */
uint8_t LEDCLOSE[2] = {1,1};
uint8_t KEYB1_FLAG = 0;
uint8_t KEYB2_FLAG = 0;
uint8_t KEYB3_FLAG = 0;
uint8_t Period_Flag1;
uint8_t Period_Flag2 = 0;
uint16_t Period = 100;
char EEPROM_Buf[30];
char EEPROM_Buf1[30];
char adc1_R38_Buf[30];
char adc2_R37_Buf[30];
char VP1_Buf[30];
char VP2_Buf[30];
float VP1 = 2.4;
float VP2 = 2.4;
float VR37;
float VR38;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_ADC1_Init(void);
static void MX_ADC2_Init(void);
static void MX_TIM2_Init(void);
/* USER CODE BEGIN PFP */
void LED_ALLClose(uint8_t *LEDCLOSE);
void I2C_EEPROM_WRITE(uint8_t add,uint8_t *WriteBuf,uint16_t num);
void I2C_EEPROM_READ(uint8_t add,uint8_t *ReadBuf,uint16_t num);
uint8_t KEY_Scan(GPIO_TypeDef *GPIOx,uint16_t GPIO_Pin);
double GET_ADC_R37(void);
double GET_ADC_R38(void);
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void I2C_EEPROM_WRITE(uint8_t add,uint8_t *WriteBuf,uint16_t num)
{
I2CStart();
I2CSendByte(0xa0);
I2CWaitAck();
I2CSendByte(add);
I2CWaitAck();
while(num--)
{
I2CSendByte(*WriteBuf);
WriteBuf++;
I2CWaitAck();
}
I2CStop();
}
void I2C_EEPROM_READ(uint8_t add,uint8_t *ReadBuf,uint16_t num)
{
I2CStart();
I2CSendByte(0xa0);
I2CWaitAck();
I2CSendByte(add);
I2CWaitAck();
I2CStart();
I2CSendByte(0xa1);
I2CWaitAck();
while(num--)
{
*ReadBuf = I2CReceiveByte();
ReadBuf++;
I2CWaitAck();
}
I2CStop();
}
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
/* Prevent unused argument(s) compilation warning */
UNUSED(GPIO_Pin);
/* NOTE: This function should not be modified, when the callback is needed,
the HAL_GPIO_EXTI_Callback could be implemented in the user file
*/
if(GPIO_Pin == GPIO_PIN_1) //B2
{
VP1 += 0.3f;
if(VP1 > 3.3f)
VP1 = 0;
Period_Flag2 ++;
I2C_EEPROM_WRITE(0x08,&Period_Flag2,sizeof(Period_Flag2));
HAL_Delay(5);
sprintf(EEPROM_Buf1,"%d",Period_Flag2);
LCD_DisplayStringLine(Line9,(uint8_t*)EEPROM_Buf1);
}
__HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_1);
}
uint8_t KEY_Scan(GPIO_TypeDef *GPIOx,uint16_t GPIO_Pin) //按下返回1,反之返回0
{
if(HAL_GPIO_ReadPin(GPIOx,GPIO_Pin) == RESET)
{
while(HAL_GPIO_ReadPin(GPIOx,GPIO_Pin)==RESET);
return 1;
}
else
return 0;
}
double GET_ADC_R37(void)
{
double adc2;
HAL_ADC_Start(&hadc2);
adc2 = HAL_ADC_GetValue(&hadc2);
return (adc2*3.3/4096);
}
double GET_ADC_R38(void)
{
double adc1;
HAL_ADC_Start(&hadc1);
adc1 = HAL_ADC_GetValue(&hadc1);
return (adc1*3.3/4096);
}
void LED_ALLClose(uint8_t *LEDCLOSE)
{
uint8_t i;
LED1(ON);
LED2(ON);
for(i=0;i<2;i++)
{
if(LEDCLOSE[i])
switch(i)
{
case 0:
LED1(OFF);
break;
case 1:
LED2(OFF);
break;
}
}
LED3(OFF);
LED4(OFF);
LED5(OFF);
LED6(OFF);
LED7(OFF);
LED8(OFF);
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);
}
/* 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_TIM2_Init();
/* USER CODE BEGIN 2 */
I2CInit();
LCD_Init();
LCD_SetBackColor(Black);
LCD_SetTextColor(White);
LCD_Clear(Black);
HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_2);
I2C_EEPROM_READ(0x00,&Period_Flag1,sizeof(Period_Flag1));
HAL_Delay(5);
I2C_EEPROM_READ(0x08,&Period_Flag2,sizeof(Period_Flag2));
HAL_Delay(5);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
//显示界面
if(KEYB1_FLAG == 0)
{
LCD_DisplayStringLine(Line1,(uint8_t*)" DATA ");
VR37 = GET_ADC_R37();
VR38 = GET_ADC_R38();
sprintf(adc2_R37_Buf," VR37:%.2fV",VR37);
LCD_DisplayStringLine(Line4,(uint8_t*)adc2_R37_Buf);
sprintf(adc1_R38_Buf," VR38:%.2fV",VR38);
LCD_DisplayStringLine(Line6,(uint8_t*)adc1_R38_Buf);
}
//Setting Interface
if(KEYB1_FLAG == 1)
{
LCD_DisplayStringLine(Line1,(uint8_t*)" PARA ");
sprintf(VP1_Buf," VP1:%.1fV",VP1);
LCD_DisplayStringLine(Line4,(uint8_t*)VP1_Buf);
if(KEY_Scan(B3_GPIO_Port,B3_Pin) == 1)
{
VP2 += 0.3f;
Period_Flag1 = 1;
I2C_EEPROM_WRITE(0x00,&Period_Flag1,sizeof(Period_Flag1));
HAL_Delay(5);
}
if(VP2 > 3.3f)
{
VP2 = 0.0;
Period_Flag1 = 2;
I2C_EEPROM_WRITE(0x00,&Period_Flag1,sizeof(Period_Flag1));
HAL_Delay(5);
}
sprintf(VP2_Buf," VP2:%.1fV",VP2);
LCD_DisplayStringLine(Line6,(uint8_t*)VP2_Buf);
sprintf(EEPROM_Buf," %d",Period_Flag1);
LCD_DisplayStringLine(Line9,(uint8_t*)EEPROM_Buf);
}
if(VR37 > VP1)
LEDCLOSE[0] = 0;
else
LEDCLOSE[0] = 1;
if(VR38 > VP2)
LEDCLOSE[1] = 0;
else
LEDCLOSE[1] = 1;
LED_ALLClose(LEDCLOSE);
//PA1 PWM输出
if(VR37>VR38) //10KHz
{
Period = 100;
}
else //200Hz
{
Period = 5000;
}
__HAL_TIM_SetAutoreload(&htim2,Period);
if(KEY_Scan(B1_GPIO_Port,B1_Pin) == 1) //B1按下
{
KEYB1_FLAG ++;
LCD_Clear(Black);
}
if(KEYB1_FLAG >=2)
KEYB1_FLAG = 0;
}
/* 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();
}
}
/**
* @brief ADC1 Initialization Function
* @param None
* @retval None
*/
static void MX_ADC1_Init(void)
{
/* USER CODE BEGIN ADC1_Init 0 */
/* USER CODE END ADC1_Init 0 */
ADC_MultiModeTypeDef multimode = {0};
ADC_ChannelConfTypeDef sConfig = {0};
/* USER CODE BEGIN ADC1_Init 1 */
/* USER CODE END ADC1_Init 1 */
/** Common config
*/
hadc1.Instance = ADC1;
hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
hadc1.Init.Resolution = ADC_RESOLUTION_12B;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.GainCompensation = 0;
hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
hadc1.Init.LowPowerAutoWait = DISABLE;
hadc1.Init.ContinuousConvMode = DISABLE;
hadc1.Init.NbrOfConversion = 1;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc1.Init.DMAContinuousRequests = DISABLE;
hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;
hadc1.Init.OversamplingMode = DISABLE;
if (HAL_ADC_Init(&hadc1) != HAL_OK)
{
Error_Handler();
}
/** Configure the ADC multi-mode
*/
multimode.Mode = ADC_MODE_INDEPENDENT;
if (HAL_ADCEx_MultiModeConfigChannel(&hadc1, &multimode) != HAL_OK)
{
Error_Handler();
}
/** Configure Regular Channel
*/
sConfig.Channel = ADC_CHANNEL_11;
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_2CYCLES_5;
sConfig.SingleDiff = ADC_SINGLE_ENDED;
sConfig.OffsetNumber = ADC_OFFSET_NONE;
sConfig.Offset = 0;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN ADC1_Init 2 */
/* USER CODE END ADC1_Init 2 */
}
/**
* @brief ADC2 Initialization Function
* @param None
* @retval None
*/
static void MX_ADC2_Init(void)
{
/* USER CODE BEGIN ADC2_Init 0 */
/* USER CODE END ADC2_Init 0 */
ADC_ChannelConfTypeDef sConfig = {0};
/* USER CODE BEGIN ADC2_Init 1 */
/* USER CODE END ADC2_Init 1 */
/** Common config
*/
hadc2.Instance = ADC2;
hadc2.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
hadc2.Init.Resolution = ADC_RESOLUTION_12B;
hadc2.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc2.Init.GainCompensation = 0;
hadc2.Init.ScanConvMode = ADC_SCAN_DISABLE;
hadc2.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
hadc2.Init.LowPowerAutoWait = DISABLE;
hadc2.Init.ContinuousConvMode = DISABLE;
hadc2.Init.NbrOfConversion = 1;
hadc2.Init.DiscontinuousConvMode = DISABLE;
hadc2.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc2.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc2.Init.DMAContinuousRequests = DISABLE;
hadc2.Init.Overrun = ADC_OVR_DATA_PRESERVED;
hadc2.Init.OversamplingMode = DISABLE;
if (HAL_ADC_Init(&hadc2) != HAL_OK)
{
Error_Handler();
}
/** Configure Regular Channel
*/
sConfig.Channel = ADC_CHANNEL_15;
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_2CYCLES_5;
sConfig.SingleDiff = ADC_SINGLE_ENDED;
sConfig.OffsetNumber = ADC_OFFSET_NONE;
sConfig.Offset = 0;
if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN ADC2_Init 2 */
/* USER CODE END ADC2_Init 2 */
}
/**
* @brief TIM2 Initialization Function
* @param None
* @retval None
*/
static void MX_TIM2_Init(void)
{
/* USER CODE BEGIN TIM2_Init 0 */
/* USER CODE END TIM2_Init 0 */
TIM_MasterConfigTypeDef sMasterConfig = {0};
TIM_OC_InitTypeDef sConfigOC = {0};
/* USER CODE BEGIN TIM2_Init 1 */
/* USER CODE END TIM2_Init 1 */
htim2.Instance = TIM2;
htim2.Init.Prescaler = 80-1;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = 100-1;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_PWM_Init(&htim2) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 50;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM2_Init 2 */
/* USER CODE END TIM2_Init 2 */
HAL_TIM_MspPostInit(&htim2);
}
/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOF_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/*Configure GPIO pin Output Level */
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);
/*Configure GPIO pins : PC13 PC14 PC15 PC8
PC9 PC10 PC11 PC12 */
GPIO_InitStruct.Pin = 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_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
/*Configure GPIO pins : PD2 */
GPIO_InitStruct.Pin = GPIO_PIN_2;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
/*Configure GPIO pins : PB0 PB2 */
GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_2;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/*Configure GPIO pin : PB1 */
GPIO_InitStruct.Pin = GPIO_PIN_1;
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* EXTI interrupt init*/
HAL_NVIC_SetPriority(EXTI1_IRQn, 2, 0);
HAL_NVIC_EnableIRQ(EXTI1_IRQn);
}
/* 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 */
LED和LCD的冲突可以看一下这位大佬博主写的这一篇文章!非常有用!!!
STM32LED–基于HAL库(LCD与LED冲突?一文看懂如何精准劝架)
这位博主还有很多别的资源!大家有需要的可以看看哦!