蓝桥杯嵌入式第十五届模拟赛--程序设计部分

本文介绍了STM32开发中,main.c文件的核心功能,包括系统时钟配置、ADC采样、TIM定时、USART通信、GPIO控制、LCD显示、按键处理、PWM输出以及LED显示,展示了蓝桥杯嵌入式竞赛中可能遇到的模块应用实例。
摘要由CSDN通过智能技术生成

原题:

代码:

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 "usart.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "lcd.h"
#include "stdio.h"
#include "interrupt.h"
#include "myadc.h"
#include "string.h"
#include "led.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
extern struct key keys[4];//四个按键
extern char rxdata[30];//串口接收到的数据
extern uint8_t rxdat;
extern uchar rx_pointer;//串口收到的位数
extern int times;//中断检测次数,用来实现定时功能
extern int three_f;//3s 灭灯
extern int five_f;//5s 睡眠

int view=1;//界面
double TEMP=23.5;//温度
int MODE=0;//模式 初始为自动
int GEAR=1;//挡位
int USART_flag=0;//串口是否发送了数据
int key_flag=0;//按键是否按下了
uchar led_sta=0x00;//led灯
int tim_begin=0;//是否可以开始计时
/* 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 */
void lcd_proc(void);
void key_proc(void);
void PWM_Set(void);
void TEMP_Set(void);
void uart_rx(void);
void LED_Set(void);
void set_bit(unsigned char *num, int bit_index);
void clear_bit(unsigned char *num, int bit_index);
/* 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_TIM4_Init();
  MX_TIM2_Init();
  MX_ADC2_Init();
  MX_USART1_UART_Init();
  /* USER CODE BEGIN 2 */
	LCD_Init();//LCD初始化
	LCD_Clear(Black);
	LCD_SetBackColor(Black);
	LCD_SetTextColor(White);
	
	HAL_TIM_Base_Start_IT(&htim4);
	
	HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_2);//PWM输出
	
	HAL_UART_Receive_IT(&huart1,&rxdat,1);//开启串口中断
	
	LED_Display(led_sta);
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
		lcd_proc();//LCD界面
		key_proc();//按键部分
		TEMP_Set();//根据模拟电压信号设置TEMP
		PWM_Set();//根据挡位设置PWM输出
		LED_Set();//根据需求设置LED
		if(key_flag==0&&USART_flag==0&&tim_begin==0)//什么也没操作 等待五秒休眠
		{
			tim_begin=1;
			times=0;
			five_f=0;
		}
		if(five_f==1&&view==1)//数据界面 进入 休眠界面
		{
			view=!view;
			LCD_Clear(Black);
			five_f=0;
		}
		if(three_f==1)//串口发送完3秒 灯灭
		{
			clear_bit(&led_sta,3);
			three_f=0;
		}
		if(rx_pointer!=0)//串口接收数据
		{
			int temp=rx_pointer;
			HAL_Delay(1);
			if(temp==rx_pointer) uart_rx();
		}
    /* USER CODE END WHILE */

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

  /** 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 lcd_proc(void)
{
	char text[30];
	
	if(view)//数据界面
	{
		sprintf(text,"        DATA    ");
		LCD_DisplayStringLine(Line1,(unsigned char*)text);
		sprintf(text,"     TEMP:%.1lf    ",TEMP);
		LCD_DisplayStringLine(Line3,(unsigned char*)text);
		sprintf(text,"     MODE:%s    ",MODE==0?"Auto":"Manu");
		LCD_DisplayStringLine(Line4,(unsigned char*)text);
		sprintf(text,"     GEAR:%d    ",GEAR);
		LCD_DisplayStringLine(Line5,(unsigned char*)text);
	}else//睡眠界面
	{
		sprintf(text,"     SLEEPING    ");
		LCD_DisplayStringLine(Line4,(unsigned char*)text);
		sprintf(text,"     TEMP:%.1lf    ",TEMP);
		LCD_DisplayStringLine(Line5,(unsigned char*)text);
	}
	
}

void key_proc(void)
{
	if(keys[0].single_flag==1)//B1
	{
		tim_begin=0;//此时有动作发送,不能开始计时
		if(view)//数据界面
		{
			MODE=!MODE;
		}else//睡眠界面,唤醒
		{
			view=!view;//切换模式
			LCD_Clear(Black);
		}
		key_flag=0;//动作结束
		keys[0].single_flag=0;
	}else if(keys[1].single_flag==1)//B2
	{
		tim_begin=0;
		if(view)//数据界面,操纵挡位
		{
			if(MODE==1)
			{
				GEAR++;//调整挡位
				if(GEAR>=3) GEAR=3;
			}
		}else//睡眠界面,唤醒
		{
			view=!view;
			LCD_Clear(Black);
		}
		key_flag=0;
		keys[1].single_flag=0;
	}else if(keys[2].single_flag==1)//B3
	{
		tim_begin=0;
		if(view)
		{
			if(MODE==1)
			{
				GEAR--;
				if(GEAR<=1) GEAR=1;
			}
		}else
		{
			view=!view;
			LCD_Clear(Black);
		}
		key_flag=0;
		keys[2].single_flag=0;
	}
}

void PWM_Set(void)//设置占空比
{
	if(GEAR==1)
	{
		__HAL_TIM_SetCompare(&htim2,TIM_CHANNEL_2,100);
	}else if(GEAR==2)
	{
		__HAL_TIM_SetCompare(&htim2,TIM_CHANNEL_2,400);
	}else if(GEAR==3)
	{
		__HAL_TIM_SetCompare(&htim2,TIM_CHANNEL_2,800);
	}
}

void TEMP_Set(void)//设置温度
{
	double temp=getADC(&hadc2);
	if(temp<1)
	{
		TEMP=0.0;
	}else if(temp>=1&&temp<=3)
	{
		TEMP=getADC(&hadc2)*10+10;
	}else
	{
		TEMP=3*10+10;
	}
	
	if(MODE==0)
	{
		if(TEMP<25)
		{
			GEAR=1;
		}else if(TEMP>=25&&TEMP<=30)
		{
			GEAR=2;
		}else
		{
			GEAR=3;
		}
	}
}

void uart_rx(void)
{
	if(rx_pointer>0)
	{
		tim_begin=0;//同按键
		USART_flag=1;//串口有发送
		set_bit(&led_sta,3);//点灯
		three_f=0;
		times=0;
		
		if(rx_pointer==2)//数据位数符合需求
		{
			char rx[10];
			sscanf(rxdata,"%s",rx);
			if(!strcmp("B1",rx))
			{
				keys[0].single_flag=1;
			}else if(!strcmp("B2",rx))
			{
				keys[1].single_flag=1;
			}else if(!strcmp("B3",rx))
			{
				keys[2].single_flag=1;
			}else 
			{
				char temp[20];
				sprintf(temp,"NULL\r\n");
				HAL_UART_Transmit(&huart1,(uint8_t*)temp,strlen(temp),50);
			}
			
		}else
		{
			char temp[20];
			sprintf(temp,"NULL\r\n");
			HAL_UART_Transmit(&huart1,(uint8_t*)temp,strlen(temp),50);
		}
		USART_flag=0;
		rx_pointer=0;//置0  重新准备读
		memset(rxdata,0,30);
	}
}

void LED_Set(void)
{
	//每次只对一个十六进制的一位进行修改,最后再根据这个十六进制的值电灯
	if(MODE==0)
	{
		set_bit(&led_sta,7);
	}else
	{
		clear_bit(&led_sta,7);
	}
	
	if(GEAR==1)
	{
		set_bit(&led_sta,0);
	}else
	{
		clear_bit(&led_sta,0);
	}
	if(GEAR==2)
	{
		set_bit(&led_sta,1);
	}else
	{
		clear_bit(&led_sta,1);
	}
	if(GEAR==3)
	{
		set_bit(&led_sta,2);
	}else
	{
		clear_bit(&led_sta,2);
	}
	

	LED_Display(led_sta);
}

void clear_bit(unsigned char *num, int bit_index) //位置0
{  
    *num = (*num & ~(1 << bit_index));  
}

void set_bit(unsigned char *num, int bit_index)//位置1
{  
    *num = (*num & ~(1 << bit_index)) | (1 << bit_index);  
}  
/* 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 */

interrupt.c

#include "interrupt.h"
#include "usart.h"
struct key keys[4];
int times=0;//中断执行次数,定时器工作频率100Hz,10ms执行一次中断
int three_f=0;
int five_f=0;
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
	times++;
	if(times>=300)
	{
		three_f=1;
	}
	if(times>=500)
	{
		five_f=1;
	}
	if(htim->Instance==TIM4)//中断来源
	{
		//读取四个按键的状态
		keys[0].key_status=HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0);
		keys[1].key_status=HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_1);
		keys[2].key_status=HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_2);
		keys[3].key_status=HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0);
		
		//状态机消抖
		for(int i=0;i<4;i++)
		{
			switch(keys[i].judge_status)
			{
				case 0:
				{
					if(keys[i].key_status==0)
					{
						keys[i].judge_status=1;
						keys[i].key_time=0;
					}
				}
				break;
				case 1:
				{
					if(keys[i].key_status==0)
					{
						keys[i].judge_status=2;
					}else
					{
						keys[i].judge_status=0;
					}
				}
				break;
				case 2:
				{
					if(keys[i].key_status==0)
					{
						keys[i].key_time++;
						if(keys[i].key_time>=70)
						{
							keys[i].long_flag=1;
						}
					}else
					{
						keys[i].judge_status=0;
						if(keys[i].key_time<70)
						{
							keys[i].single_flag=1;
						}
					}
				}
				break;
			}
		}
	}
}

//串口接收
char rxdata[30];
uint8_t rxdat;
uchar rx_pointer;

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *hurat)
{
	rxdata[rx_pointer++]=rxdat;
	HAL_UART_Receive_IT(&huart1,&rxdat,1);
}


interrupt.h

#ifndef _INTERRUPT_H_
#define _INTERRUPT_H_

#include "main.h"
#include "stdbool.h"
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim);

struct key
{
	bool single_flag;
	bool long_flag;
	unsigned int key_time;
	unsigned int key_status;
	unsigned int judge_status;
};

#endif

led.c

#include "led.h"

void LED_Display(uchar dsLED)
{
	HAL_GPIO_WritePin(GPIOC,GPIO_PIN_All,GPIO_PIN_SET);	//将GPIOC的所有引脚全部置高电平,LED灯熄灭
	HAL_GPIO_WritePin(GPIOC,dsLED<<8,GPIO_PIN_RESET);		//根据dsLEDd的数值将对应LED点亮
	HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);		//将锁存器打开
	HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);	//将锁存器关闭
}

led.h

#ifndef _LED_H_
#define _LED_H_

#include "main.h"
void LED_Display(uchar dsLED);

#endif

myadc.c

#include "myadc.h"

//读取adc
double getADC(ADC_HandleTypeDef *pin)
{
	unsigned int adc;
	HAL_ADC_Start(pin);
	adc=HAL_ADC_GetValue(pin);
	return adc*3.3/4096;
}

myadc.h

#ifndef _MYADC_H_
#define _MYADC_H_

#include "main.h"

double getADC(ADC_HandleTypeDef *pin);
#endif

 ----THE END-----

有关蓝桥杯嵌入式的模块知识请参考如下:

蓝桥杯嵌入式模块学习系列

【蓝桥杯嵌入式学习G431】模块一:LED-CSDN博客

【蓝桥杯嵌入式学习G431】模块二:LCD-CSDN博客

【蓝桥杯嵌入式学习G431】模块三:KEY-CSDN博客

【蓝桥杯嵌入式学习G431】模块四:PWM-CSDN博客

【蓝桥杯嵌入式学习G431】模块五:USART-CSDN博客

【蓝桥杯嵌入式学习G431】模块六:ADC-CSDN博客

(还有几个模块没更新~)


有关蓝桥杯嵌入式历届真题请参考如下:

蓝桥杯嵌入式历届真题系列

蓝桥杯嵌入式第十二届省赛--程序设计部分-CSDN博客

蓝桥杯嵌入式第十三届省赛第一场--程序设计部分-CSDN博客

蓝桥杯嵌入式第十五届模拟赛--程序设计部分-CSDN博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值