STM32 矩阵键盘密码验证:8位密码输入与 OLED 显示
#include "stm32f10x.h" #include "stm32f10x_gpio.h" #include "stm32f10x_rcc.h" #include "stm32f10x_usart.h" #include "stm32f10x_i2c.h" #include "stdio.h" #include "stdlib.h" #include "string.h" #include "oled.h"
#define ROW_NUM 4 #define COL_NUM 4 #define PASSWORD_LENGTH 8
char password[PASSWORD_LENGTH] = '12345678'; char input[PASSWORD_LENGTH];
void delay_ms(uint16_t ms) { while (ms--) { uint16_t count = 16000; while (count--); } }
void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
uint8_t ReadRow(void) { uint8_t row = 0; row |= (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_11) << 3); row |= (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_12) << 2); row |= (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_13) << 1); row |= GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_14); return row; }
uint8_t ReadCol(void) { uint8_t col = 0; col |= (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_4) << 3); col |= (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_5) << 2); col |= (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_6) << 1); col |= GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_7); return col; }
uint8_t GetKeyPress(void) { uint8_t row, col; GPIO_ResetBits(GPIOC, GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7); GPIO_SetBits(GPIOC, GPIO_Pin_4); col = ReadCol(); if (col != 0) { while (ReadCol() != 0); return col; }
GPIO_ResetBits(GPIOC, GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7);
GPIO_SetBits(GPIOC, GPIO_Pin_5);
col = ReadCol();
if (col != 0) {
while (ReadCol() != 0);
return col + 4;
}
GPIO_ResetBits(GPIOC, GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7);
GPIO_SetBits(GPIOC, GPIO_Pin_6);
col = ReadCol();
if (col != 0) {
while (ReadCol() != 0);
return col + 8;
}
GPIO_ResetBits(GPIOC, GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7);
GPIO_SetBits(GPIOC, GPIO_Pin_7);
col = ReadCol();
if (col != 0) {
while (ReadCol() != 0);
return col + 12;
}
return 0;
}
void USART_Configuration(void) { USART_InitTypeDef USART_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
}
void USART_SendString(char* str) { while (*str) { while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); USART_SendData(USART1, *str++); } }
void Buzzer_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void Buzzer_On(void) { GPIO_SetBits(GPIOA, GPIO_Pin_8); }
void Buzzer_Off(void) { GPIO_ResetBits(GPIOA, GPIO_Pin_8); }
void OLED_Display(char* str) { OLED_Clear(); OLED_ShowString(0, 0, str); OLED_Refresh_Gram(); }
int main(void) { GPIO_Configuration(); USART_Configuration(); Buzzer_Init(); OLED_Init();
OLED_Display("Please enter password:");
uint8_t index = 0;
while (1) {
uint8_t key = GetKeyPress();
if (key != 0) {
input[index++] = key;
Buzzer_On();
delay_ms(100);
Buzzer_Off();
if (index == PASSWORD_LENGTH) {
if (strcmp(password, input) == 0) {
OLED_Display("Pass!");
USART_SendString("Pass!\r\n");
delay_ms(1000);
Buzzer_On();
delay_ms(500);
Buzzer_Off();
delay_ms(500);
Buzzer_On();
delay_ms(500);
Buzzer_Off();
} else {
OLED_Display("Warning!");
USART_SendString("Warning!\r\n");
while (1) {
Buzzer_On();
delay_ms(250);
Buzzer_Off();
delay_ms(250);
}
}
}
}
}
原文地址: https://www.cveoy.top/t/topic/o89W 著作权归作者所有。请勿转载和采集!