STM32(HAL库 ) AS608光学指纹模块驱动(中断接收方式)
STM32 AS608 光學(xué)指紋模塊通過(guò)串口打印測(cè)試:(百分百HAL庫(kù))
引腳連接:
AS608模塊 TXD 連接單片機(jī)RXT,RXD連接TXD,touch引腳可以隨便接一個(gè)IO 引腳,最后一根線(xiàn)接3.3V,touch引腳主要用于判斷是否有手指按下,本文測(cè)試沒(méi)用到,如果需要?jiǎng)t需判斷該引腳狀態(tài),然后再發(fā)送數(shù)據(jù)包,這樣可以有效提高代碼效率;本文測(cè)試使用STM32的串口3引腳作為AS608連接引腳。
一、使用STM32CUBEMX配置
串口3作為AS608通信引腳配置如下:
中斷使能:
注意:上面引腳波特率應(yīng)該與你AS608模塊波特率一致,我這里使用115200
串口1作為DEBUG測(cè)試引腳配置如下:
時(shí)鐘配置:
配置完成生成工程代碼即可。
二、代碼編寫(xiě)
1、as608.c文件
2、as608.h文件
#ifndef __AS608_H #define __AS608_H#include "stm32f1xx_hal.h"#define CharBuffer1 0x01 #define CharBuffer2 0x02#define USART3_MAX_RECV_LEN 400 //最大接收緩存字節(jié)數(shù)#define AS608_UART huart3//AS608模塊所使用的串口extern uint8_t USART3_RX_BUF[USART3_MAX_RECV_LEN];extern uint8_t USART3_RX_STA;extern uint32_t AS608Addr;//模塊地址typedef struct {uint16_t pageID;//指紋IDuint16_t mathscore;//匹配得分 }SearchResult;typedef struct {uint16_t PS_max;//指紋最大容量uint8_t PS_level;//安全等級(jí)uint32_t PS_addr;uint8_t PS_size;//通訊數(shù)據(jù)包大小uint8_t PS_N;//波特率基數(shù)N }SysPara;uint8_t as608_init(void);uint8_t PS_GetImage(void); //錄入圖像 uint8_t PS_GenChar(uint8_t BufferID);//生成特征 uint8_t PS_Match(void);//精確比對(duì)兩枚指紋特征 uint8_t PS_Search(uint8_t BufferID,uint16_t StartPage,uint16_t PageNum,SearchResult *p);//搜索指紋 uint8_t PS_RegModel(void);//合并特征(生成模板) uint8_t PS_StoreChar(uint8_t BufferID,uint16_t PageID);//儲(chǔ)存模板 uint8_t PS_DeletChar(uint16_t PageID,uint16_t N);//刪除模板 uint8_t PS_Empty(void);//清空指紋庫(kù) uint8_t PS_WriteReg(uint8_t RegNum,uint8_t DATA);//寫(xiě)系統(tǒng)寄存器 uint8_t PS_ReadSysPara(SysPara *p); //讀系統(tǒng)基本參數(shù) uint8_t PS_SetAddr(uint32_t addr); //設(shè)置模塊地址 uint8_t PS_WriteNotepad(uint8_t NotePageNum,uint8_t *content);//寫(xiě)記事本 uint8_t PS_ReadNotepad(uint8_t NotePageNum,uint8_t *note);//讀記事 uint8_t PS_HighSpeedSearch(uint8_t BufferID,uint16_t StartPage,uint16_t PageNum,SearchResult *p);//高速搜索 uint8_t PS_ValidTempleteNum(uint16_t *ValidN);//讀有效模板個(gè)數(shù) uint8_t PS_HandShake(uint32_t *PS_Addr); //與AS608模塊握手const char *EnsureMessage(uint8_t ensure);//確認(rèn)碼錯(cuò)誤信息解析void Add_FR(void);void press_FR(void);void Del_FR(void);void Del_FR_Lib(void);#endif如果串口使用不一樣,只需修改頭文件中宏定義:
#define AS608_UART huart3//AS608模塊所使用的串口
即可。
由于通信接收用的是中斷接收,所以還需編寫(xiě)中斷接收部分代碼,并且在不知道要接收多長(zhǎng)的數(shù)據(jù)前提下,只能利用串口空閑中斷方式來(lái)進(jìn)行不定長(zhǎng)數(shù)據(jù)接收;
空閑中斷:
顧名思義,就是需要判斷串口接收總線(xiàn)狀態(tài)、如果處于繁忙狀態(tài),則還有數(shù)據(jù)正在發(fā)送過(guò)來(lái),如果處于空閑,則表明數(shù)據(jù)接收完成;所以只需要判斷串口接收總線(xiàn)的狀態(tài)。
在使用之前需要對(duì)該串口進(jìn)行初始化,初始化函數(shù)如下:
在初始化時(shí)需要使能中斷接收,以及使能空閑中斷,之后對(duì)模塊進(jìn)行檢測(cè),調(diào)用模塊檢測(cè)函數(shù)發(fā)送相關(guān)指令、如果模塊錯(cuò)誤或者未連接返回1,成功返回0。
中斷處理函數(shù)如下,只需在對(duì)應(yīng)的串口中斷添加以下代碼即可:
/*** @brief This function handles USART3 global interrupt.*/ void USART3_IRQHandler(void) {/* USER CODE BEGIN USART3_IRQn 0 *///串口接收不定長(zhǎng)數(shù)據(jù)//判斷是否為空閑中斷,如果是就認(rèn)為接收數(shù)據(jù)完成 if(__HAL_UART_GET_FLAG(&AS608_UART,UART_FLAG_IDLE) != RESET){//認(rèn)為數(shù)據(jù)接收完成,進(jìn)行處理//1、清除空閑中斷__HAL_UART_CLEAR_IDLEFLAG(&AS608_UART);//2、獲取接收大小//3、清空接收狀態(tài)AS608_UART.RxXferCount = sizeof(USART3_RX_BUF);AS608_UART.pRxBuffPtr = USART3_RX_BUF;USART3_RX_STA = 1;//接收數(shù)據(jù)完成return ; }/* USER CODE END USART3_IRQn 0 */HAL_UART_IRQHandler(&huart3);/* USER CODE BEGIN USART3_IRQn 1 *//* USER CODE END USART3_IRQn 1 */ }驅(qū)動(dòng)程序到此編寫(xiě)完成。
實(shí)際應(yīng)用示例:
main.c
/* 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 "usart.h" #include "gpio.h"/* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include "as608.h" #include <stdio.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 *//* USER CODE END PV *//* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); /* USER CODE BEGIN PFP *//* USER CODE END PFP *//* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ /***************************************** 函數(shù)名: 參數(shù):無(wú) 功能描述:printf輸出重定向到串口1 返回值: *****************************************/ int fputc(int ch,FILE *f){uint8_t temp[1]={ch};HAL_UART_Transmit(&huart1,temp,1,2);return ch; } /* USER CODE END 0 *//*** @brief The application entry point.* @retval int*/ int main(void) {/* USER CODE BEGIN 1 */uint8_t res = 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_USART2_UART_Init();MX_USART3_UART_Init();/* USER CODE BEGIN 2 */printf("AS608指紋模塊測(cè)試開(kāi)始\r\n");res = as608_init();if(res == 0){printf("AS608指紋模塊初始化成功\r\n");}elseprintf("AS608指紋模塊初始化失敗\r\n");/* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */while (1){/* USER CODE END WHILE *//* USER CODE BEGIN 3 *///注意:這里測(cè)試都是通過(guò)串口,沒(méi)有按鍵和OLED,所以測(cè)試時(shí)只能一個(gè)函數(shù)一個(gè)函數(shù)測(cè)試//Add_FR();//添加指紋,默認(rèn)ID為1, 可根據(jù)實(shí)際需要修改press_FR();//刷指紋測(cè)試//Del_FR_Lib();//刪除指紋庫(kù)}/* USER CODE END 3 */ }/*** @brief System Clock Configuration* @retval None*/ void SystemClock_Config(void) {RCC_OscInitTypeDef RCC_OscInitStruct = {0};RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};/** 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.HSEPredivValue = RCC_HSE_PREDIV_DIV1;RCC_OscInitStruct.HSIState = RCC_HSI_ON;RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;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_DIV2;RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK){Error_Handler();} }/* USER CODE BEGIN 4 *//* 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 */測(cè)試結(jié)果:
測(cè)試成功。
完整工程鏈接:
https://pan.baidu.com/s/1NYAyaCEsx5vJbCZV6vQ3lQ 提取碼: rqgq
總結(jié)
以上是生活随笔為你收集整理的STM32(HAL库 ) AS608光学指纹模块驱动(中断接收方式)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Java实现的OA考勤管理系统、基于SS
- 下一篇: COM学习(三)——COM的跨语言