stm32f103c8t6电子密码锁程序
时间: 2023-08-26 11:07:14 浏览: 130
以下是一个简单的基于STM32F103C8T6的电子密码锁程序:
```c
#include "stm32f10x.h"
#include "stdio.h"
#include "string.h"
#define PASSWORD_LENGTH 6
#define MAX_ATTEMPTS 3
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
uint8_t password[PASSWORD_LENGTH] = {'1', '2', '3', '4', '5', '6'};
uint8_t attempt_count = 0;
uint8_t password_index = 0;
uint8_t input_buffer[PASSWORD_LENGTH];
uint8_t input_index = 0;
uint8_t is_locked = 1;
void GPIO_Configuration(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void USART_Configuration(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
USART_InitStructure.USART_BaudRate = 9600;
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_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
}
void NVIC_Configuration(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void TIM_Configuration(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_TimeBaseStructure.TIM_Period = 2000 - 1;
TIM_TimeBaseStructure.TIM_Prescaler = 7200 - 1;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
TIM_Cmd(TIM2, ENABLE);
}
void TIM2_IRQHandler(void)
{
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) {
USART_SendData(USART1, 'L');
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
}
}
void check_password(void)
{
if (memcmp(input_buffer, password, PASSWORD_LENGTH) == 0) {
USART_SendData(USART1, 'U');
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
attempt_count = 0;
input_index = 0;
} else {
++attempt_count;
if (attempt_count >= MAX_ATTEMPTS) {
is_locked = 1;
USART_SendData(USART1, 'E');
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
} else {
USART_SendData(USART1, 'F');
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
input_index = 0;
}
}
}
int main(void)
{
GPIO_Configuration();
USART_Configuration();
NVIC_Configuration();
TIM_Configuration();
while (1) {
if (!is_locked) {
if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0)) {
input_buffer[input_index++] = '1';
while (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0));
}
if (input_index == PASSWORD_LENGTH) {
check_password();
}
} else {
USART_SendData(USART1, 'L');
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
}
}
}
```
这个程序使用USART1发送指定的字符以进行锁定,解锁或错误提示。 它还使用定时器TIM2来检测锁定状态并在锁定状态下发送“ L”以指示锁定状态。 它使用GPIOA0作为输入以输入密码。 每当按下按钮时,它将'1'添加到输入缓冲区中,并在输入完成时检查密码。 如果输入与密码匹配,则将尝试计数器重置为0并清除输入缓冲区。 如果输入与密码不匹配,则将尝试计数器递增,并在达到最大尝试次数时将锁定标志设置为1以防止进一步的密码输入。
阅读全文