一、前言
ATK AS608 指纹识别模块 是 ALI EN TEK 推出的一款高 性能 的光学指纹识别模块。
ATK AS608 模块采用 了 国内 著名 指纹识别芯片公司杭州晟元芯片技术有限公司的 AS608 指纹识别芯片。 芯片内置 DSP 运算单元,集成了指纹识别算法,能高效快速 采集图像并 识别指纹特征。 模块配备了串口、 USB 通讯接口,用户无需研究复杂的图像处理及指纹识别算法,只需通过简单的串口、 USB 按照通讯协议便可控制模块。 本模块可应用于各种考勤机、保险箱 柜 、指纹门禁系统、指纹锁等场合。
二、设备参数
三、资料获取
关注微信公众号--星之援工作室 发送关键字(AS608)
➡️➡️
四、主要代码编写
AS608主要使用到了串口,使用串口协议进行对接,所以需要初始化一下我们的一个串口进行使用
usart3.c
#include "delay.h"
#include "usart3.h"
#include "stdarg.h"
#include "stdio.h"
#include "string.h"
#include "timer.h"
//串口接收缓存区
u8 USART3_RX_BUF[USART3_MAX_RECV_LEN]; //接收缓冲,最大USART3_MAX_RECV_LEN个字节.
u8 USART3_TX_BUF[USART3_MAX_SEND_LEN]; //发送缓冲,最大USART3_MAX_SEND_LEN字节
//通过判断接收连续2个字符之间的时间差不大于10ms来决定是不是一次连续的数据.
//如果2个字符接收间隔超过10ms,则认为不是1次连续数据.也就是超过10ms没有接收到
//任何数据,则表示此次接收完毕.
//接收到的数据状态
//[15]:0,没有接收到数据;1,接收到了一批数据.
//[14:0]:接收到的数据长度
vu16 USART3_RX_STA=0;
void USART3_IRQHandler(void)
{
u8 res;
if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)//接收到数据
{
res =USART_ReceiveData(USART3);
if((USART3_RX_STA&(1<<15))==0)//接收完的一批数据,还没有被处理,则不再接收其他数据
{
if(USART3_RX_STA<USART3_MAX_RECV_LEN) //还可以接收数据
{
TIM_SetCounter(TIM7,0);//计数器清空 //计数器清空
if(USART3_RX_STA==0) //使能定时器7的中断
{
TIM_Cmd(TIM7,ENABLE);//使能定时器7
}
USART3_RX_BUF[USART3_RX_STA++]=res; //记录接收到的值
}else
{
USART3_RX_STA|=1<<15; //强制标记接收完成
}
}
}
}
//初始化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); //复位串口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; //复用推挽输出
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;//波特率设置
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
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(USART3, &USART_InitStructure); //初始化串口2
USART_Cmd(USART3, ENABLE); //使能串口
//使能接收中断
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);//开启中断
//设置中断优先级
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2 ;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
TIM7_Int_Init(99,7199); //10ms中断
USART3_RX_STA=0; //清零
TIM_Cmd(TIM7,DISABLE); //关闭定时器7
}
//串口2,printf 函数
//确保一次发送数据不超过USART3_MAX_SEND_LEN字节
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); //此次发送数据的长度
for(j=0;j<i;j++) //循环发送数据
{
while(USART_GetFlagStatus(USART3,USART_FLAG_TC)==RESET); //循环发送,直到发送完毕
USART_SendData(USART3,USART3_TX_BUF[j]);
}
}
usart3.h
#ifndef __USART3_H
#define __USART3_H
#include "sys.h"
#define USART3_MAX_RECV_LEN 400 //最大接收缓存字节数
#define USART3_MAX_SEND_LEN 400 //最大发送缓存字节数
#define USART3_RX_EN 1 //0,不接收;1,接收.
extern u8 USART3_RX_BUF[USART3_MAX_RECV_LEN]; //接收缓冲,最大USART3_MAX_RECV_LEN字节
extern u8 USART3_TX_BUF[USART3_MAX_SEND_LEN]; //发送缓冲,最大USART3_MAX_SEND_LEN字节
extern vu16 USART3_RX_STA; //接收数据状态
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; //默认
char str2[6] = {0};
//串口发送一个字节
static void MYUSART_SendData(u8 data)
{
while((USART3->SR & 0X40) == 0);
USART3->DR = data;
}
//发送包头
static void SendHead(void)
{
MYUSART_SendData(0xEF);
MYUSART_SendData(0x01);
}
//发送地址
static void SendAddr(void)
{
MYUSART_SendData(AS608Addr >> 24);
MYUSART_SendData(AS608Addr >> 16);
MYUSART_SendData(AS608Addr >> 8);
MYUSART_SendData(AS608Addr);
}
//发送包标识,
static void SendFlag(u8 flag)
{
MYUSART_SendData(flag);
}
//发送包长度
static void SendLength(int length)
{
MYUSART_SendData(length >> 8);
MYUSART_SendData(length);
}
//发送指令码
static void Sendcmd(u8 cmd)
{
MYUSART_SendData(cmd);
}
//发送校验和
static void SendCheck(u16 check)
{
MYUSART_SendData(check >> 8);
MYUSART_SendData(check);
}
//判断中断接收的数组有没有应答包
//waittime为等待中断接收数据的时间(单位1ms)
//返回值:数据包首地址
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) //接收到一次数据
{
USART3_RX_STA = 0;
data = strstr((const char*)USART3_RX_BUF, (const char*)str);
if(data)
return (u8*)data;
}
}
return 0;
}
//录入图像 PS_GetImage
//功能:探测手指,探测到后录入指纹图像存于ImageBuffer。
//模块返回确认字
u8 PS_GetImage(void)
{
u16 temp;
u8 ensure;
u8 *data;
SendHead();
SendAddr();
SendFlag(0x01);//命令包标识
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
//参数:BufferID --> charBuffer1:0x01 charBuffer1:0x02
//模块返回确认字
u8 PS_GenChar(u8 BufferID)
{
u16 temp;
u8 ensure;
u8 *data;
SendHead();
SendAddr();
SendFlag(0x01);//命令包标识
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 中的特征文件
//模块返回确认字
u8 PS_Match(void)
{
u16 temp;
u8 ensure;
u8 *data;
SendHead();
SendAddr();
SendFlag(0x01);//命令包标识
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中的特征文件搜索整个或部分指纹库.若搜索到,则返回页码。
//参数: BufferID @ref CharBuffer1 CharBuffer2
//说明: 模块返回确认字,页码(相配指纹模板)
u8 PS_Search(u8 BufferID, u16 StartPage, u16 PageNum, SearchResult *p)
{
u16 temp;
u8 ensure;
u8 *data;
SendHead();
SendAddr();
SendFlag(0x01);//命令包标识
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中的特征文件合并生成 模板,结果存于CharBuffer1与CharBuffer2
//说明: 模块返回确认字
u8 PS_RegModel(void)
{
u16 temp;
u8 ensure;
u8 *data;
SendHead();
SendAddr();
SendFlag(0x01);//命令包标识
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数据库位置。
//参数: BufferID @ref charBuffer1:0x01 charBuffer1:0x02
// PageID(指纹库位置号)
//说明: 模块返回确认字
u8 PS_StoreChar(u8 BufferID, u16 PageID)
{
u16 temp;
u8 ensure;
u8 *data;
SendHead();
SendAddr();
SendFlag(0x01);//命令包标识
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数据库中指定ID号开始的N个指纹模板
//参数: PageID(指纹库模板号),N删除的模板个数。
//说明: 模块返回确认字
u8 PS_DeletChar(u16 PageID, u16 N)
{
u16 temp;
u8 ensure;
u8 *data;
SendHead();
SendAddr();
SendFlag(0x01);//命令包标识
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数据库中所有指纹模板
//参数: 无
//说明: 模块返回确认字
u8 PS_Empty(void)
{
u16 temp;
u8 ensure;
u8 *data;
SendHead();
SendAddr();
SendFlag(0x01);//命令包标识
SendLength(0x03);
Sendcmd(0x0D);
temp = 0x01 + 0x03 + 0x0D;
SendCheck(temp);
data = JudgeStr(2000);
if(data)
ensure = data[9];
else
ensure = 0xff;
return ensure;
}
//写系统寄存器 PS_WriteReg
//功能: 写模块寄存器
//参数: 寄存器序号RegNum:456
//说明: 模块返回确认字
u8 PS_WriteReg(u8 RegNum, u8 DATA)
{
u16 temp;
u8 ensure;
u8 *data;
SendHead();
SendAddr();
SendFlag(0x01);//命令包标识
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设置参数成功!");
else
printf("rn%s", EnsureMessage(ensure));
return ensure;
}
//读系统基本参数 PS_ReadSysPara
//功能: 读取模块的基本参数(波特率,包大小等)
//参数: 无
//说明: 模块返回确认字 + 基本参数(16bytes)
u8 PS_ReadSysPara(SysPara *p)
{
u16 temp;
u8 ensure;
u8 *data;
SendHead();
SendAddr();
SendFlag(0x01);//命令包标识
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;
}
//设置模块地址 PS_SetAddr
//功能: 设置模块地址
//参数: PS_addr
//说明: 模块返回确认字
u8 PS_SetAddr(u32 PS_addr)
{
u16 temp;
u8 ensure;
u8 *data;
SendHead();
SendAddr();
SendFlag(0x01);//命令包标识
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; //发送完指令,更换地址
data = JudgeStr(2000);
if(data)
ensure = data[9];
else
ensure = 0xff;
AS608Addr = PS_addr;
if(ensure == 0x00)
printf("rn设置地址成功!");
else
printf("rn%s", EnsureMessage(ensure));
return ensure;
}
//功能: 模块内部为用户开辟了256bytes的FLASH空间用于存用户记事本,
// 该记事本逻辑上被分成 16 个页。
//参数: NotePageNum(0~15),Byte32(要写入内容,32个字节)
//说明: 模块返回确认字
u8 PS_WriteNotepad(u8 NotePageNum, u8 *Byte32)
{
u16 temp;
u8 ensure, i;
u8 *data;
SendHead();
SendAddr();
SendFlag(0x01);//命令包标识
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用户区的128bytes数据
//参数: NotePageNum(0~15)
//说明: 模块返回确认字+用户信息
u8 PS_ReadNotepad(u8 NotePageNum, u8 *Byte32)
{
u16 temp;
u8 ensure, i;
u8 *data;
SendHead();
SendAddr();
SendFlag(0x01);//命令包标识
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中的特征文件高速搜索整个或部分指纹库。
// 若搜索到,则返回页码,该指令对于的确存在于指纹库中 ,且登录时质量
// 很好的指纹,会很快给出搜索结果。
//参数: BufferID, StartPage(起始页),PageNum(页数)
//说明: 模块返回确认字+页码(相配指纹模板)
u8 PS_HighSpeedSearch(u8 BufferID, u16 StartPage, u16 PageNum, SearchResult *p)
{
u16 temp;
u8 ensure;
u8 *data;
SendHead();
SendAddr();
SendFlag(0x01);//命令包标识
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;
}
//读有效模板个数 PS_ValidTempleteNum
//功能:读有效模板个数
//参数: 无
//说明: 模块返回确认字+有效模板个数ValidN
u8 PS_ValidTempleteNum(u16 *ValidN)
{
u16 temp;
u8 ensure;
u8 *data;
SendHead();
SendAddr();
SendFlag(0x01);//命令包标识
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有效指纹个数=%d", (data[10] << 8) + data[11]);
}
else
printf("rn%s", EnsureMessage(ensure));
return ensure;
}
//与AS608握手 PS_HandShake
//参数: 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) //接收到数据
{
if(//判断是不是模块返回的应答包
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;
}
//模块应答包确认码信息解析
//功能:解析确认码错误信息返回信息
//参数: ensure
const char *EnsureMessage(u8 ensure)
{
const char *p;
switch(ensure)
{
case 0x00:
p = " OK ";
break;
case 0x01:
p = " 数据包接收错误 ";
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 = "缓冲区内无有效图";
break;
case 0x18:
p = " 读写FLASH出错 ";
break;
case 0x19:
p = " 未定义错误 ";
break;
case 0x1a:
p = " 无效寄存器号 ";
break;
case 0x1b:
p = " 寄存器内容错误 ";
break;
case 0x1c:
p = " 记事本页码错误 ";
break;
case 0x1f:
p = " 指纹库满 ";
break;
case 0x20:
p = " 地址错误 ";
break;
default :
p = " 返回确认码有误 ";
break;
}
return p;
}
//显示确认码错误信息
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参数
//刷指纹
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确认");
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)//读指纹模块状态引脚
#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;//通讯数据包大小
u8 PS_N;//波特率基数N
}SysPara;
void PS_StaGPIO_Init(void);//初始化PA6读状态引脚
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);//写系统寄存器
u8 PS_ReadSysPara(SysPara *p); //读系统基本参数
u8 PS_SetAddr(u32 addr); //设置模块地址
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);//读有效模板个数
u8 PS_HandShake(u32 *PS_Addr); //与AS608模块握手
const char *EnsureMessage(u8 ensure);//确认码错误信息解析
void Add_FR(void);
void press_FR(void);
void Del_FR(void);
#endif
五、参考
物联网毕设 -- 指纹密码锁(GSM+STM32+指纹)https://blog.csdn.net/herui_2/article/details/133907334?spm=1001.2014.3001.5502
AS608指纹识别模块+STM32实现指纹录入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
完整代码请关注公众号进行获取和咨询
联系方式 微信号:13648103287