一、前言
ATK AS608 指紋識別模塊 是 ALI EN TEK 推出的一款高 性能 的光學(xué)指紋識別模塊。
ATK AS608 模塊采用 了 國內(nèi) 著名 指紋識別芯片公司杭州晟元芯片技術(shù)有限公司的 AS608 指紋識別芯片。 芯片內(nèi)置 DSP 運算單元,集成了指紋識別算法,能高效快速 采集圖像并 識別指紋特征。 模塊配備了串口、 USB 通訊接口,用戶無需研究復(fù)雜的圖像處理及指紋識別算法,只需通過簡單的串口、 USB 按照通訊協(xié)議便可控制模塊。 本模塊可應(yīng)用于各種考勤機、保險箱 柜 、指紋門禁系統(tǒng)、指紋鎖等場合。
二、設(shè)備參數(shù)
三、資料獲取
關(guān)注微信公眾號--星之援工作室 發(fā)送關(guān)鍵字(AS608)
????
四、主要代碼編寫
AS608主要使用到了串口,使用串口協(xié)議進行對接,所以需要初始化一下我們的一個串口進行使用
usart3.c
#include "delay.h"
#include "usart3.h"
#include "stdarg.h"
#include "stdio.h"
#include "string.h"
#include "timer.h"
//串口接收緩存區(qū)
u8 USART3_RX_BUF[USART3_MAX_RECV_LEN]; //接收緩沖,最大USART3_MAX_RECV_LEN個字節(jié).
u8 USART3_TX_BUF[USART3_MAX_SEND_LEN]; //發(fā)送緩沖,最大USART3_MAX_SEND_LEN字節(jié)
//通過判斷接收連續(xù)2個字符之間的時間差不大于10ms來決定是不是一次連續(xù)的數(shù)據(jù).
//如果2個字符接收間隔超過10ms,則認(rèn)為不是1次連續(xù)數(shù)據(jù).也就是超過10ms沒有接收到
//任何數(shù)據(jù),則表示此次接收完畢.
//接收到的數(shù)據(jù)狀態(tài)
//[15]:0,沒有接收到數(shù)據(jù);1,接收到了一批數(shù)據(jù).
//[14:0]:接收到的數(shù)據(jù)長度
vu16 USART3_RX_STA=0;
void USART3_IRQHandler(void)
{
u8 res;
if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)//接收到數(shù)據(jù)
{
res =USART_ReceiveData(USART3);
if((USART3_RX_STA&(1<<15))==0)//接收完的一批數(shù)據(jù),還沒有被處理,則不再接收其他數(shù)據(jù)
{
if(USART3_RX_STA<USART3_MAX_RECV_LEN) //還可以接收數(shù)據(jù)
{
TIM_SetCounter(TIM7,0);//計數(shù)器清空 //計數(shù)器清空
if(USART3_RX_STA==0) //使能定時器7的中斷
{
TIM_Cmd(TIM7,ENABLE);//使能定時器7
}
USART3_RX_BUF[USART3_RX_STA++]=res; //記錄接收到的值
}else
{
USART3_RX_STA|=1<<15; //強制標(biāo)記接收完成
}
}
}
}
//初始化IO 串口2
//pclk1:PCLK1時鐘頻率(Mhz)
//bound:波特率
void usart3_init(u32 bound)
{
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); // GPIOA時鐘
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE); //串口2時鐘使能
USART_DeInit(USART3); //復(fù)位串口2
//USART3_TX PA2
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //PA2
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //復(fù)用推挽輸出
GPIO_Init(GPIOB, &GPIO_InitStructure); //初始化PA2
//USART3_RX PA3
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空輸入
GPIO_Init(GPIOB, &GPIO_InitStructure); //初始化PA3
USART_InitStructure.USART_BaudRate = bound;//波特率設(shè)置
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字長為8位數(shù)據(jù)格式
USART_InitStructure.USART_StopBits = USART_StopBits_1;//一個停止位
USART_InitStructure.USART_Parity = USART_Parity_No;//無奇偶校驗位
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//無硬件數(shù)據(jù)流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收發(fā)模式
USART_Init(USART3, &USART_InitStructure); //初始化串口2
USART_Cmd(USART3, ENABLE); //使能串口
//使能接收中斷
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);//開啟中斷
//設(shè)置中斷優(yōu)先級
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2 ;//搶占優(yōu)先級3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子優(yōu)先級3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根據(jù)指定的參數(shù)初始化VIC寄存器
TIM7_Int_Init(99,7199); //10ms中斷
USART3_RX_STA=0; //清零
TIM_Cmd(TIM7,DISABLE); //關(guān)閉定時器7
}
//串口2,printf 函數(shù)
//確保一次發(fā)送數(shù)據(jù)不超過USART3_MAX_SEND_LEN字節(jié)
void u2_printf(char* fmt,...)
{
u16 i,j;
va_list ap;
va_start(ap,fmt);
vsprintf((char*)USART3_TX_BUF,fmt,ap);
va_end(ap);
i=strlen((const char*)USART3_TX_BUF); //此次發(fā)送數(shù)據(jù)的長度
for(j=0;j<i;j++) //循環(huán)發(fā)送數(shù)據(jù)
{
while(USART_GetFlagStatus(USART3,USART_FLAG_TC)==RESET); //循環(huán)發(fā)送,直到發(fā)送完畢
USART_SendData(USART3,USART3_TX_BUF[j]);
}
}
usart3.h
#ifndef __USART3_H
#define __USART3_H
#include "sys.h"
#define USART3_MAX_RECV_LEN 400 //最大接收緩存字節(jié)數(shù)
#define USART3_MAX_SEND_LEN 400 //最大發(fā)送緩存字節(jié)數(shù)
#define USART3_RX_EN 1 //0,不接收;1,接收.
extern u8 USART3_RX_BUF[USART3_MAX_RECV_LEN]; //接收緩沖,最大USART3_MAX_RECV_LEN字節(jié)
extern u8 USART3_TX_BUF[USART3_MAX_SEND_LEN]; //發(fā)送緩沖,最大USART3_MAX_SEND_LEN字節(jié)
extern vu16 USART3_RX_STA; //接收數(shù)據(jù)狀態(tài)
void usart3_init(u32 bound); //串口3初始化
void u2_printf(char* fmt,...);
#endif
AS608.c
#include <string.h>
#include <stdio.h>
#include "delay.h"
#include "usart3.h"
#include "as608.h"
#include "oled_iic.h"
#include "key.h"
u32 AS608Addr = 0XFFFFFFFF; //默認(rèn)
char str2[6] = {0};
//串口發(fā)送一個字節(jié)
static void MYUSART_SendData(u8 data)
{
while((USART3->SR & 0X40) == 0);
USART3->DR = data;
}
//發(fā)送包頭
static void SendHead(void)
{
MYUSART_SendData(0xEF);
MYUSART_SendData(0x01);
}
//發(fā)送地址
static void SendAddr(void)
{
MYUSART_SendData(AS608Addr >> 24);
MYUSART_SendData(AS608Addr >> 16);
MYUSART_SendData(AS608Addr >> 8);
MYUSART_SendData(AS608Addr);
}
//發(fā)送包標(biāo)識,
static void SendFlag(u8 flag)
{
MYUSART_SendData(flag);
}
//發(fā)送包長度
static void SendLength(int length)
{
MYUSART_SendData(length >> 8);
MYUSART_SendData(length);
}
//發(fā)送指令碼
static void Sendcmd(u8 cmd)
{
MYUSART_SendData(cmd);
}
//發(fā)送校驗和
static void SendCheck(u16 check)
{
MYUSART_SendData(check >> 8);
MYUSART_SendData(check);
}
//判斷中斷接收的數(shù)組有沒有應(yīng)答包
//waittime為等待中斷接收數(shù)據(jù)的時間(單位1ms)
//返回值:數(shù)據(jù)包首地址
static u8 *JudgeStr(u16 waittime)
{
char *data;
u8 str[8];
str[0] = 0xef;
str[1] = 0x01;
str[2] = AS608Addr >> 24;
str[3] = AS608Addr >> 16;
str[4] = AS608Addr >> 8;
str[5] = AS608Addr;
str[6] = 0x07;
str[7] = '?';
USART3_RX_STA = 0;
while(--waittime)
{
delay_ms(1);
if(USART3_RX_STA & 0X8000) //接收到一次數(shù)據(jù)
{
USART3_RX_STA = 0;
data = strstr((const char*)USART3_RX_BUF, (const char*)str);
if(data)
return (u8*)data;
}
}
return 0;
}
//錄入圖像 PS_GetImage
//功能:探測手指,探測到后錄入指紋圖像存于ImageBuffer。
//模塊返回確認(rèn)字
u8 PS_GetImage(void)
{
u16 temp;
u8 ensure;
u8 *data;
SendHead();
SendAddr();
SendFlag(0x01);//命令包標(biāo)識
SendLength(0x03);
Sendcmd(0x01);
temp = 0x01 + 0x03 + 0x01;
SendCheck(temp);
data = JudgeStr(2000);
if(data)
ensure = data[9];
else
ensure = 0xff;
return ensure;
}
//生成特征 PS_GenChar
//功能:將ImageBuffer中的原始圖像生成指紋特征文件存于CharBuffer1或CharBuffer2
//參數(shù):BufferID --> charBuffer1:0x01 charBuffer1:0x02
//模塊返回確認(rèn)字
u8 PS_GenChar(u8 BufferID)
{
u16 temp;
u8 ensure;
u8 *data;
SendHead();
SendAddr();
SendFlag(0x01);//命令包標(biāo)識
SendLength(0x04);
Sendcmd(0x02);
MYUSART_SendData(BufferID);
temp = 0x01 + 0x04 + 0x02 + BufferID;
SendCheck(temp);
data = JudgeStr(2000);
if(data)
ensure = data[9];
else
ensure = 0xff;
return ensure;
}
//精確比對兩枚指紋特征 PS_Match
//功能:精確比對CharBuffer1 與CharBuffer2 中的特征文件
//模塊返回確認(rèn)字
u8 PS_Match(void)
{
u16 temp;
u8 ensure;
u8 *data;
SendHead();
SendAddr();
SendFlag(0x01);//命令包標(biāo)識
SendLength(0x03);
Sendcmd(0x03);
temp = 0x01 + 0x03 + 0x03;
SendCheck(temp);
data = JudgeStr(2000);
if(data)
ensure = data[9];
else
ensure = 0xff;
return ensure;
}
//搜索指紋 PS_Search
//功能:以CharBuffer1或CharBuffer2中的特征文件搜索整個或部分指紋庫.若搜索到,則返回頁碼。
//參數(shù): BufferID @ref CharBuffer1 CharBuffer2
//說明: 模塊返回確認(rèn)字,頁碼(相配指紋模板)
u8 PS_Search(u8 BufferID, u16 StartPage, u16 PageNum, SearchResult *p)
{
u16 temp;
u8 ensure;
u8 *data;
SendHead();
SendAddr();
SendFlag(0x01);//命令包標(biāo)識
SendLength(0x08);
Sendcmd(0x04);
MYUSART_SendData(BufferID);
MYUSART_SendData(StartPage >> 8);
MYUSART_SendData(StartPage);
MYUSART_SendData(PageNum >> 8);
MYUSART_SendData(PageNum);
temp = 0x01 + 0x08 + 0x04 + BufferID
+ (StartPage >> 8) + (u8)StartPage
+ (PageNum >> 8) + (u8)PageNum;
SendCheck(temp);
data = JudgeStr(2000);
if(data)
{
ensure = data[9];
p->pageID = (data[10] << 8) + data[11];
p->mathscore = (data[12] << 8) + data[13];
}
else
ensure = 0xff;
return ensure;
}
//合并特征(生成模板)PS_RegModel
//功能:將CharBuffer1與CharBuffer2中的特征文件合并生成 模板,結(jié)果存于CharBuffer1與CharBuffer2
//說明: 模塊返回確認(rèn)字
u8 PS_RegModel(void)
{
u16 temp;
u8 ensure;
u8 *data;
SendHead();
SendAddr();
SendFlag(0x01);//命令包標(biāo)識
SendLength(0x03);
Sendcmd(0x05);
temp = 0x01 + 0x03 + 0x05;
SendCheck(temp);
data = JudgeStr(2000);
if(data)
ensure = data[9];
else
ensure = 0xff;
return ensure;
}
//儲存模板 PS_StoreChar
//功能:將 CharBuffer1 或 CharBuffer2 中的模板文件存到 PageID 號flash數(shù)據(jù)庫位置。
//參數(shù): BufferID @ref charBuffer1:0x01 charBuffer1:0x02
// PageID(指紋庫位置號)
//說明: 模塊返回確認(rèn)字
u8 PS_StoreChar(u8 BufferID, u16 PageID)
{
u16 temp;
u8 ensure;
u8 *data;
SendHead();
SendAddr();
SendFlag(0x01);//命令包標(biāo)識
SendLength(0x06);
Sendcmd(0x06);
MYUSART_SendData(BufferID);
MYUSART_SendData(PageID >> 8);
MYUSART_SendData(PageID);
temp = 0x01 + 0x06 + 0x06 + BufferID
+ (PageID >> 8) + (u8)PageID;
SendCheck(temp);
data = JudgeStr(2000);
if(data)
ensure = data[9];
else
ensure = 0xff;
return ensure;
}
//刪除模板 PS_DeletChar
//功能: 刪除flash數(shù)據(jù)庫中指定ID號開始的N個指紋模板
//參數(shù): PageID(指紋庫模板號),N刪除的模板個數(shù)。
//說明: 模塊返回確認(rèn)字
u8 PS_DeletChar(u16 PageID, u16 N)
{
u16 temp;
u8 ensure;
u8 *data;
SendHead();
SendAddr();
SendFlag(0x01);//命令包標(biāo)識
SendLength(0x07);
Sendcmd(0x0C);
MYUSART_SendData(PageID >> 8);
MYUSART_SendData(PageID);
MYUSART_SendData(N >> 8);
MYUSART_SendData(N);
temp = 0x01 + 0x07 + 0x0C
+ (PageID >> 8) + (u8)PageID
+ (N >> 8) + (u8)N;
SendCheck(temp);
data = JudgeStr(2000);
if(data)
ensure = data[9];
else
ensure = 0xff;
return ensure;
}
//清空指紋庫 PS_Empty
//功能: 刪除flash數(shù)據(jù)庫中所有指紋模板
//參數(shù): 無
//說明: 模塊返回確認(rèn)字
u8 PS_Empty(void)
{
u16 temp;
u8 ensure;
u8 *data;
SendHead();
SendAddr();
SendFlag(0x01);//命令包標(biāo)識
SendLength(0x03);
Sendcmd(0x0D);
temp = 0x01 + 0x03 + 0x0D;
SendCheck(temp);
data = JudgeStr(2000);
if(data)
ensure = data[9];
else
ensure = 0xff;
return ensure;
}
//寫系統(tǒng)寄存器 PS_WriteReg
//功能: 寫模塊寄存器
//參數(shù): 寄存器序號RegNum:456
//說明: 模塊返回確認(rèn)字
u8 PS_WriteReg(u8 RegNum, u8 DATA)
{
u16 temp;
u8 ensure;
u8 *data;
SendHead();
SendAddr();
SendFlag(0x01);//命令包標(biāo)識
SendLength(0x05);
Sendcmd(0x0E);
MYUSART_SendData(RegNum);
MYUSART_SendData(DATA);
temp = RegNum + DATA + 0x01 + 0x05 + 0x0E;
SendCheck(temp);
data = JudgeStr(2000);
if(data)
ensure = data[9];
else
ensure = 0xff;
if(ensure == 0)
printf("rn設(shè)置參數(shù)成功!");
else
printf("rn%s", EnsureMessage(ensure));
return ensure;
}
//讀系統(tǒng)基本參數(shù) PS_ReadSysPara
//功能: 讀取模塊的基本參數(shù)(波特率,包大小等)
//參數(shù): 無
//說明: 模塊返回確認(rèn)字 + 基本參數(shù)(16bytes)
u8 PS_ReadSysPara(SysPara *p)
{
u16 temp;
u8 ensure;
u8 *data;
SendHead();
SendAddr();
SendFlag(0x01);//命令包標(biāo)識
SendLength(0x03);
Sendcmd(0x0F);
temp = 0x01 + 0x03 + 0x0F;
SendCheck(temp);
data = JudgeStr(1000);
if(data)
{
ensure = data[9];
p->PS_max = (data[14] << 8) + data[15];
p->PS_level = data[17];
p->PS_addr = (data[18] << 24) + (data[19] << 16) + (data[20] << 8) + data[21];
p->PS_size = data[23];
p->PS_N = data[25];
}
else
ensure = 0xff;
if(ensure == 0x00)
{
printf("rn模塊最大指紋容量=%d", p->PS_max);
printf("rn對比等級=%d", p->PS_level);
printf("rn地址=%x", p->PS_addr);
printf("rn波特率=%d", p->PS_N * 9600);
}
else
printf("rn%s", EnsureMessage(ensure));
return ensure;
}
//設(shè)置模塊地址 PS_SetAddr
//功能: 設(shè)置模塊地址
//參數(shù): PS_addr
//說明: 模塊返回確認(rèn)字
u8 PS_SetAddr(u32 PS_addr)
{
u16 temp;
u8 ensure;
u8 *data;
SendHead();
SendAddr();
SendFlag(0x01);//命令包標(biāo)識
SendLength(0x07);
Sendcmd(0x15);
MYUSART_SendData(PS_addr >> 24);
MYUSART_SendData(PS_addr >> 16);
MYUSART_SendData(PS_addr >> 8);
MYUSART_SendData(PS_addr);
temp = 0x01 + 0x07 + 0x15
+ (u8)(PS_addr >> 24) + (u8)(PS_addr >> 16)
+ (u8)(PS_addr >> 8) + (u8)PS_addr;
SendCheck(temp);
AS608Addr = PS_addr; //發(fā)送完指令,更換地址
data = JudgeStr(2000);
if(data)
ensure = data[9];
else
ensure = 0xff;
AS608Addr = PS_addr;
if(ensure == 0x00)
printf("rn設(shè)置地址成功!");
else
printf("rn%s", EnsureMessage(ensure));
return ensure;
}
//功能: 模塊內(nèi)部為用戶開辟了256bytes的FLASH空間用于存用戶記事本,
// 該記事本邏輯上被分成 16 個頁。
//參數(shù): NotePageNum(0~15),Byte32(要寫入內(nèi)容,32個字節(jié))
//說明: 模塊返回確認(rèn)字
u8 PS_WriteNotepad(u8 NotePageNum, u8 *Byte32)
{
u16 temp;
u8 ensure, i;
u8 *data;
SendHead();
SendAddr();
SendFlag(0x01);//命令包標(biāo)識
SendLength(36);
Sendcmd(0x18);
MYUSART_SendData(NotePageNum);
for(i = 0; i < 32; i++)
{
MYUSART_SendData(Byte32[i]);
temp += Byte32[i];
}
temp = 0x01 + 36 + 0x18 + NotePageNum + temp;
SendCheck(temp);
data = JudgeStr(2000);
if(data)
ensure = data[9];
else
ensure = 0xff;
return ensure;
}
//讀記事PS_ReadNotepad
//功能: 讀取FLASH用戶區(qū)的128bytes數(shù)據(jù)
//參數(shù): NotePageNum(0~15)
//說明: 模塊返回確認(rèn)字+用戶信息
u8 PS_ReadNotepad(u8 NotePageNum, u8 *Byte32)
{
u16 temp;
u8 ensure, i;
u8 *data;
SendHead();
SendAddr();
SendFlag(0x01);//命令包標(biāo)識
SendLength(0x04);
Sendcmd(0x19);
MYUSART_SendData(NotePageNum);
temp = 0x01 + 0x04 + 0x19 + NotePageNum;
SendCheck(temp);
data = JudgeStr(2000);
if(data)
{
ensure = data[9];
for(i = 0; i < 32; i++)
{
Byte32[i] = data[10 + i];
}
}
else
ensure = 0xff;
return ensure;
}
//高速搜索PS_HighSpeedSearch
//功能:以 CharBuffer1或CharBuffer2中的特征文件高速搜索整個或部分指紋庫。
// 若搜索到,則返回頁碼,該指令對于的確存在于指紋庫中 ,且登錄時質(zhì)量
// 很好的指紋,會很快給出搜索結(jié)果。
//參數(shù): BufferID, StartPage(起始頁),PageNum(頁數(shù))
//說明: 模塊返回確認(rèn)字+頁碼(相配指紋模板)
u8 PS_HighSpeedSearch(u8 BufferID, u16 StartPage, u16 PageNum, SearchResult *p)
{
u16 temp;
u8 ensure;
u8 *data;
SendHead();
SendAddr();
SendFlag(0x01);//命令包標(biāo)識
SendLength(0x08);
Sendcmd(0x1b);
MYUSART_SendData(BufferID);
MYUSART_SendData(StartPage >> 8);
MYUSART_SendData(StartPage);
MYUSART_SendData(PageNum >> 8);
MYUSART_SendData(PageNum);
temp = 0x01 + 0x08 + 0x1b + BufferID
+ (StartPage >> 8) + (u8)StartPage
+ (PageNum >> 8) + (u8)PageNum;
SendCheck(temp);
data = JudgeStr(2000);
if(data)
{
ensure = data[9];
p->pageID = (data[10] << 8) + data[11];
p->mathscore = (data[12] << 8) + data[13];
}
else
ensure = 0xff;
return ensure;
}
//讀有效模板個數(shù) PS_ValidTempleteNum
//功能:讀有效模板個數(shù)
//參數(shù): 無
//說明: 模塊返回確認(rèn)字+有效模板個數(shù)ValidN
u8 PS_ValidTempleteNum(u16 *ValidN)
{
u16 temp;
u8 ensure;
u8 *data;
SendHead();
SendAddr();
SendFlag(0x01);//命令包標(biāo)識
SendLength(0x03);
Sendcmd(0x1d);
temp = 0x01 + 0x03 + 0x1d;
SendCheck(temp);
data = JudgeStr(2000);
if(data)
{
ensure = data[9];
*ValidN = (data[10] << 8) + data[11];
}
else
ensure = 0xff;
if(ensure == 0x00)
{
printf("rn有效指紋個數(shù)=%d", (data[10] << 8) + data[11]);
}
else
printf("rn%s", EnsureMessage(ensure));
return ensure;
}
//與AS608握手 PS_HandShake
//參數(shù): PS_Addr地址指針
//說明: 模塊返新地址(正確地址)
u8 PS_HandShake(u32 *PS_Addr)
{
SendHead();
SendAddr();
MYUSART_SendData(0X01);
MYUSART_SendData(0X00);
MYUSART_SendData(0X00);
delay_ms(200);
if(USART3_RX_STA & 0X8000) //接收到數(shù)據(jù)
{
if(//判斷是不是模塊返回的應(yīng)答包
USART3_RX_BUF[0] == 0XEF
&& USART3_RX_BUF[1] == 0X01
&& USART3_RX_BUF[6] == 0X07
)
{
*PS_Addr = (USART3_RX_BUF[2] << 24) + (USART3_RX_BUF[3] << 16)
+ (USART3_RX_BUF[4] << 8) + (USART3_RX_BUF[5]);
USART3_RX_STA = 0;
return 0;
}
USART3_RX_STA = 0;
}
return 1;
}
//模塊應(yīng)答包確認(rèn)碼信息解析
//功能:解析確認(rèn)碼錯誤信息返回信息
//參數(shù): ensure
const char *EnsureMessage(u8 ensure)
{
const char *p;
switch(ensure)
{
case 0x00:
p = " OK ";
break;
case 0x01:
p = " 數(shù)據(jù)包接收錯誤 ";
break;
case 0x02:
p = "傳感器上沒有手指";
break;
case 0x03:
p = "錄入指紋圖像失敗";
break;
case 0x04:
p = " 指紋太干或太淡 ";
break;
case 0x05:
p = " 指紋太濕或太糊 ";
break;
case 0x06:
p = " 指紋圖像太亂 ";
break;
case 0x07:
p = " 指紋特征點太少 ";
break;
case 0x08:
p = " 指紋不匹配 ";
break;
case 0x09:
p = " 沒有搜索到指紋 ";
break;
case 0x0a:
p = " 特征合并失敗 ";
break;
case 0x0b:
p = "地址序號超出范圍";
case 0x10:
p = " 刪除模板失敗 ";
break;
case 0x11:
p = " 清空指紋庫失敗 ";
break;
case 0x15:
p = "緩沖區(qū)內(nèi)無有效圖";
break;
case 0x18:
p = " 讀寫FLASH出錯 ";
break;
case 0x19:
p = " 未定義錯誤 ";
break;
case 0x1a:
p = " 無效寄存器號 ";
break;
case 0x1b:
p = " 寄存器內(nèi)容錯誤 ";
break;
case 0x1c:
p = " 記事本頁碼錯誤 ";
break;
case 0x1f:
p = " 指紋庫滿 ";
break;
case 0x20:
p = " 地址錯誤 ";
break;
default :
p = " 返回確認(rèn)碼有誤 ";
break;
}
return p;
}
//顯示確認(rèn)碼錯誤信息
void ShowErrMessage(u8 ensure)
{
//OLED_ShowCH(5,0,(u8*)EnsureMessage(ensure));
}
//錄指紋
void Add_FR(void)
{
u8 i, ensure, processnum = 0;
u8 ID_NUM = 0;
while(1)
{
switch (processnum)
{
case 0:
i++;
OLED_ShowCH(0, 2, " 請按手指 ");
ensure = PS_GetImage();
if(ensure == 0x00)
{
ensure = PS_GenChar(CharBuffer1); //生成特征
if(ensure == 0x00)
{
OLED_ShowCH(0, 2, " 指紋正常 ");
OLED_ShowCH(0, 4, " ");
i = 0;
processnum = 1; //跳到第二步
}
else ShowErrMessage(ensure);
}
else ShowErrMessage(ensure);
break;
case 1:
i++;
OLED_ShowCH(0, 2, " 請再按一次 ");
OLED_ShowCH(0, 4, " ");
ensure = PS_GetImage();
if(ensure == 0x00)
{
ensure = PS_GenChar(CharBuffer2); //生成特征
if(ensure == 0x00)
{
OLED_ShowCH(0, 2, " 指紋正常 ");
OLED_ShowCH(0, 4, " ");
i = 0;
processnum = 2; //跳到第三步
}
else ShowErrMessage(ensure);
}
else ShowErrMessage(ensure);
break;
case 2:
OLED_ShowCH(0, 2, " 對比兩次指紋 ");
OLED_ShowCH(0, 4, " ");
ensure = PS_Match();
if(ensure == 0x00)
{
OLED_ShowCH(0, 2, " 對比成功 ");
OLED_ShowCH(0, 4, " ");
processnum = 3; //跳到第四步
}
else
{
OLED_ShowCH(0, 2, " 對比失敗 ");
OLED_ShowCH(0, 4, " ");
ShowErrMessage(ensure);
i = 0;
processnum = 0; //跳回第一步
}
delay_ms(500);
break;
case 3:
OLED_ShowCH(0, 2, " 生成指紋模板 ");
OLED_ShowCH(0, 4, " ");
delay_ms(500);
ensure = PS_RegModel();
if(ensure == 0x00)
{
OLED_ShowCH(0, 2, "生成指紋模板成功");
OLED_ShowCH(0, 4, " ");
processnum = 4; //跳到第五步
}
else
{
processnum = 0;
ShowErrMessage(ensure);
}
delay_ms(1000);
break;
case 4:
OLED_ShowCH(0, 0, " 按K4加,按K2減 ");
OLED_ShowCH(0, 2, " 按K3保存 ");
OLED_ShowCH(0, 4, " 0=< ID <=99 ");
while(key_num != 3)
{
key_num = KEY_Scan(0);
if(key_num == 2)
{
key_num = 0;
if(ID_NUM > 0)
ID_NUM--;
}
if(key_num == 4)
{
key_num = 0;
if(ID_NUM < 99)
ID_NUM++;
}
OLED_ShowCH(40, 6, "ID=");
OLED_ShowNum(65, 6, ID_NUM, 2, 1);
}
key_num = 0;
ensure = PS_StoreChar(CharBuffer2, ID_NUM); //儲存模板
if(ensure == 0x00)
{
OLED_Clear();
OLED_ShowCH(0, 2, " 錄入指紋成功 ");
OLED_ShowCH(0, 4, " ");
delay_ms(1500);
OLED_Clear();
OLED_ShowCH(0, 0, "指紋模塊測試程序");
OLED_ShowCH(16, 2, "K1鍵添加指紋");
OLED_ShowCH(16, 4, "K3鍵刪除指紋");
OLED_ShowCH(16, 6, "K5鍵驗證指紋");
return ;
}
else
{
OLED_Clear();
processnum = 0;
ShowErrMessage(ensure);
}
break;
}
delay_ms(400);
if(i == 10) //超過5次沒有按手指則退出
{
break;
}
}
}
SysPara AS608Para;//指紋模塊AS608參數(shù)
//刷指紋
void press_FR(void)
{
SearchResult seach;
u8 ensure;
char str[20];
while(key_num != 1)
{
key_num = KEY_Scan(0);
ensure = PS_GetImage();
if(ensure == 0x00) //獲取圖像成功
{
ensure = PS_GenChar(CharBuffer1);
if(ensure == 0x00) //生成特征成功
{
ensure = PS_HighSpeedSearch(CharBuffer1, 0, 99, &seach);
if(ensure == 0x00) //搜索成功
{
OLED_ShowCH(0, 2, " 指紋驗證成功 ");
sprintf(str, " ID:%d 得分:%d ", seach.pageID, seach.mathscore);
OLED_ShowCH(0, 4, (u8*)str);
delay_ms(1500);
delay_ms(1500);
}
else
{
OLED_ShowCH(32, 2, "驗證失敗");
delay_ms(1500);
}
}
else
{};
OLED_Clear();
OLED_ShowCH(32, 2, "請按手指");
}
}
OLED_Clear();
OLED_ShowCH(0, 0, "指紋模塊測試程序");
OLED_ShowCH(16, 2, "K1鍵添加指紋");
OLED_ShowCH(16, 4, "K3鍵刪除指紋");
OLED_ShowCH(16, 6, "K5鍵驗證指紋");
}
//刪除指紋
void Del_FR(void)
{
u8 ensure;
u16 ID_NUM = 0;
OLED_ShowCH(0, 0, "K4加 K2減 K3確認(rèn)");
OLED_ShowCH(0, 2, " K5清空指紋庫 ");
OLED_ShowCH(0, 4, "K1返回 0=<ID<=99");
while(key_num != 3)
{
key_num = KEY_Scan(0);
if(key_num == 2)
{
key_num = 0;
if(ID_NUM > 0)
ID_NUM--;
}
if(key_num == 4)
{
key_num = 0;
if(ID_NUM < 99)
ID_NUM++;
}
if(key_num == 1)
goto MENU ; //返回主頁面
if(key_num == 5)
{
key_num = 0;
ensure = PS_Empty(); //清空指紋庫
if(ensure == 0)
{
OLED_Clear();
OLED_ShowCH(0, 2, " 清空指紋庫成功 ");
}
else
ShowErrMessage(ensure);
delay_ms(1500);
goto MENU ; //返回主頁面
}
OLED_ShowCH(40, 6, "ID=");
OLED_ShowNum(65, 6, ID_NUM, 2, 1);
}
ensure = PS_DeletChar(ID_NUM, 1); //刪除單個指紋
if(ensure == 0)
{
OLED_Clear();
OLED_ShowCH(0, 2, " 刪除指紋成功 ");
}
else
ShowErrMessage(ensure);
delay_ms(1500);
MENU:
OLED_Clear();
OLED_ShowCH(0, 0, "指紋模塊測試程序");
OLED_ShowCH(16, 2, "K1鍵添加指紋");
OLED_ShowCH(16, 4, "K3鍵刪除指紋");
OLED_ShowCH(16, 6, "K5鍵驗證指紋");
key_num = 0;
}
AS608.h
#ifndef __AS608_H
#define __AS608_H
#include <stdio.h>
#include "stm32f10x.h"
#define PS_Sta PAin(6)//讀指紋模塊狀態(tài)引腳
#define CharBuffer1 0x01
#define CharBuffer2 0x02
extern u32 AS608Addr;//模塊地址
typedef struct
{
u16 pageID;//指紋ID
u16 mathscore;//匹配得分
}SearchResult;
typedef struct
{
u16 PS_max;//指紋最大容量
u8 PS_level;//安全等級
u32 PS_addr;
u8 PS_size;//通訊數(shù)據(jù)包大小
u8 PS_N;//波特率基數(shù)N
}SysPara;
void PS_StaGPIO_Init(void);//初始化PA6讀狀態(tài)引腳
u8 PS_GetImage(void); //錄入圖像
u8 PS_GenChar(u8 BufferID);//生成特征
u8 PS_Match(void);//精確比對兩枚指紋特征
u8 PS_Search(u8 BufferID,u16 StartPage,u16 PageNum,SearchResult *p);//搜索指紋
u8 PS_RegModel(void);//合并特征(生成模板)
u8 PS_StoreChar(u8 BufferID,u16 PageID);//儲存模板
u8 PS_DeletChar(u16 PageID,u16 N);//刪除模板
u8 PS_Empty(void);//清空指紋庫
u8 PS_WriteReg(u8 RegNum,u8 DATA);//寫系統(tǒng)寄存器
u8 PS_ReadSysPara(SysPara *p); //讀系統(tǒng)基本參數(shù)
u8 PS_SetAddr(u32 addr); //設(shè)置模塊地址
u8 PS_WriteNotepad(u8 NotePageNum,u8 *content);//寫記事本
u8 PS_ReadNotepad(u8 NotePageNum,u8 *note);//讀記事
u8 PS_HighSpeedSearch(u8 BufferID,u16 StartPage,u16 PageNum,SearchResult *p);//高速搜索
u8 PS_ValidTempleteNum(u16 *ValidN);//讀有效模板個數(shù)
u8 PS_HandShake(u32 *PS_Addr); //與AS608模塊握手
const char *EnsureMessage(u8 ensure);//確認(rèn)碼錯誤信息解析
void Add_FR(void);
void press_FR(void);
void Del_FR(void);
#endif
五、參考
物聯(lián)網(wǎng)畢設(shè) -- 指紋密碼鎖(GSM+STM32+指紋)https://blog.csdn.net/herui_2/article/details/133907334?spm=1001.2014.3001.5502
AS608指紋識別模塊+STM32實現(xiàn)指紋錄入https://blog.csdn.net/lllmeimei/article/details/123541494?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522170218427616800182794955%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=170218427616800182794955&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~top_click~default-2-123541494-null-null.142%5Ev96%5Epc_search_result_base7&utm_term=AS608&spm=1018.2226.3001.4187
完整代碼請關(guān)注公眾號進行獲取和咨詢
聯(lián)系方式 微信號:13648103287