stm32 learning record - scan code to unlock

Table of contents

usart.c

hardware connection

Baidu network disk source code


I use a code scanning module for serial communication, which can recognize the content of the QR code and send it to the microcontroller directly through the serial port.

The modules used include code scanning modules, relays, and electronic locks.

The essence is that the code scanning module scans the code and transmits the content of the code to the single-chip microcomputer for judgment through the serial port. After identifying the content of the code, it controls the switch of the relay, and the relay further controls the electronic lock.

The MCU judges that this step is actually a serial port code

usart.c

#include "config.h"
#include "Usart.h"

u8 USART_RxBUF[USART_RXBUF_SIZE];
u16 USART1_RecPos=0;


void Usart_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1,ENABLE);
	
	
 GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;
 GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
 GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
		
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
 GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
		
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	
	
	USART_InitStructure.USART_BaudRate=USART_BAUD;
	USART_InitStructure.USART_WordLength=USART_WordLength_8b;
	USART_InitStructure.USART_StopBits=USART_StopBits_1;
	USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Parity=USART_Parity_No;
	USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
	
	USART_Init(USART1,&USART_InitStructure);
	
	NVIC_InitStructure.NVIC_IRQChannel=USART1_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority=2;
	NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	
	USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
	
	USART_Cmd(USART1,ENABLE);
	USART_ClearFlag(USART1,USART_FLAG_TC);
	
}
void UsartSendByte(USART_TypeDef* USARTx,u8 ch)
{
	USART_SendData(USARTx,(u16)ch);
	while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET);
}
void UsartSendString(USART_TypeDef*USARTx,u8*str)
{
	u32 pos=0;
	while(*(str+pos)!='\0')
	{
		UsartSendByte(USARTx,*(str+pos));
		pos++;
	}
	
}
int fputc(int ch,FILE*f)
{
	USART_SendData(USART1,(u16)ch);
	while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET);
	return (ch);
	
}
void USART1_IRQHandler(void)
{
	u8 RecCh;
	if(USART_GetITStatus(USART1,USART_IT_RXNE)==SET)
	{
		USART1RecTimer=10;
		RecCh= (u8)USART_ReceiveData(USART1);
		USART_RxBUF[USART1_RecPos++]=RecCh;
		USART_ClearITPendingBit(USART1,USART_IT_RXNE);
	}
	
	
}
unsigned char USRET1_RecProcess(void)
{
	if(USART1RecTimer)return FALSE;
	if(!USART1_RecPos)return FALSE;
	USART_RxBUF[USART1_RecPos++]='\0';
	
	if(strstr((char *)USART_RxBUF,"码的内容")!=NULL)
	{
		relay_off();
		SysTickDelayMs(200);
		relay_on();
	}
	memset(USART_RxBUF,0,USART_RXBUF_SIZE);
	USART1_RecPos=0;
	return TRUE;
}

Functions to set judgment and control relays

unsigned char USRET1_RecProcess(void)
{
	if(USART1RecTimer)return FALSE;
	if(!USART1_RecPos)return FALSE;
	USART_RxBUF[USART1_RecPos++]='\0';
	
	if(strstr((char *)USART_RxBUF,"码的内容")!=NULL)
	{
		relay_off();
		SysTickDelayMs(200);
		relay_on();
	}
	memset(USART_RxBUF,0,USART_RXBUF_SIZE);
	USART1_RecPos=0;
	return TRUE;
}

To set the content of the code, you can download a QR code generator.

hardware connection

The rx of the code scanning module——Single-chip microcomputer PA9

tx of the scanning module——Single-chip microcomputer PA10

Relay IN - PB7

Baidu network disk source code

Finally, here is the code I used for debugging.

Link: https://pan.baidu.com/s/1J5sy5ldL1jGp90VrwaHSAA 
Extraction code: 9438

Guess you like

Origin blog.csdn.net/sujiaxin12/article/details/123762236