前言
无
一、题目
二、模块初始化
1.LCD这里不用配置,直接使用提供的资源包就行
2.双ADC:开启双ADCsingle-ended
3.LED:开启PC8-15,PD2输出模式就行了。
4.定时器:TIM3(按键消抖定时器):PSC:80-1,ARR:10000-1,TIM17(PWM输出定时器):PSC:0,ARR:100-1
三、代码实现
bsp组中共有:
interrupt.h:
#ifndef __INTERRUPT_H__
#define __INTERRUPT_H__
#include "main.h"
#include "stdbool.h"
struct keys
{
bool key_sta;
unsigned char key_judge;
bool single_flag;
unsigned int key_time;
bool long_flag;
};
#endif
interrupt.c:
#include "interrupt.h"
/* Captured Values */
uint32_t uwIC2Value1_T2CH2 = 0;
uint32_t uwIC2Value2_T2CH2 = 0;
uint32_t uwHighCapture_T2CH2 = 0;
uint32_t uwLowCapture_T2CH2 = 0;
/* Capture index */
uint16_t uhCaptureIndex_T2CH2 = 0;
/* Frequency Value */
uint32_t uwFrequency_T2CH2 = 0;
double uwDuty_T2CH2 = 0;
/* Captured Values */
uint32_t uwIC3Value1_T2CH3 = 0;
uint32_t uwIC3Value2_T2CH3 = 0;
uint32_t uwHighCapture_T2CH3 = 0;
uint32_t uwLowCapture_T2CH3 = 0;
/* Capture index */
uint16_t uhCaptureIndex_T2CH3 = 0;
/* Frequency Value */
uint32_t uwFrequency_T2CH3 = 0;
double uwDuty_T2CH3 = 0;
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance == TIM2)
{
if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2)
{
if(uhCaptureIndex_T2CH2 == 0)
{
/* Get the 1st Input Capture value */
uwIC2Value1_T2CH2 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2);
__HAL_TIM_SET_CAPTUREPOLARITY(htim, TIM_CHANNEL_2, TIM_INPUTCHANNELPOLARITY_FALLING);
uhCaptureIndex_T2CH2 = 1;
}
else if(uhCaptureIndex_T2CH2 == 1)
{
/* Get the 2nd Input Capture value */
uwIC2Value2_T2CH2 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2);
__HAL_TIM_SET_CAPTUREPOLARITY(htim, TIM_CHANNEL_2, TIM_INPUTCHANNELPOLARITY_RISING);
/* Capture computation */
if (uwIC2Value2_T2CH2 > uwIC2Value1_T2CH2)
{
uwHighCapture_T2CH2 = (uwIC2Value2_T2CH2 - uwIC2Value1_T2CH2);
}
else if (uwIC2Value2_T2CH2 < uwIC2Value1_T2CH2)
{
/* 0xFFFF is max TIM1_CCRx value */
uwHighCapture_T2CH2 = ((0xFFFFFFFF - uwIC2Value1_T2CH2) + uwIC2Value2_T2CH2) + 1;
}
else
{
/* If capture values are equal, we have reached the limit of frequency
measures */
Error_Handler();
}
uhCaptureIndex_T2CH2 = 2;
uwIC2Value1_T2CH2 = uwIC2Value2_T2CH2;
/* Frequency computation: for this example TIMx (TIM1) is clocked by
APB2Clk */
}
else if(uhCaptureIndex_T2CH2 == 2)
{
uwIC2Value2_T2CH2 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2);
if (uwIC2Value2_T2CH2 > uwIC2Value1_T2CH2)
{
uwLowCapture_T2CH2 = (uwIC2Value2_T2CH2 - uwIC2Value1_T2CH2);
}
else if (uwIC2Value2_T2CH2 < uwIC2Value1_T2CH2)
{
/* 0xFFFF is max TIM1_CCRx value */
uwLowCapture_T2CH2 = ((0xFFFFFFFF - uwIC2Value1_T2CH2) + uwIC2Value2_T2CH2) + 1;
}
uwFrequency_T2CH2 = 1000000 / (uwLowCapture_T2CH2 + uwHighCapture_T2CH2);
uwDuty_T2CH2 = uwHighCapture_T2CH2 * 100.0 / (uwLowCapture_T2CH2 + uwHighCapture_T2CH2);
uhCaptureIndex_T2CH2 = 0;
}
}
if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_3)
{
if(uhCaptureIndex_T2CH3 == 0)
{
/* Get the 1st Input Capture value */
uwIC3Value1_T2CH3 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_3);
__HAL_TIM_SET_CAPTUREPOLARITY(htim, TIM_CHANNEL_3, TIM_INPUTCHANNELPOLARITY_FALLING);
uhCaptureIndex_T2CH3 = 1;
}
else if(uhCaptureIndex_T2CH3 == 1)
{
/* Get the 2nd Input Capture value */
uwIC3Value2_T2CH3 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_3);
__HAL_TIM_SET_CAPTUREPOLARITY(htim, TIM_CHANNEL_3, TIM_INPUTCHANNELPOLARITY_RISING);
/* Capture computation */
if (uwIC3Value2_T2CH3 > uwIC3Value1_T2CH3)
{
uwHighCapture_T2CH3 = (uwIC3Value2_T2CH3 - uwIC3Value1_T2CH3);
}
else if (uwIC3Value2_T2CH3 < uwIC3Value1_T2CH3)
{
/* 0xFFFF is max TIM1_CCRx value */
uwHighCapture_T2CH3 = ((0xFFFFFFFF - uwIC3Value1_T2CH3) + uwIC3Value2_T2CH3) + 1;
}
else
{
/* If capture values are equal, we have reached the limit of frequency
measures */
Error_Handler();
}
uhCaptureIndex_T2CH3 = 2;
uwIC3Value1_T2CH3 = uwIC3Value2_T2CH3;
/* Frequency computation: for this example TIMx (TIM1) is clocked by
APB2Clk */
}
else if(uhCaptureIndex_T2CH3 == 2)
{
uwIC3Value2_T2CH3 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_3);
if (uwIC3Value2_T2CH3 > uwIC3Value1_T2CH3)
{
uwLowCapture_T2CH3 = (uwIC3Value2_T2CH3 - uwIC3Value1_T2CH3);
}
else if (uwIC3Value2_T2CH3 < uwIC3Value1_T2CH3)
{
/* 0xFFFF is max TIM1_CCRx value */
uwLowCapture_T2CH3 = ((0xFFFFFFFF - uwIC3Value1_T2CH3) + uwIC3Value2_T2CH3) + 1;
}
uwFrequency_T2CH3 = 1000000 / (uwLowCapture_T2CH3 + uwHighCapture_T2CH3);
uwDuty_T2CH3 = uwHighCapture_T2CH3 * 100.0 / (uwLowCapture_T2CH3 + uwHighCapture_T2CH3);
uhCaptureIndex_T2CH3 = 0;
}
}
}
}
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].key_judge)
{
case 0:
{
if(key[i].key_sta == 0)
{
key[i].key_judge = 1;
key[i].key_time = 0;
}
break;
}
case 1:
{
if(key[i].key_sta == 0)
{
key[i].key_judge = 2;
}
else
{
key[i].key_judge = 0;
}
break;
}
case 2:
{
if(key[i].key_sta == 1)
{
key[i].key_judge = 0;
if(key[i].key_time < 80)
{
key[i].single_flag = 1;
}
}
else
{
key[i].key_time++;
if(key[i].key_time >= 80)
{
key[i].long_flag = 1;
}
}
break;
}
}
}
}
}
main.h:
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.h
* @brief : Header for main.c file.
* This file contains the common defines of the application.
******************************************************************************
* @attention
*
* Copyright (c) 2023 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 */
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __MAIN_H
#define __MAIN_H
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#include "stm32g4xx_hal.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Exported types ------------------------------------------------------------*/
/* USER CODE BEGIN ET */
/* USER CODE END ET */
/* Exported constants --------------------------------------------------------*/
/* USER CODE BEGIN EC */
/* USER CODE END EC */
/* Exported macro ------------------------------------------------------------*/
/* USER CODE BEGIN EM */
/* USER CODE END EM */
/* Exported functions prototypes ---------------------------------------------*/
void Error_Handler(void);
/* USER CODE BEGIN EFP */
/* USER CODE END EFP */
/* Private defines -----------------------------------------------------------*/
/* USER CODE BEGIN Private defines */
#define DATA 0
#define PARA 1
#define LD1 0
#define LD2 1
#define LD3 2
#define LD4 3
#define LD5 4
#define LD6 5
#define LD7 6
#define LD8 7
#define PULS1 0
#define PULS2 1
/* USER CODE END Private defines */
#ifdef __cplusplus
}
#endif
#endif /* __MAIN_H */
main.c:
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2023 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 "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "interrupt.h"
#include "lcd.h"
#include "stdio.h"
#include "dadc.h"
#include "led.h"
/* 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 */
extern uint32_t uwFrequency_T2CH2;
extern double uwDuty_T2CH2;
extern uint32_t uwFrequency_T2CH3;
extern double uwDuty_T2CH3;
char text[30];
extern struct keys key[4];
float volt_AO1;
float volt_AO2;
unsigned int FrequencyOut;
unsigned char DisplayMode;
unsigned char voltLD = LD1;
unsigned char FreLD = LD2;
unsigned char tempvoltLD = LD1;
unsigned char tempFreLD = LD2;
unsigned char outMode;
unsigned char LED;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
void DisposeKey(void);
void LCD_Disp(void);
void LED_Control(void);
/* 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_ADC2_Init();
MX_TIM2_Init();
MX_TIM3_Init();
MX_TIM17_Init();
/* USER CODE BEGIN 2 */
HAL_TIM_IC_Start_IT(&htim2, TIM_CHANNEL_2);
HAL_TIM_IC_Start_IT(&htim2, TIM_CHANNEL_3);
HAL_TIM_Base_Start_IT(&htim3);
getDualADC(&hadc2);
HAL_Delay(2);
getDualADC(&hadc2);
LCD_Init();
LCD_Clear(Black);
LCD_SetBackColor(Black);
LCD_SetTextColor(White);
if(outMode == PULS1)
{
FrequencyOut = uwFrequency_T2CH2;
}
else if(outMode == PULS2)
{
FrequencyOut = uwFrequency_T2CH3;
}
__HAL_TIM_SET_PRESCALER(&htim17, 80000000 / 100 / FrequencyOut);
HAL_TIM_PWM_Start(&htim17, TIM_CHANNEL_1);
LED_Disp(0x00);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
getDualADC(&hadc2);
volt_AO1 = adc2_in17_AO1 * 3.3 / 4096;
volt_AO2 = adc2_in13_AO2 * 3.3 / 4096;
DisposeKey();
if(outMode == PULS1)
{
FrequencyOut = uwFrequency_T2CH2;
}
else if(outMode == PULS2)
{
FrequencyOut = uwFrequency_T2CH3;
}
__HAL_TIM_SET_PRESCALER(&htim17, 80000000 / 100 / FrequencyOut);
LED_Control();
LED_Disp(LED);
LCD_Disp();
}
/* 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 */
void DisposeKey(void)
{
if(key[0].single_flag)
{
LCD_Clear(Black);
if(DisplayMode == DATA)
{
DisplayMode = PARA;
}
else if(DisplayMode == PARA)
{
DisplayMode = DATA;
LED = LED & ~(0X01 << voltLD);
LED = LED & ~(0X01 << FreLD);
FreLD = tempFreLD;
voltLD = tempvoltLD;
}
key[0].single_flag = 0;
}
if(key[1].single_flag)
{
if(DisplayMode == PARA)
{
tempvoltLD++;
tempvoltLD %= 8;
if(tempFreLD == tempvoltLD)
tempvoltLD++;
tempvoltLD %= 8;
}
key[1].single_flag = 0;
}
if(key[2].single_flag)
{
if(DisplayMode == PARA)
{
tempFreLD++;
tempFreLD %= 8;
if(tempFreLD == tempvoltLD)
tempFreLD++;
tempFreLD %= 8;
}
key[2].single_flag = 0;
}
if(key[3].single_flag)
{
outMode = !outMode;
key[3].single_flag = 0;
}
}
void LCD_Disp(void)
{
if(DisplayMode == DATA)
{
LCD_DisplayStringLine(Line1, " DATA");
sprintf(text, " V1:%.2fV", volt_AO1);
LCD_DisplayStringLine(Line3, text);
sprintf(text, " V2:%.2fV", volt_AO2);
LCD_DisplayStringLine(Line4, text);
sprintf(text, " F1:%dHz ", uwFrequency_T2CH2);
LCD_DisplayStringLine(Line5, text);
sprintf(text, " F2:%dHz ", uwFrequency_T2CH3);
LCD_DisplayStringLine(Line6, text);
}
if(DisplayMode == PARA)
{
LCD_DisplayStringLine(Line1, " PARA");
sprintf(text, " VD:LD%d", tempvoltLD + 1);
LCD_DisplayStringLine(Line3, text);
sprintf(text, " FD:LD%d", tempFreLD + 1);
LCD_DisplayStringLine(Line4, text);
}
}
void LED_Control(void)
{
if(volt_AO1 > volt_AO2)
{
LED = LED & ~(0X01 << voltLD) | (0x01 << voltLD);
}
else
{
LED = LED & ~(0X01 << voltLD);
}
if(uwFrequency_T2CH2 > uwFrequency_T2CH3)
{
LED = LED & ~(0X01 << FreLD) | (0x01 << FreLD);
}
else
{
LED = LED & ~(0X01 << FreLD);
}
}
/* 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 */
四、完成效果
蓝桥杯嵌入式第十一届国赛试题实现效果
五、总结
本篇文章只是为了存放我的代码,所以看不懂很正常,如果需要代码可以找我私信。