温馨提示:
真题演练分为模拟篇和研究篇。本专栏的主要作用是记录我的备赛过程,我打算先自己做一遍,把遇到的问题和不同之处记录到演练篇 ,然后再返回来仔细研究一下,找到最佳的解题方法记录到研究篇。题目在:https://pan.quark.cn/s/fc121e80dbff
目录
解题记录:
用时:3小时50分钟
完成度:全部完成
过程中遇到的问题:
-
LED的闪烁问题**(之前没有注意到)** ,不可以使用Toggle函数;因为LCD会对引脚的状态产生影响。应该使用标志变量,或利用奇偶数来实现状态切换。例如:
cppif(LD2_tim%2==0) LED_swicth(LED2_Pin,GPIO_PIN_RESET); else LED_swicth(LED2_Pin,GPIO_PIN_SET);
-
不知道为什么,我的开发板上一打开TIM就会产生一次超时中断;解决方法是设置一个计数变量为0,只要调用中断服务函数就自增1,当计数为2时再实现计划的功能。比如:
cpp//初始化全局变量tim6=0; /* USER CODE BEGIN TIM6_DAC_IRQn 1 */ tim6++; if(tim6==2) { //各种操作; tim6=0; HAL_TIM_Base_Stop_IT(&htim6); } /* USER CODE END TIM6_DAC_IRQn 1 */
-
在LED驱动函数中包含关闭所有LED灯的,只需要在进入while前关闭一次LED,不用每次都关闭LED了。
cpp//LED控制 void LED_swicth(uint16_t GPIO_pin,GPIO_PinState PinState) { HAL_GPIO_WritePin(LED_LE_GPIO_Port,LED_LE_Pin,GPIO_PIN_SET); HAL_GPIO_WritePin(LED1_GPIO_Port,LED1_Pin|LED2_Pin|LED3_Pin|LED4_Pin| LED5_Pin|LED6_Pin|LED7_Pin|LED8_Pin,GPIO_PIN_SET); HAL_GPIO_WritePin(LED1_GPIO_Port,GPIO_pin,PinState); HAL_GPIO_WritePin(LED_LE_GPIO_Port,LED_LE_Pin,GPIO_PIN_RESET); }
-
**重要!!**题目要求按键响应小于100ms,那我们就设置为90ms。
-
有时需要在中断中清屏,但是中断的位置不确定;所以要设置一个全局标志,在中断改变标志,然后在需要清屏的地方判断标志位来清屏。
-
串口接收信号后要延时50ms。
反思总结
需要改进的方面:
应该一次性就把变量设计好,一开始全部设置为全局变量。有时间的话在优化时去除一些不需要全局使用的变量即可。
做题记录
草稿:
外设:
主流程:
功能函数:
代码:
main.c
cpp
//省略了自动生成的代码
/* USER CODE BEGIN PV */
char LCD_str[20];
char rxBuf[10]="000_000\n";
uint8_t LCD_show=0;
uint8_t LD1_f=0;
uint8_t LD2_f=0;
uint8_t tryTime=0;
uint8_t clear_f=0;
uint8_t tim6=0;
char KEY[3]={'1','2','3'};
char B[3]={'@','@','@'};
uint32_t STA_F=2000;
uint32_t STA_D=10;
uint32_t LD2_tim=0;
/* USER CODE END PV */
/* USER CODE BEGIN 0 */
//LED控制
void LED_swicth(uint16_t GPIO_pin,GPIO_PinState PinState)
{
HAL_GPIO_WritePin(LED_LE_GPIO_Port,LED_LE_Pin,GPIO_PIN_SET);
HAL_GPIO_WritePin(
LED1_GPIO_Port,LED1_Pin|LED2_Pin|LED3_Pin|LED4_Pin|
LED5_Pin|LED6_Pin|LED7_Pin|LED8_Pin,GPIO_PIN_SET);
HAL_GPIO_WritePin(LED1_GPIO_Port,GPIO_pin,PinState);
HAL_GPIO_WritePin(LED_LE_GPIO_Port,LED_LE_Pin,GPIO_PIN_RESET);
}
//LED状态
void LED_State()
{
if(LD1_f==1)
LED_swicth(LED1_Pin,GPIO_PIN_RESET);
}
//密码验证
void KEY_Try()
{
if(KEY[0]==B[0]&&KEY[1]==B[1]&&KEY[2]==B[2])
{
LCD_show=1;
LCD_Clear(Black);
LD1_f=1;
tryTime=0;
__HAL_TIM_SET_COUNTER(&htim2,99);
__HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_2,9);
HAL_TIM_Base_Start_IT(&htim6);
}
else
{
tryTime++;
B[0]='@',B[1]='@',B[2]='@';
if(tryTime>=3)
{
LD2_f=1;
HAL_TIM_Base_Start_IT(&htim7);
}
}
}
//密码改变
void KEY_Change(char* Bx)
{
if(*Bx=='@'||*Bx=='9')
*Bx='0';
else
*Bx=*Bx+1;
}
//按键响应
void B1_fun()
{
if(LCD_show!=0)
return;
KEY_Change(&B[0]);
}
void B2_fun()
{
if(LCD_show!=0)
return;
KEY_Change(&B[1]);
}
void B3_fun()
{
if(LCD_show!=0)
return;
KEY_Change(&B[2]);
}
void B4_fun()
{
if(LCD_show!=0)
return;
KEY_Try();
}
//按键扫描
void keyScan()
{
uint16_t keyDelay=90;
if(HAL_GPIO_ReadPin(KEY1_GPIO_Port,KEY1_Pin)==GPIO_PIN_RESET)
{
HAL_Delay(keyDelay);
if(HAL_GPIO_ReadPin(KEY1_GPIO_Port,KEY1_Pin)==GPIO_PIN_RESET)
{
B1_fun();
}
}
if(HAL_GPIO_ReadPin(KEY2_GPIO_Port,KEY2_Pin)==GPIO_PIN_RESET)
{
HAL_Delay(keyDelay);
if(HAL_GPIO_ReadPin(KEY2_GPIO_Port,KEY2_Pin)==GPIO_PIN_RESET)
{
B2_fun();
}
}
if(HAL_GPIO_ReadPin(KEY3_GPIO_Port,KEY3_Pin)==GPIO_PIN_RESET)
{
HAL_Delay(keyDelay);
if(HAL_GPIO_ReadPin(KEY3_GPIO_Port,KEY3_Pin)==GPIO_PIN_RESET)
{
B3_fun();
}
}
if(HAL_GPIO_ReadPin(KEY4_GPIO_Port,KEY4_Pin)==GPIO_PIN_RESET)
{
HAL_Delay(keyDelay);
if(HAL_GPIO_ReadPin(KEY4_GPIO_Port,KEY4_Pin)==GPIO_PIN_RESET)
{
B4_fun();
}
}
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
LCD_Init();
LCD_SetBackColor(Black);
LCD_SetTextColor(White);
LCD_Clear(Black);
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DMA_Init();
MX_TIM2_Init();
MX_TIM6_Init();
MX_TIM7_Init();
MX_USART1_UART_Init();
/* Infinite loop */
/* USER CODE BEGIN WHILE */
HAL_TIM_Base_Start(&htim2);
HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_2);
HAL_UART_Receive_DMA(&huart1,(uint8_t*)rxBuf,sizeof(rxBuf));
LED_swicth(LED1_Pin|LED2_Pin|LED3_Pin|LED4_Pin|LED5_Pin|LED6_Pin|LED7_Pin|LED8_Pin,GPIO_PIN_SET);
while (1)
{
keyScan();
if(LCD_show==0)
{
if(clear_f==1)
{
LCD_Clear(Black);
clear_f=0;
}
LED_State();
LCD_DisplayStringLine(Line2,(uint8_t*)" PSD");
sprintf(LCD_str," B1:%c",B[0]);
LCD_DisplayStringLine(Line4,(uint8_t*)LCD_str);
sprintf(LCD_str," B2:%c",B[1]);
LCD_DisplayStringLine(Line5,(uint8_t*)LCD_str);
sprintf(LCD_str," B3:%c",B[2]);
LCD_DisplayStringLine(Line6,(uint8_t*)LCD_str);
}
else if(LCD_show==1)
{
LED_State();
LCD_DisplayStringLine(Line2,(uint8_t*)" STA");
sprintf(LCD_str," F:%dHz",STA_F);
LCD_DisplayStringLine(Line4,(uint8_t*)LCD_str);
STA_D=__HAL_TIM_GET_COMPARE(&htim2,TIM_CHANNEL_2)+1;
sprintf(LCD_str," D:%d%c",STA_D,'%');
LCD_DisplayStringLine(Line5,(uint8_t*)LCD_str);
}
}
}
main.h
cpp
//省略了自动生成的代码
/* USER CODE BEGIN Includes */
#include "lcd.h"
#include <stdio.h>
/* USER CODE END Includes */
/* USER CODE BEGIN EFP */
void LED_swicth(uint16_t GPIO_pin,GPIO_PinState PinState);
/* USER CODE END EFP */
/* USER CODE BEGIN Private defines */
extern char LCD_str[];
extern char rxBuf[];
extern uint8_t LCD_show;
extern uint8_t LD1_f;
extern uint8_t tryTime;
extern uint8_t tim6;
extern uint8_t clear_f;
extern char KEY[];
extern char B[];
extern uint32_t STA_F;
extern uint32_t STA_D;
extern uint32_t LD2_tim;
/* USER CODE END Private defines */
stm32g4xx_it.c:
cpp
//省略了自动生成的代码
void DMA1_Channel1_IRQHandler(void)
{
/* USER CODE BEGIN DMA1_Channel1_IRQn 0 */
/* USER CODE END DMA1_Channel1_IRQn 0 */
HAL_DMA_IRQHandler(&hdma_usart1_rx);
/* USER CODE BEGIN DMA1_Channel1_IRQn 1 */
HAL_Delay(50);
if(KEY[0]==rxBuf[0]&&KEY[1]==rxBuf[1]&&KEY[2]==rxBuf[2])
{
KEY[0]=rxBuf[4];
KEY[1]=rxBuf[5];
KEY[2]=rxBuf[6];
}
/* USER CODE END DMA1_Channel1_IRQn 1 */
}
void TIM6_DAC_IRQHandler(void)
{
/* USER CODE BEGIN TIM6_DAC_IRQn 0 */
/* USER CODE END TIM6_DAC_IRQn 0 */
HAL_TIM_IRQHandler(&htim6);
/* USER CODE BEGIN TIM6_DAC_IRQn 1 */
tim6++;
if(tim6==2)
{
clear_f=1;
tim6=0;
LCD_show=0;
LD1_f=0;
LED_swicth(LED1_Pin,GPIO_PIN_SET);
B[0]='@',B[1]='@',B[2]='@';
HAL_TIM_Base_Stop_IT(&htim6);
}
/* USER CODE END TIM6_DAC_IRQn 1 */
}
void TIM7_IRQHandler(void)
{
/* USER CODE BEGIN TIM7_IRQn 0 */
/* USER CODE END TIM7_IRQn 0 */
HAL_TIM_IRQHandler(&htim7);
/* USER CODE BEGIN TIM7_IRQn 1 */
LD2_tim++;
if(LD2_tim==50)
{
LD2_tim=0;
HAL_TIM_Base_Stop_IT(&htim7);
}
else
{
if(LD2_tim%2==0)
LED_swicth(LED2_Pin,GPIO_PIN_RESET);
else
LED_swicth(LED2_Pin,GPIO_PIN_SET);
}
/* USER CODE END TIM7_IRQn 1 */
}