蓝桥杯嵌入式十五届模拟三程序题

这篇文章详细展示了如何在CSDN的蓝桥杯嵌入式教程中,通过C语言实现设备控制、中断管理、ADC和DMA数据采集、LCD显示以及按键事件处理,包括LED状态和合格率计算,适用于初学者学习嵌入式开发。
摘要由CSDN通过智能技术生成

新建工程参照:零基础蓝桥杯嵌入式教程_csdn 蓝桥杯嵌入式教程-CSDN博客

主要代码

my_main.h

#ifndef _MY_MAIN_H_
#define _MY_MAIN_H_

#include "main.h"
#include "lcd.h"
#include "stdbool.h"
#include "string.h"
#include "stdio.h"

#include "adc.h"
#include "dma.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"


void setup(void);
void loop(void);
void led_proce(void);
void key_proce(void);
void led_disp(uint num,bool on_off);
void LED_proce(void);

#endif

my_main.c

#include "my_main.h"

typedef struct keys
{
	bool shortdown;
	bool longdown;
	uint8_t times;
	bool status;
}KEY;
KEY key[4];

char text[30];
uint8_t view=1;//显示界面标志位

double SR37_l=1.2;
double SR37_h=2.2;
double SR38_l=1.4;
double SR38_h=3.0;//标准参数

uint8_t pass37=0;
uint8_t all37=0;
uint8_t pass38=0;
uint8_t all38=0;
double PR37=0;
double PR38=0;//合格数和总数和合格率

uint16_t adc_buf1[2];
uint16_t adc_buf2[2];//DMA缓冲区

uint8_t choice=1;//标准调整选择标志位

bool led1=0;
bool led2=0;
bool led1_flag=0;
bool led2_flag=0;

char uart_tx[30];
char uart_rx[30];
char text2[5];
//led
void lcd_proce(void)
{
	if(view==1)
	{
		sprintf(text,"       GOODS     ");
		LCD_DisplayStringLine(Line1,(uint8_t *)text);
		sprintf(text,"     R37:%.2fV      ",adc_buf2[0]*3.3f/4096.0f);
		LCD_DisplayStringLine(Line3,(uint8_t *)text);
		sprintf(text,"     R38:%.2fV      ",adc_buf1[0]*3.3f/4096.0f);
		LCD_DisplayStringLine(Line4,(uint8_t *)text);
	}
	if(view==2)
	{
		sprintf(text,"      STANDARD     ");
		LCD_DisplayStringLine(Line1,(uint8_t *)text);
		sprintf(text,"    SR37:%.1f-%.1f      ",SR37_l,SR37_h);
		LCD_DisplayStringLine(Line3,(uint8_t *)text);
		sprintf(text,"    SR38:%.1f-%.1f      ",SR38_l,SR38_h);
		LCD_DisplayStringLine(Line4,(uint8_t *)text);
	}
	if(view==3)
	{
		sprintf(text,"        PASS     ");
		LCD_DisplayStringLine(Line1,(uint8_t *)text);
		sprintf(text,"     PR37:%.1f%%        ",PR37);
		LCD_DisplayStringLine(Line3,(uint8_t *)text);
		sprintf(text,"     PR38:%.1f%%        ",PR38);
		LCD_DisplayStringLine(Line4,(uint8_t *)text);
	}
}
//key
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
	if(htim->Instance==TIM4)
	{
		key[0].status=HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0);
		key[1].status=HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_1);
		key[2].status=HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_2);
		key[3].status=HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0);
	}
	for(int i=0;i<4;i++)
	{
		if(key[i].status==0)
		{
			key[i].times++;
		}
		if(key[i].status==1)
		{
			if(key[i].times>4) key[i].shortdown=1;
//			if(key[i].times>=40) key[i].longdown=1;
			key[i].times=0;
		}
	}
}

void key_proce(void)
{
double R37_V=adc_buf2[0]*3.3f/4096.0f;
double R38_V=adc_buf1[0]*3.3f/4096.0f;
	if(key[0].shortdown==1)//按键1功能
	{
		key[0].shortdown=0;
		if(view<3)//界面切换
		{
			view++;
		}
		else 
			{
				view=1;
			}
		
//		if(view==2)
//		{
//			choice=1;//每次进入标准设置界面,默认调整第一个参数
//		}
	}
	if(key[1].shortdown==1)//按键2功能
	{
		key[1].shortdown=0;
		if(view==1)//参数1界面,判断R37是否合格
		{
			if(SR37_l<=R37_V&&R37_V<=SR37_h)
			{
				pass37++;
				all37++;
				led1=1;//进入1S定时
			}else {all37++;}
		}
		if(view==2)//标准2界面,改变选择参数的标志位
		{
			if(choice<4)
			{
				choice++;
			}else choice=1;
		}
	}
	if(key[2].shortdown==1)//按键3功能
	{
		key[2].shortdown=0;
		if(view==1)//参数1界面,判断R38是否合格
		{
			if(SR38_l<=R38_V&&R38_V<=SR38_h)
			{
				pass38++;
				all38++;
				led2=1;//进入1S定时
			}else {all38++;}
		}
		if(view==2)//调整参数加
		{
			if(choice==1)
			{
				if(SR37_h<3.0)
				{
					SR37_h=SR37_h+0.2;
				}else SR37_h=2.2;
				pass37=0;
				all37=0;
			}
			if(choice==2)
			{
				if(SR37_l<2.0)
				{
					SR37_l=SR37_l+0.2;
				}else SR37_l=1.2;
				pass37=0;
				all37=0;
			}
			if(choice==3)
			{
				if(SR38_h<3.0)
				{
					SR38_h=SR38_h+0.2;
				}else SR38_h=2.2;
				pass38=0;
				all38=0;
			}
			if(choice==4)
			{
				if(SR38_l<2.0)
				{
					SR38_l=SR38_l+0.2;
				}else SR38_l=1.2;
				pass38=0;
				all38=0;
			}
		}
	}
	if(key[3].shortdown==1)
	{
		key[3].shortdown=0;
		if(view==3)//清零
		{
			pass37=0;
			all37=0;
			pass38=0;
			all38=0;
		}
		if(view==2)//调整参数减
		{
			if(choice==1)
			{
				if(SR37_h>2.2)
				{
					SR37_h=SR37_h-0.2;
				}else SR37_h=3.0;
				pass37=0;
				all37=0;
			}
			if(choice==2)
			{
				if(SR37_l>1.2)
				{
					SR37_l=SR37_l-0.2;
				}else SR37_l=2.0;
				pass37=0;
				all37=0;
			}
			if(choice==3)
			{
				if(SR38_h>2.2)
				{
					SR38_h=SR38_h-0.2;
				}else SR38_h=3.0;
				pass38=0;
				all38=0;
			}
			if(choice==4)
			{
				if(SR38_l>1.2)
				{
					SR38_l=SR38_l-0.2;
				}else SR38_l=2.0;
				pass38=0;
				all38=0;
			}
		}
	}
}
/led
void led_disp(uint num,bool on_off)
{
	static uint8_t mmry=0x00;
	if(on_off==1) mmry|=num;
	if(on_off==0) mmry&=~num;
	HAL_GPIO_WritePin(GPIOC,GPIO_PIN_All,GPIO_PIN_SET);
	HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);
	HAL_GPIO_WritePin(GPIOC,mmry<<8,GPIO_PIN_RESET);
	HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);
}

void led_proce(void)
{
	led_disp(1,led1_flag);
	led_disp(2,led2_flag);
	if(view==1)
	{
		led_disp(4,1);
	}else led_disp(4,0);//不符合要灭灯
	
	if(view==2)
	{
		led_disp(8,1);
	}else led_disp(8,0);
	
	if(view==3)
	{
		led_disp(16,1);
	}else led_disp(16,0);
}
//uart
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
{
	sscanf(uart_rx,"%3s",text2);
	if(text2[0]=='R'&&text2[1]=='3'&&text2[2]=='7')
	{
		sprintf(uart_tx,"R37:%d,%d,%.1f%%",all37,pass37,PR37);
		HAL_UART_Transmit(&huart1,(uint8_t *)uart_tx,strlen(uart_tx),50);
		HAL_UARTEx_ReceiveToIdle_IT(&huart1,(uint8_t *)uart_rx,50);
	}
	if(text2[0]=='R'&&text2[1]=='3'&&text2[2]=='8')
	{
		sprintf(uart_tx,"R38:%d,%d,%.1f%%",all38,pass38,PR38);
		HAL_UART_Transmit(&huart1,(uint8_t *)uart_tx,strlen(uart_tx),50);
		HAL_UARTEx_ReceiveToIdle_IT(&huart1,(uint8_t *)uart_rx,50);
	}
}

/
void setup(void)
{
	HAL_TIM_Base_Start_IT(&htim4);
	
	LCD_Init();
	LCD_Clear(Black);
	LCD_SetBackColor(Black);
	LCD_SetTextColor(White);
	
	HAL_ADC_Start_DMA(&hadc1,(uint32_t *)adc_buf1,2);
	HAL_ADC_Start_DMA(&hadc2,(uint32_t *)adc_buf2,2);
	HAL_ADCEx_Calibration_Start(&hadc2,ADC_SINGLE_ENDED);
	HAL_ADCEx_Calibration_Start(&hadc1,ADC_SINGLE_ENDED);
	
	HAL_UARTEx_ReceiveToIdle_IT(&huart1,(uint8_t *)uart_rx,50);
	
}

void loop(void)
{
		if(pass37==0)
		{
			PR37=0;
		}
			else PR37=pass37*100.0f/all37;
		if(pass38==0)
		{
			PR38=0;
		}
			else PR38=pass38*100.0f/all38;
	lcd_proce();
	key_proce();
	led_proce();
}


sysytick

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file    stm32g4xx_it.c
  * @brief   Interrupt Service Routines.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2024 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"
#include "stm32g4xx_it.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "my_main.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN TD */
extern bool led1;
extern bool led2;
extern bool led1_flag;
extern bool led2_flag;
uint16_t time1=0;
uint16_t time2=0;
/* USER CODE END TD */

/* 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 -----------------------------------------------*/
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/* External variables --------------------------------------------------------*/
extern DMA_HandleTypeDef hdma_adc1;
extern DMA_HandleTypeDef hdma_adc2;
extern TIM_HandleTypeDef htim4;
extern UART_HandleTypeDef huart1;
/* USER CODE BEGIN EV */

/* USER CODE END EV */

/******************************************************************************/
/*           Cortex-M4 Processor Interruption and Exception Handlers          */
/******************************************************************************/
/**
  * @brief This function handles Non maskable interrupt.
  */
void NMI_Handler(void)
{
  /* USER CODE BEGIN NonMaskableInt_IRQn 0 */

  /* USER CODE END NonMaskableInt_IRQn 0 */
  /* USER CODE BEGIN NonMaskableInt_IRQn 1 */
  while (1)
  {
  }
  /* USER CODE END NonMaskableInt_IRQn 1 */
}

/**
  * @brief This function handles Hard fault interrupt.
  */
void HardFault_Handler(void)
{
  /* USER CODE BEGIN HardFault_IRQn 0 */

  /* USER CODE END HardFault_IRQn 0 */
  while (1)
  {
    /* USER CODE BEGIN W1_HardFault_IRQn 0 */
    /* USER CODE END W1_HardFault_IRQn 0 */
  }
}

/**
  * @brief This function handles Memory management fault.
  */
void MemManage_Handler(void)
{
  /* USER CODE BEGIN MemoryManagement_IRQn 0 */

  /* USER CODE END MemoryManagement_IRQn 0 */
  while (1)
  {
    /* USER CODE BEGIN W1_MemoryManagement_IRQn 0 */
    /* USER CODE END W1_MemoryManagement_IRQn 0 */
  }
}

/**
  * @brief This function handles Prefetch fault, memory access fault.
  */
void BusFault_Handler(void)
{
  /* USER CODE BEGIN BusFault_IRQn 0 */

  /* USER CODE END BusFault_IRQn 0 */
  while (1)
  {
    /* USER CODE BEGIN W1_BusFault_IRQn 0 */
    /* USER CODE END W1_BusFault_IRQn 0 */
  }
}

/**
  * @brief This function handles Undefined instruction or illegal state.
  */
void UsageFault_Handler(void)
{
  /* USER CODE BEGIN UsageFault_IRQn 0 */

  /* USER CODE END UsageFault_IRQn 0 */
  while (1)
  {
    /* USER CODE BEGIN W1_UsageFault_IRQn 0 */
    /* USER CODE END W1_UsageFault_IRQn 0 */
  }
}

/**
  * @brief This function handles System service call via SWI instruction.
  */
void SVC_Handler(void)
{
  /* USER CODE BEGIN SVCall_IRQn 0 */

  /* USER CODE END SVCall_IRQn 0 */
  /* USER CODE BEGIN SVCall_IRQn 1 */

  /* USER CODE END SVCall_IRQn 1 */
}

/**
  * @brief This function handles Debug monitor.
  */
void DebugMon_Handler(void)
{
  /* USER CODE BEGIN DebugMonitor_IRQn 0 */

  /* USER CODE END DebugMonitor_IRQn 0 */
  /* USER CODE BEGIN DebugMonitor_IRQn 1 */

  /* USER CODE END DebugMonitor_IRQn 1 */
}

/**
  * @brief This function handles Pendable request for system service.
  */
void PendSV_Handler(void)
{
  /* USER CODE BEGIN PendSV_IRQn 0 */

  /* USER CODE END PendSV_IRQn 0 */
  /* USER CODE BEGIN PendSV_IRQn 1 */

  /* USER CODE END PendSV_IRQn 1 */
}

/**
  * @brief This function handles System tick timer.
  */
void SysTick_Handler(void)
{
  /* USER CODE BEGIN SysTick_IRQn 0 */
	if(led1==1)
	{
		time1++;
		led1_flag=1;
		if(time1>1000)
		{
			time1=0;
			led1=0;
			led1_flag=0;
		}
	}
  /* USER CODE END SysTick_IRQn 0 */
  HAL_IncTick();
  /* USER CODE BEGIN SysTick_IRQn 1 */
	if(led2==1)
	{
		time2++;
		led2_flag=1;
		if(time2>1000)
		{
			time2=0;
			led2=0;
			led2_flag=0;
		}
	}
  /* USER CODE END SysTick_IRQn 1 */
}

/******************************************************************************/
/* STM32G4xx Peripheral Interrupt Handlers                                    */
/* Add here the Interrupt Handlers for the used peripherals.                  */
/* For the available peripheral interrupt handler names,                      */
/* please refer to the startup file (startup_stm32g4xx.s).                    */
/******************************************************************************/

/**
  * @brief This function handles DMA1 channel1 global interrupt.
  */
void DMA1_Channel1_IRQHandler(void)
{
  /* USER CODE BEGIN DMA1_Channel1_IRQn 0 */

  /* USER CODE END DMA1_Channel1_IRQn 0 */
  HAL_DMA_IRQHandler(&hdma_adc1);
  /* USER CODE BEGIN DMA1_Channel1_IRQn 1 */

  /* USER CODE END DMA1_Channel1_IRQn 1 */
}

/**
  * @brief This function handles DMA1 channel2 global interrupt.
  */
void DMA1_Channel2_IRQHandler(void)
{
  /* USER CODE BEGIN DMA1_Channel2_IRQn 0 */

  /* USER CODE END DMA1_Channel2_IRQn 0 */
  HAL_DMA_IRQHandler(&hdma_adc2);
  /* USER CODE BEGIN DMA1_Channel2_IRQn 1 */

  /* USER CODE END DMA1_Channel2_IRQn 1 */
}

/**
  * @brief This function handles TIM4 global interrupt.
  */
void TIM4_IRQHandler(void)
{
  /* USER CODE BEGIN TIM4_IRQn 0 */

  /* USER CODE END TIM4_IRQn 0 */
  HAL_TIM_IRQHandler(&htim4);
  /* USER CODE BEGIN TIM4_IRQn 1 */

  /* USER CODE END TIM4_IRQn 1 */
}

/**
  * @brief This function handles USART1 global interrupt / USART1 wake-up interrupt through EXTI line 25.
  */
void USART1_IRQHandler(void)
{
  /* USER CODE BEGIN USART1_IRQn 0 */

  /* USER CODE END USART1_IRQn 0 */
  HAL_UART_IRQHandler(&huart1);
  /* USER CODE BEGIN USART1_IRQn 1 */

  /* USER CODE END USART1_IRQn 1 */
}

/* USER CODE BEGIN 1 */

/* USER CODE END 1 */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

  • 12
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Nemophilist12

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值