省一了,简单分享下(虽然国赛白给了
程序设计题很重要,有85分,客观题(15分)我都是乱蒙的,但我程序设计全实现了(大概)所以才能省一。
开发板是新板,型号:CT117E-M4(STM32G431RBT6)
如下图
这道题的最大坑点在于EEPROM的初始化,要仔细看题目最后的要求,上电初始化价格都是1.0,而题目中又要求从eeprom读取价格信息。其实就是要做一个eeprom是否是第一次上电的判断,如果是就初始化1.0,否则就直接从eeprom里读取就行了。
要实现是否第一次上电的判断,就是在eeprom的某个地址(题目要求之外的地址)存储一个自己的标志位。
在这道题里,eeprom要用到的地址是0-3,而我是在地址5写自己的标志位,数值66。每一次上电都从地址5处读取信息,看数值是不是66,如果不是就说明是第一次上电,这时候就可以把66写进去,同时也按照题目要求初始化价格为1.0。如果读到了66就说明不是第一次上电了,可以直接从eeprom读取信息。
代码用的cubemx配置生成的工程,这个软件用起来很方便,建议要参加蓝桥杯嵌入式的都学一下(不太清楚旧板子可不可以用)。
软件自动生成的英文注释懒得删了,自己写代码写在那些“User code begin”和“User code end”之间就不会被软件覆盖。
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2022 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 "tim.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "lcd.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 */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
// 按键相关变量定义
__IO uint32_t uwTick_Key = 0; // 消抖
uint8_t Key_Val, Key_Down, Key_Up, Key_Old; // 存储键值
// LCD相关变量定义
__IO uint32_t uwTick_LCD = 0; // 减速
uint8_t lstr[22]; // 屏幕显示字符串
int page = 1; // 页面标号
// led
__IO uint32_t LD2 = 0;
__IO uint32_t LD1 = 0;
uint8_t led = 0;
int b4_flag = 0;
// EEPROM存储变量
uint8_t xs, ys; // 存储到eeprom中的商品库存
uint8_t xp, yp; // 存储到eeprom中的商品价格
uint8_t num, num1= 66; // 标记开发板是否第一次上电
// 串口相关变量定义
uint8_t rx;
char ustr1[20];
char ustr2[13];
int uflag = 0; // 串口接收中断标志
// 商品相关变量定义
int x_buy = 0, y_buy = 0; // 商品购买数量
uint8_t x_s, y_s; // 商品存储数量
float x_price, y_price; // 商品价格
// eeprom测试
//uint8_t eeprom_str1[5] = {0x11, 0x22, 0x33, 0x44, 0x55};
//uint8_t eeprom_str2[5] = {0};
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
// 功能函数定义
void KEY_Proc(); // 按键功能函数
void LCD_Proc(); // LCD屏幕显示功能函数
void UART_Proc1(); // B4按下串口发送信息
void UART_Proc2(); // 串口接收查询
void LED2_Proc(); // LED2控制程序 间隔0.1s闪烁
/* 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_USART1_UART_Init();
MX_TIM2_Init();
/* USER CODE BEGIN 2 */
// 初始化LCD
LCD_Init();
LCD_Clear(Black);
LCD_SetBackColor(Black);
LCD_SetTextColor(White);
// 初始化I2C
I2CInit();
xs = 10; // 商品库存初始值10
ys = 10;
xp = 10; // 商品价格初始值为1.0 放大10倍存入eeprom
yp = 10;
read_eeprom(&num, 5, 1);
HAL_Delay(1);
// 根据eeprom地址5的数据判断是否为第一次上电
// 第一次上电需要将初始值写入EEPROM
// 判断为非第一次上电将不会执行以下if段
if(num != num1){ // 将num1的数值改为和eeprom地址5处数据不同 即判断为第一次上电
write_eeprom(&xs, 0, 1); // 不加延时会出错
HAL_Delay(10);
write_eeprom(&ys, 1, 1);
HAL_Delay(10);
write_eeprom(&xp, 2, 1);
HAL_Delay(10);
write_eeprom(&yp, 3, 1);
HAL_Delay(10);
num = num1; // 第一次上电需要修改地址5处数值 以便下次上电判断
write_eeprom(&num, 5, 1);
HAL_Delay(1);
}
// 从EEPROM中读出
read_eeprom(&xs, 0, 1);
HAL_Delay(1);
read_eeprom(&ys, 1, 1);
HAL_Delay(1);
read_eeprom(&xp, 2, 1);
HAL_Delay(1);
read_eeprom(&yp, 3, 1);
x_s = xs;
y_s = ys;
x_price = (float)xp/10;
y_price = (float)yp/10;
// 初始化TIM2 PA1输出PWM
HAL_TIM_Base_Start(&htim2);
HAL_TIM_OC_Start_IT(&htim2, TIM_CHANNEL_2);
// 开串口中断
HAL_UART_Receive_IT(&huart1, &rx, 1);
// EEPROM测试
// write_eeprom(eeprom_str1, 0, 5);
// HAL_Delay(10);
// read_eeprom(eeprom_str2, 0, 5);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
if(uflag) UART_Proc2();
KEY_Proc();
LCD_Proc();
LED_Disp(led);
if(x_s == 0&& y_s == 0){
LED2_Proc();
}
else{
led &= ~(0x02);
}
if(b4_flag){
if((uwTick - LD1) >= 5000){
led &= ~(0x01);
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_2, 25);
b4_flag = 0;
}
}
/* 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 KEY_Proc()
{
if((uwTick - uwTick_Key) < 20) return ;
uwTick_Key = uwTick;
Key_Val = KEY_Scan();
Key_Down = Key_Val&(Key_Old^Key_Val);
Key_Up = ~Key_Val&(Key_Old^Key_Val);
Key_Old = Key_Val;
// 按键B1 翻页
if(Key_Down == 1){
if(page < 3) page++;
else page = 1;
LCD_Clear(Black);
}
// 按键B2 商品X
else if(Key_Down == 2){
if(page == 1){
if(x_buy < x_s) x_buy += 1;
else{
x_buy = 0;
LCD_Clear(Black);
}
}
else if(page == 2){
if(x_price < 2) x_price += 0.1;
else x_price = 1.0;
xp = x_price * 10;
write_eeprom(&xp, 2, 1);
}
else if(page == 3){
x_s += 1;
xs = x_s;
write_eeprom(&xs, 0, 1);
}
}
// 按键B3 商品Y
else if(Key_Down == 3){
if(page == 1){
if(y_buy < y_s) y_buy += 1;
else{
y_buy = 0;
LCD_Clear(Black);
}
}
else if(page == 2){
if(y_price < 2) y_price += 0.1;
else y_price = 1.0;
yp = y_price * 10;
write_eeprom(&yp, 3, 1);
}
else if(page == 3){
y_s += 1;
ys = y_s;
write_eeprom(&ys, 1, 1);
}
}
// 按键B4 确认购买
else if(Key_Down == 4){
if(page == 1){
x_s -= x_buy;
y_s -= y_buy;
UART_Proc1();
xs = x_s;
ys = y_s;
write_eeprom(&xs, 0, 1);
HAL_Delay(1);
write_eeprom(&ys, 1, 1);
x_buy = 0;
y_buy = 0;
LCD_Clear(Black);
b4_flag = 1;
LD1 = uwTick;
led |= 0x01;
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_2, 150);
}
}
}
// LCD屏幕显示功能函数
void LCD_Proc()
{
// 减速
// if((uwTick - uwTick_LCD) < 100) return ;
// uwTick_LCD = uwTick;
// 商品购买界面
if(page == 1){
sprintf((char *)lstr, " SHOP");
LCD_DisplayStringLine(Line1, lstr);
sprintf((char *)lstr, " X:%d", x_buy);
LCD_DisplayStringLine(Line3, lstr);
sprintf((char *)lstr, " Y:%d", y_buy);
LCD_DisplayStringLine(Line4, lstr);
}
// 商品价格界面
else if(page == 2){
sprintf((char *)lstr, " PRICE");
LCD_DisplayStringLine(Line1, lstr);
sprintf((char *)lstr, " X:%.1f", x_price);
LCD_DisplayStringLine(Line3, lstr);
sprintf((char *)lstr, " Y:%.1f", y_price);
LCD_DisplayStringLine(Line4, lstr);
}
// 库存信息界面
else if(page == 3){
sprintf((char *)lstr, " REP");
LCD_DisplayStringLine(Line1, lstr);
sprintf((char *)lstr, " X:%d", x_s);
LCD_DisplayStringLine(Line3, lstr);
sprintf((char *)lstr, " Y:%d", y_s);
LCD_DisplayStringLine(Line4, lstr);
}
// eeprom测试
// sprintf((char *)lstr, "EE: %02X,%02X,%02X,%02X,%02X", eeprom_str2[0],eeprom_str2[1],eeprom_str2[2],eeprom_str2[3],eeprom_str2[4]);
// LCD_DisplayStringLine(Line3, lstr);
}
// LED1
void LED2_Proc()
{
if((uwTick - LD2) < 100) return ;
LD2 = uwTick;
led ^= 0x02;
}
// 串口接收中断服务函数
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if(huart->Instance == USART1){
uflag = 1; // 置串口接收标志位
}
}
void UART_Proc1()
{
float z = x_buy*x_price + y_buy*y_price;
sprintf(ustr1, "X:%d,Y:%d,Z:%.1f\r\n", x_buy, y_buy, z);
HAL_UART_Transmit(&huart1, (unsigned char *)ustr1, strlen(ustr1), 50);
}
void UART_Proc2()
{
uflag = 0; // 清串口接收标志位
sprintf(ustr2, "X:%.1f,Y:%.1f\r\n", x_price, y_price);
HAL_UART_Transmit(&huart1, (unsigned char *)ustr2, strlen(ustr2), 50);
HAL_UART_Receive_IT(&huart1, &rx, 1); // 再开中断
}
/* 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 */
这个文件比赛时官方有提供,就是其中的I2CWaitAck函数要改一下(倒数四行)。
还有eeprom读写函数要自己背下来。
/*
程序说明: CT117E-M4嵌入式竞赛板GPIO模拟I2C总线驱动程序
软件环境: MDK-ARM HAL库
硬件环境: CT117E-M4嵌入式竞赛板
日 期: 2020-3-1
*/
#include "i2c_hal.h"
#define DELAY_TIME 20
/**
* @brief SDA线输入模式配置
* @param None
* @retval None
*/
void SDA_Input_Mode()
{
GPIO_InitTypeDef GPIO_InitStructure = {0};
GPIO_InitStructure.Pin = GPIO_PIN_7;
GPIO_InitStructure.Mode = GPIO_MODE_INPUT;
GPIO_InitStructure.Pull = GPIO_PULLUP;
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
}
/**
* @brief SDA线输出模式配置
* @param None
* @retval None
*/
void SDA_Output_Mode()
{
GPIO_InitTypeDef GPIO_InitStructure = {0};
GPIO_InitStructure.Pin = GPIO_PIN_7;
GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_OD;
GPIO_InitStructure.Pull = GPIO_NOPULL;
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
}
/**
* @brief SDA线输出一个位
* @param val 输出的数据
* @retval None
*/
void SDA_Output( uint16_t val )
{
if ( val )
{
GPIOB->BSRR |= GPIO_PIN_7;
}
else
{
GPIOB->BRR |= GPIO_PIN_7;
}
}
/**
* @brief SCL线输出一个位
* @param val 输出的数据
* @retval None
*/
void SCL_Output( uint16_t val )
{
if ( val )
{
GPIOB->BSRR |= GPIO_PIN_6;
}
else
{
GPIOB->BRR |= GPIO_PIN_6;
}
}
/**
* @brief SDA输入一位
* @param None
* @retval GPIO读入一位
*/
uint8_t SDA_Input(void)
{
if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_7) == GPIO_PIN_SET){
return 1;
}else{
return 0;
}
}
/**
* @brief I2C的短暂延时
* @param None
* @retval None
*/
static void delay1(unsigned int n)
{
uint32_t i;
for ( i = 0; i < n; ++i);
}
/**
* @brief I2C起始信号
* @param None
* @retval None
*/
void I2CStart(void)
{
SDA_Output(1);
delay1(DELAY_TIME);
SCL_Output(1);
delay1(DELAY_TIME);
SDA_Output(0);
delay1(DELAY_TIME);
SCL_Output(0);
delay1(DELAY_TIME);
}
/**
* @brief I2C结束信号
* @param None
* @retval None
*/
void I2CStop(void)
{
SCL_Output(0);
delay1(DELAY_TIME);
SDA_Output(0);
delay1(DELAY_TIME);
SCL_Output(1);
delay1(DELAY_TIME);
SDA_Output(1);
delay1(DELAY_TIME);
}
/**
* @brief I2C等待确认信号
* @param None
* @retval None
*/
unsigned char I2CWaitAck(void)
{
unsigned short cErrTime = 5;
SDA_Input_Mode();
delay1(DELAY_TIME);
SCL_Output(1);
delay1(DELAY_TIME);
while(SDA_Input())
{
cErrTime--;
delay1(DELAY_TIME);
if (0 == cErrTime)
{
SDA_Output_Mode();
I2CStop();
return ERROR;
}
}
// 以下三行有修改
SCL_Output(0);
delay1(DELAY_TIME);
SDA_Output_Mode();
return SUCCESS;
}
/**
* @brief I2C发送确认信号
* @param None
* @retval None
*/
void I2CSendAck(void)
{
SDA_Output(0);
delay1(DELAY_TIME);
delay1(DELAY_TIME);
SCL_Output(1);
delay1(DELAY_TIME);
SCL_Output(0);
delay1(DELAY_TIME);
}
/**
* @brief I2C发送非确认信号
* @param None
* @retval None
*/
void I2CSendNotAck(void)
{
SDA_Output(1);
delay1(DELAY_TIME);
delay1(DELAY_TIME);
SCL_Output(1);
delay1(DELAY_TIME);
SCL_Output(0);
delay1(DELAY_TIME);
}
/**
* @brief I2C发送一个字节
* @param cSendByte 需要发送的字节
* @retval None
*/
void I2CSendByte(unsigned char cSendByte)
{
unsigned char i = 8;
while (i--)
{
SCL_Output(0);
delay1(DELAY_TIME);
SDA_Output(cSendByte & 0x80);
delay1(DELAY_TIME);
cSendByte += cSendByte;
delay1(DELAY_TIME);
SCL_Output(1);
delay1(DELAY_TIME);
}
SCL_Output(0);
delay1(DELAY_TIME);
}
/**
* @brief I2C接收一个字节
* @param None
* @retval 接收到的字节
*/
unsigned char I2CReceiveByte(void)
{
unsigned char i = 8;
unsigned char cR_Byte = 0;
SDA_Input_Mode();
while (i--)
{
cR_Byte += cR_Byte;
SCL_Output(0);
delay1(DELAY_TIME);
delay1(DELAY_TIME);
SCL_Output(1);
delay1(DELAY_TIME);
cR_Byte |= SDA_Input();
}
SCL_Output(0);
delay1(DELAY_TIME);
SDA_Output_Mode();
return cR_Byte;
}
// I2C初始化
void I2CInit(void)
{
GPIO_InitTypeDef GPIO_InitStructure = {0};
GPIO_InitStructure.Pin = GPIO_PIN_7 | GPIO_PIN_6;
GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStructure.Pull = GPIO_PULLUP;
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
}
// 函数功能: 写EEPROM
// 参数说明: data为写入的数据, addr指定E2PROM内部地址, len指示写入数据的长度
void write_eeprom(uint8_t* data, uint8_t addr, uint8_t len)
{
I2CStart();
I2CSendByte(0xa0);
I2CWaitAck();
I2CSendByte(addr);
I2CWaitAck();
while(len--){
I2CSendByte(*data++);
I2CWaitAck();
}
I2CStop();
delay1(500);
}
// 函数功能: 读EEPROM
// 参数说明: data存储读出的数据, addr指定要读的EEPROM内部地址, len指示读出数据的长度
void read_eeprom(uint8_t* data, uint8_t addr, uint8_t len)
{
I2CStart();
I2CSendByte(0xa0);
I2CWaitAck();
I2CSendByte(addr);
I2CWaitAck();
I2CStart();
I2CSendByte(0xa1);
I2CWaitAck();
while(len--){
*data++ = I2CReceiveByte();
if(len) I2CSendAck();
else I2CSendNotAck();
}
I2CStop();
}
上面是cubemx生成的初始化代码不用管,下面的按键扫描函数和led控制函数都需要自己写,具体使用在主函数。
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file gpio.c
* @brief This file provides code for the configuration
* of all used GPIO pins.
******************************************************************************
* @attention
*
* Copyright (c) 2022 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 "gpio.h"
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/*----------------------------------------------------------------------------*/
/* Configure GPIO */
/*----------------------------------------------------------------------------*/
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/** Configure pins as
* Analog
* Input
* Output
* EVENT_OUT
* EXTI
*/
void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOF_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15|GPIO_PIN_8
|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12, GPIO_PIN_SET);
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_RESET);
/*Configure GPIO pins : PC13 PC14 PC15 PC8
PC9 PC10 PC11 PC12 */
GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15|GPIO_PIN_8
|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
/*Configure GPIO pin : PA0 */
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/*Configure GPIO pins : PB0 PB1 PB2 */
GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/*Configure GPIO pin : PD2 */
GPIO_InitStruct.Pin = GPIO_PIN_2;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
}
/* USER CODE BEGIN 2 */
// 按键扫描函数
uint8_t KEY_Scan()
{
uint8_t value = 0;
if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_0) == GPIO_PIN_RESET) value = 1;
if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_1) == GPIO_PIN_RESET) value = 2;
if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_2) == GPIO_PIN_RESET) value = 3;
if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == GPIO_PIN_RESET) value = 4;
return value;
}
// LED控制函数
void LED_Disp(uint8_t leds)
{
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15|GPIO_PIN_8
|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOC, leds << 8, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_RESET);
}
/* USER CODE END 2 */