第十届蓝桥杯嵌入式国赛(STM32G4及HAL库)
Posted ORI2333
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第十届蓝桥杯嵌入式国赛(STM32G4及HAL库)相关的知识,希望对你有一定的参考价值。
前言
本程序基于STM32G431RB开发,使用工具为STM32CubeMX + Keil MDK 5 + HAL库。
题目说明
实现程序
为了方便提交,或者说不容易遗漏,本工程将绝大部分实现代码写在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"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "string.h"
#include "ds18b20.h"
#include "i2c - hal.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
#define RCLK_PIN GPIO_PIN_2
#define SER_PIN GPIO_PIN_1
#define SCK_PIN GPIO_PIN_3
#define RCLK_H HAL_GPIO_WritePin(GPIOA, RCLK_PIN, GPIO_PIN_SET)
#define RCLK_L HAL_GPIO_WritePin(GPIOA, RCLK_PIN, GPIO_PIN_RESET)
#define SER_H HAL_GPIO_WritePin(GPIOA, SER_PIN, GPIO_PIN_SET)
#define SER_L HAL_GPIO_WritePin(GPIOA, SER_PIN, GPIO_PIN_RESET)
#define SCK_H HAL_GPIO_WritePin(GPIOA, SCK_PIN, GPIO_PIN_SET)
#define SCK_L HAL_GPIO_WritePin(GPIOA, SCK_PIN, GPIO_PIN_RESET)
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
ADC_HandleTypeDef hadc2;
TIM_HandleTypeDef htim3;
TIM_HandleTypeDef htim4;
UART_HandleTypeDef huart1;
/* USER CODE BEGIN PV */
char Text_buff[30]; //显示用
double AO1_Val = 0; //AO1电压值
double AO2_Val = 0;
uint8_t PWM2_zkb = 0;
double Temp_Val = 0;
uint16_t G_num = 0;
uint8_t Key1_Flag = 0;
uint8_t Key2_Flag = 0;
uint8_t Key3_Flag = 0;
uint8_t Key4_Flag = 0;
uint16_t IC3ReadValue1 = 0, IC3ReadValue2 = 0;
uint16_t CaptureNumber = 0;
uint32_t TIM3Freq = 0;
double TIM3Duty = 0;
uint32_t Capture_High = 0;
uint32_t Capture_Low = 0;
uint8_t Temp_Para = 30;
uint8_t X_Para = 0;
uint32_t Time = 0;
uint8_t mode = 0;
uc8 Seg7[17] = 0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f,
0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71, 0x00;
uint8_t SEG_Flag = 0;
uint16_t ch = 0;
char rxbuff[30];
char RxBuff[30];
uint8_t Print_Flag = 0;
uint8_t N_Flag = 0;
uint8_t LED_8_Flag = 0;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_ADC2_Init(void);
static void MX_TIM3_Init(void);
static void MX_TIM4_Init(void);
static void MX_USART1_UART_Init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
//获取ADC
double GetADC(uint32_t ch)
ADC_ChannelConfTypeDef sConfig = 0;
sConfig.Channel = ch;
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_247CYCLES_5;
sConfig.SingleDiff = ADC_SINGLE_ENDED;
sConfig.OffsetNumber = ADC_OFFSET_NONE;
sConfig.Offset = 0;
if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK)
Error_Handler();
HAL_ADC_Start(&hadc2);
HAL_ADC_PollForConversion(&hadc2,10);
uint16_t adc = HAL_ADC_GetValue(&hadc2);
return (adc*3.3/4096);
均值滤波
//u16 ADC_Average(u32 ch, u8 times)
//
// u32 temp_val = 0;
// u8 t;
// for(t = 0; t < times; t++)
// temp_val += GetADC(ch);
// HAL_Delay(5);
//
//
// return temp_val/times;
//
//
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
if(htim -> Instance == TIM3)
if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2)
if(CaptureNumber == 0)
IC3ReadValue1 = TIM3->CCR2;
CaptureNumber = 1;
__HAL_TIM_SET_CAPTUREPOLARITY(htim,TIM_CHANNEL_2,TIM_INPUTCHANNELPOLARITY_FALLING);
else if(CaptureNumber == 1)
IC3ReadValue2 = TIM3->CCR2;
CaptureNumber = 2;
__HAL_TIM_SET_CAPTUREPOLARITY(htim,TIM_CHANNEL_2,TIM_INPUTCHANNELPOLARITY_RISING);
if (IC3ReadValue2 > IC3ReadValue1)
Capture_High = (IC3ReadValue2 - IC3ReadValue1);
else
Capture_High = ((0xFFFF - IC3ReadValue1) + IC3ReadValue2);
IC3ReadValue1 = IC3ReadValue2;
else if(CaptureNumber == 2)
IC3ReadValue2 = TIM3->CCR2;
CaptureNumber = 0;
if (IC3ReadValue2 > IC3ReadValue1)
Capture_Low = (IC3ReadValue2 - IC3ReadValue1);
else
Capture_Low = ((0xFFFF - IC3ReadValue1) + IC3ReadValue2);
TIM3Freq = (uint32_t) 1000000 / (Capture_Low + Capture_High);
TIM3Duty = Capture_High * 1.0 / (Capture_Low + Capture_High);
double GetTemp()
u16 read = (ds18b20_read() & 0x07FF);
return (read/16.);
//主界面
void MainDisplay()
//赋值
AO1_Val = GetADC(ADC_CHANNEL_17);
AO2_Val = GetADC(ADC_CHANNEL_13);
PWM2_zkb = TIM3Duty * 100 ;
Temp_Val = GetTemp();
//显示部分
LCD_DisplayStringLine(Line1,(uint8_t *)" Main ");
sprintf(Text_buff," AO1:%.2f V ",AO1_Val);
LCD_DisplayStringLine(Line2,(uint8_t *)Text_buff);
sprintf(Text_buff," AO2:%.2f V ",AO2_Val);
LCD_DisplayStringLine(Line3,(uint8_t *)Text_buff);
sprintf(Text_buff," PWM2:%d %% ",PWM2_zkb);
LCD_DisplayStringLine(Line4,(uint8_t *)Text_buff);
sprintf(Text_buff," Temp:%.2f 'C ",Temp_Val);
LCD_DisplayStringLine(Line5,(uint8_t *)Text_buff);
sprintf(Text_buff," N:%d ",G_num);
LCD_DisplayStringLine(Line7,(uint8_t *)Text_buff);
//参数配置界面
void ParaDisply()
LCD_DisplayStringLine(Line1,(uint8_t *)" Para ");
sprintf(Text_buff," T:%d ",Temp_Para);
if(Key2_Flag == 1)
LCD_SetBackColor(Yellow);
LCD_DisplayStringLine(Line2,(uint8_t *)Text_buff);
LCD_SetBackColor(Blue);
else
LCD_DisplayStringLine(Line2,(uint8_t *)Text_buff);
sprintf(Text_buff," X:AO%d ",X_Para+1);
if(Key2_Flag == 2)
LCD_SetBackColor(Yellow);
LCD_DisplayStringLine(Line3,(uint8_t *)Text_buff);
LCD_SetBackColor(Blue);
else
LCD_DisplayStringLine(Line3,(uint8_t *)Text_buff);
LCD_DisplayStringLine(Line4,(uint8_t *)" ");
LCD_DisplayStringLine(Line5,(uint8_t *)" ");
LCD_DisplayStringLine(Line7,(uint8_t *)" ");
//定时1ms
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
static uint16_t TIM4_Cnt1 = 0;
static uint16_t SEG_Cnt1 = 0;
static uint16_t Print_Cnt1 = 0;
static uint16_t LED_Cnt1 = 0;
if(htim->Instance == TIM4)
TIM4_Cnt1++;
if(TIM4_Cnt1 >= 100)
TIM4_Cnt1 = 0;
SEG_Cnt1++;
Print_Cnt1++;
LED_Cnt1++;
if(LED_Cnt1 >= 8)
LED_Cnt1 = 0;
LED_8_Flag = !LED_8_Flag;
if(Print_Cnt1 >= 10)
Print_Cnt1 = 0;
Print_Flag = 1;
if(SEG_Cnt1 >= 20)
SEG_Cnt1 = 0;
SEG_Flag = !SEG_Flag;
Time++;
void KeyScan()
static u8 key_up=1; //按键松开标志
static u8 key_ups=1;
if(key_ups && (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == RESET
|| HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_2) == RESET ))
Time = 0;
key_ups = 0;
N_Flag = 1;
else if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == SET
&& HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_2) == SET)
key_ups = 1;
if(Time >= 8) mode = 1;
else mode = 0;
if(Time > 5000) Time = 0;
if(mode == 1) key_up = 1;
if(key_up && (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == RESET
|| HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_2) == RESET))
key_up = 0;
if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_2) == RESET)
if(Key2_Flag == 1)
Temp_Para++;
if(Temp_Para > 40)
Temp_Para = 40;
if(Key2_Flag == 2)
X_Para = !X_Para;
if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == RESET)
if(Key2_Flag == 1)
Temp_Para--;
if(Temp_Para < 20)
Temp_Para = 20;
if(Key2_Flag == 2)
X_Para = !X_Para;
else if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == SET
&& HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_2) == SET)
key_up = 1;
void SEG_Display(u8 Bit1, u8 Bit2, u8 Bit3)
u8 i = 0;
u8 code_tmp = 0;
RCLK_L;
code_tmp = Seg7[Bit3];
for(i = 0; i < 8; i++)
SCK_L;
if(code_tmp & 0x80)
SER_H;
else
SER_L;
code_tmp = code_tmp << 1;
SCK_L;
SCK_H;
code_tmp = Seg7[Bit2];
for(i = 0; i < 8; i++)
SCK_L;
if(code_tmp & 0x80)
SER_H;
else
SER_L;
code_tmp = code_tmp << 1;
SCK_L;
SCK_H;
code_tmp = Seg7[Bit1];
for(i = 0; i < 8; i++)
SCK_L;
if(code_tmp & 0x80)
SER_H;
else
SER_L;
code_tmp = code_tmp << 1;
SCK_L;
SCK_H;
RCLK_L;
RCLK_H;
void SEG()
if(SEG_Flag == 0)
u8 SEG_temp_1 = Temp_Para/10;
u8 SEG_temp_2 = Temp_Para%10;
SEG_Display(12, SEG_temp_1, SEG_temp_2);
else
SEG_Display(10, 0, X_Para+1);
void My_Printf()
if(X_Para == 0 && (AO1_Val > (TIM3Duty * 3.3)) )
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15
| GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_8, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_RESET);
if(Print_Flag == 1)
printf("$%.2f\\r\\n",Temp_Val);
Print_Flag = 0;
if(X_Para == 1 && (AO2_Val > (TIM3Duty * 3.3)))
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15
| GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_8, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_RESET);
if( Print_Flag == 1)以上是关于第十届蓝桥杯嵌入式国赛(STM32G4及HAL库)的主要内容,如果未能解决你的问题,请参考以下文章