STM32(HAL库 标准库通用) AS608光学指纹模块驱动

工作原理:

如果想要直接看代码,可以跳过原理,直接往下翻,代码在最后
AS608主要通过串口通信
1、缓冲区与指纹库
系统内设有一个 72K 字节的图像缓冲区与二个 512bytes 大小的特征文件缓冲区,名字分别称为:ImageBuffer,CharBuffer1 和 CharBuffer2。用户可以通过指令读写任意一个缓冲区。
CharBuffer1 或 CharBuffer2 既可以用于存放普通特征文件也可以用于存放模板特征文件。通过 UART 口上传或下载图像时为了加快速度,只用到像素字节的高 4 位,即将两个像素合
成一个字节传送。通过 USB 口则是整 8 位像素。
指纹库容量根据挂接的 FLASH 容量不同而改变,系统会自动判别。指纹模板按照序号存放,序号定义为:0—(N-1)(N 为指纹库容量)。用户只能根据序号访问指纹库内容。

2、用户记事本
系统在 FLASH 中开辟了一个 512 字节的存储区域作为用户记事本,该记事本逻辑上被分成 16 页,每页 32 字节。上位机可以通过 PS_WriteNotepad 指令和 PS_ReadNotepad 指令
访问任意一页。注意写记事本某一页的时候,该页 32 字节的内容被整体写入,原来的内容被覆盖。
3、随机数产生器
系统内部集成了硬件 32 位随机数生成器(不需要随机数种子),用户可以通过指令让模块产生一个随机数并上传给上位机。
4、模块地址 (大小:4bytes ,属性:读写)
模块的默认地址为0xFFFFFFFF,可通过指令修改,数据包的地址域必须与该地址相配,命令包/数据包才被系统接收。 注:与上位机通讯必须是默认地址 0xFFFFFFFF !
5、模块口令 (大小:4bytes ,属性:写)
系统默认口令为 0,可通过指令修改。若默认口令未被修改,则系统不要求验证口令,
上位机和 MCU 与芯片通讯;若口令被修改,则上位机与芯片通讯的第一个指令必须是验证
口令,只有口令验证通过后,芯片才接收其它指令。 注:不建议修改口令!
6、数据包大小设置(大小:1bytes ,属性:读写)
发送数据包和接收数据包的长度根据该值设定。
7、波特率数 系数 N 设置 (大小:1bytes ,属性:读写)
USART 波特率=N×9600,N=1~12。
8、安全等级 level 设置(大小:1bytes ,属性:读写)
系统根据安全等级设定比对阀值,level=1~5。安全等级为 1 时认假率最高,拒认率最低。
安全等级为 5 时认假率最低,拒认率最高。

指纹发送指令流程:获取指纹图像指令—>生成指纹特征指令----->搜索验证指纹指令

指令图解:

录入图像指令:
STM32(HAL库 标准库通用) AS608光学指纹模块驱动_第1张图片
搜索指纹指令:
STM32(HAL库 标准库通用) AS608光学指纹模块驱动_第2张图片
生成指纹特征指令:
STM32(HAL库 标准库通用) AS608光学指纹模块驱动_第3张图片
指纹通信协议格式:
STM32(HAL库 标准库通用) AS608光学指纹模块驱动_第4张图片以上就是指纹进行通信时的一些指令,根据这几张图,就可以写代码了。

代码部分:

我这里采用的是串口二,有需要自己改一下:
代码部分我主要讲把官方给的标准库的代码,进行简单移植,变为HAL库也能使用的代码,末尾也会附上标准库的代码
as608.c

#include 
#include 
#include "usart.h"
#include "as608.h"
#include "oled.h"
#include "key.h"

uint32_t AS608Addr = 0XFFFFFFFF; //默认
char str2[6] = {0};

//串口接收缓存区 	
uint8_t USART2_RX_BUF[USART2_MAX_RECV_LEN]; 				//接收缓冲,最大USART2_MAX_RECV_LEN个字节.
uint8_t USART2_TX_BUF[USART2_MAX_SEND_LEN]; 			  //发送缓冲,最大USART2_MAX_SEND_LEN字节

//通过判断接收连续2个字符之间的时间差不大于10ms来决定是不是一次连续的数据.
//如果2个字符接收间隔超过10ms,则认为不是1次连续数据.也就是超过10ms没有接收到
//任何数据,则表示此次接收完毕.
//接收到的数据状态
//[15]:0,没有接收到数据;1,接收到了一批数据.
//[14:0]:接收到的数据长度
__IO uint16_t USART2_RX_STA=0;



//串口发送一个字节
static void MYUSART_SendData(uint8_t data)
{
  while((USART2->SR & 0X40) == 0);
  USART2->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(uint8_t flag)
{
  MYUSART_SendData(flag);
}
//发送包长度
static void SendLength(int length)
{
  MYUSART_SendData(length >> 8);
  MYUSART_SendData(length);
}
//发送指令码
static void Sendcmd(uint8_t cmd)
{
  MYUSART_SendData(cmd);
}
//发送校验和
static void SendCheck(uint16_t check)
{
  MYUSART_SendData(check >> 8);
  MYUSART_SendData(check);
}
//判断中断接收的数组有没有应答包
//waittime为等待中断接收数据的时间(单位1ms)
//返回值:数据包首地址
static uint8_t *JudgeStr(uint16_t waittime)
{
  char *data;
  uint8_t 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] = '\0';
//  USART2_RX_STA = 0;
		//HAL库的写法
	HAL_UART_Receive(&huart2,(uint8_t *)USART2_RX_BUF,USART2_MAX_RECV_LEN,waittime/4);//串口三接收数据
	if(!memcmp(str,USART2_RX_BUF,7))//比对数据
	{
		  data = strstr((const char*)USART2_RX_BUF, (const char*)str);
      if(data)
        return (uint8_t*)data;
	}
	//
//  while(--waittime)
//  {
//    HAL_Delay(1);
//    if(USART2_RX_STA & 0X8000) //接收到一次数据
//    {
//      USART2_RX_STA = 0;
//      data = strstr((const char*)USART2_RX_BUF, (const char*)str);
//      if(data)
//        return (uint8_t*)data;
//    }
//  }
  return 0;
}
//录入图像 PS_GetImage
//功能:探测手指,探测到后录入指纹图像存于ImageBuffer。
//模块返回确认字
uint8_t PS_GetImage(void)
{
  uint16_t temp;
  uint8_t  ensure;
  uint8_t  *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
//模块返回确认字
uint8_t PS_GenChar(uint8_t BufferID)
{
  uint16_t temp;
  uint8_t  ensure;
  uint8_t  *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 中的特征文件
//模块返回确认字
uint8_t PS_Match(void)
{
  uint16_t temp;
  uint8_t  ensure;
  uint8_t  *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
//说明:  模块返回确认字,页码(相配指纹模板)
uint8_t PS_Search(uint8_t BufferID, uint16_t StartPage, uint16_t PageNum, SearchResult *p)
{
  uint16_t temp;
  uint8_t  ensure;
  uint8_t  *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) + (uint8_t)StartPage
         + (PageNum >> 8) + (uint8_t)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
//说明:  模块返回确认字
uint8_t PS_RegModel(void)
{
  uint16_t temp;
  uint8_t  ensure;
  uint8_t  *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(指纹库位置号)
//说明:  模块返回确认字
uint8_t PS_StoreChar(uint8_t BufferID, uint16_t PageID)
{
  uint16_t temp;
  uint8_t  ensure;
  uint8_t  *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) + (uint8_t)PageID;
  SendCheck(temp);
  data = JudgeStr(2000);
  if(data)
    ensure = data[9];
  else
    ensure = 0xff;
  return ensure;
}
//删除模板 PS_DeletChar
//功能:  删除flash数据库中指定ID号开始的N个指纹模板
//参数:  PageID(指纹库模板号),N删除的模板个数。
//说明:  模块返回确认字
uint8_t PS_DeletChar(uint16_t PageID, uint16_t N)
{
  uint16_t temp;
  uint8_t  ensure;
  uint8_t  *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) + (uint8_t)PageID
         + (N >> 8) + (uint8_t)N;
  SendCheck(temp);
  data = JudgeStr(2000);
  if(data)
    ensure = data[9];
  else
    ensure = 0xff;
  return ensure;
}
//清空指纹库 PS_Empty
//功能:  删除flash数据库中所有指纹模板
//参数:  无
//说明:  模块返回确认字
uint8_t PS_Empty(void)
{
  uint16_t temp;
  uint8_t  ensure;
  uint8_t  *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:4\5\6
//说明:  模块返回确认字
uint8_t PS_WriteReg(uint8_t RegNum, uint8_t DATA)
{
  uint16_t temp;
  uint8_t  ensure;
  uint8_t  *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("\r\n设置参数成功!");
  else
    printf("\r\n%s", EnsureMessage(ensure));
  return ensure;
}
//读系统基本参数 PS_ReadSysPara
//功能:  读取模块的基本参数(波特率,包大小等)
//参数:  无
//说明:  模块返回确认字 + 基本参数(16bytes)
uint8_t PS_ReadSysPara(SysPara *p)
{
  uint16_t temp;
  uint8_t  ensure;
  uint8_t  *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("\r\n模块最大指纹容量=%d", p->PS_max);
    printf("\r\n对比等级=%d", p->PS_level);
    printf("\r\n地址=%x", p->PS_addr);
    printf("\r\n波特率=%d", p->PS_N * 9600);
  }
  else
    printf("\r\n%s", EnsureMessage(ensure));
  return ensure;
}
//设置模块地址 PS_SetAddr
//功能:  设置模块地址
//参数:  PS_addr
//说明:  模块返回确认字
uint8_t PS_SetAddr(uint32_t PS_addr)
{
  uint16_t temp;
  uint8_t  ensure;
  uint8_t  *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
         + (uint8_t)(PS_addr >> 24) + (uint8_t)(PS_addr >> 16)
         + (uint8_t)(PS_addr >> 8) + (uint8_t)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("\r\n设置地址成功!");
  else
    printf("\r\n%s", EnsureMessage(ensure));
  return ensure;
}
//功能: 模块内部为用户开辟了256bytes的FLASH空间用于存用户记事本,
//	该记事本逻辑上被分成 16 个页。
//参数:  NotePageNum(0~15),Byte32(要写入内容,32个字节)
//说明:  模块返回确认字
uint8_t PS_WriteNotepad(uint8_t NotePageNum, uint8_t *Byte32)
{
  uint16_t temp;
  uint8_t  ensure, i;
  uint8_t  *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)
//说明:  模块返回确认字+用户信息
uint8_t PS_ReadNotepad(uint8_t NotePageNum, uint8_t *Byte32)
{
  uint16_t temp;
  uint8_t  ensure, i;
  uint8_t  *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(页数)
//说明:  模块返回确认字+页码(相配指纹模板)
uint8_t PS_HighSpeedSearch(uint8_t BufferID, uint16_t StartPage, uint16_t PageNum, SearchResult *p)
{
  uint16_t temp;
  uint8_t  ensure;
  uint8_t  *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) + (uint8_t)StartPage
         + (PageNum >> 8) + (uint8_t)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
uint8_t PS_ValidTempleteNum(uint16_t *ValidN)
{
  uint16_t temp;
  uint8_t  ensure;
  uint8_t  *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("\r\n有效指纹个数=%d", (data[10] << 8) + data[11]);
  }
  else
    printf("\r\n%s", EnsureMessage(ensure));
  return ensure;
}
//与AS608握手 PS_HandShake
//参数: PS_Addr地址指针
//说明: 模块返新地址(正确地址)
uint8_t PS_HandShake(uint32_t *PS_Addr)
{
  SendHead();
  SendAddr();
  MYUSART_SendData(0X01);
  MYUSART_SendData(0X00);
  MYUSART_SendData(0X00);
  HAL_Delay(200);
  if(USART2_RX_STA & 0X8000) //接收到数据
  {
    if(//判断是不是模块返回的应答包
      USART2_RX_BUF[0] == 0XEF
      && USART2_RX_BUF[1] == 0X01
      && USART2_RX_BUF[6] == 0X07
    )
    {
      *PS_Addr = (USART2_RX_BUF[2] << 24) + (USART2_RX_BUF[3] << 16)
                 + (USART2_RX_BUF[4] << 8) + (USART2_RX_BUF[5]);
      USART2_RX_STA = 0;
      return 0;
    }
    USART2_RX_STA = 0;
  }
  return 1;
}
//模块应答包确认码信息解析
//功能:解析确认码错误信息返回信息
//参数: ensure
const char *EnsureMessage(uint8_t 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(uint8_t ensure)
{
  //OLED_ShowMixedCH(5,0,(uint8_t*)EnsureMessage(ensure));
}


//录指纹
void Add_FR(void)
{
  uint8_t i, ensure, processnum = 0;
  uint8_t ID_NUM = 0;
	uint8_t key_num = 0;
  while(1)
  {
    switch (processnum)
    {
    case 0:
      i++;
			OLED_FullyClear();
      OLED_ShowMixedCH(0, 16, "    请按手指    ");
			OLED_RefreshRAM();
      ensure = PS_GetImage();
      if(ensure == 0x00)
      {
        ensure = PS_GenChar(CharBuffer1); //生成特征
        if(ensure == 0x00)
        {
          OLED_ShowMixedCH(0, 16, "    指纹正常    ");
          OLED_ShowMixedCH(0, 16, "                ");
					OLED_RefreshRAM();
          i = 0;
          processnum = 1; //跳到第二步
        }
        else ShowErrMessage(ensure);
      }
      else ShowErrMessage(ensure);
      break;

    case 1:
      i++;
      OLED_ShowMixedCH(0, 16, "   请再按一次   ");
      OLED_ShowMixedCH(0, 16, "                ");
	  	OLED_RefreshRAM();
      ensure = PS_GetImage();
      if(ensure == 0x00)
      {
        ensure = PS_GenChar(CharBuffer2); //生成特征
        if(ensure == 0x00)
        {
          OLED_ShowMixedCH(0, 16, "    指纹正常    ");
          OLED_ShowMixedCH(0, 32, "                ");
					OLED_RefreshRAM();
          i = 0;
          processnum = 2; //跳到第三步
        }
        else ShowErrMessage(ensure);
      }
      else ShowErrMessage(ensure);
      break;

    case 2:
      OLED_ShowMixedCH(0, 16, "  对比两次指纹  ");
      OLED_ShowMixedCH(0, 32, "                ");
			OLED_RefreshRAM();
      ensure = PS_Match();
      if(ensure == 0x00)
      {
        OLED_ShowMixedCH(0, 16, "    对比成功    ");
        OLED_ShowMixedCH(0, 32, "                ");
				OLED_RefreshRAM();
        processnum = 3; //跳到第四步
      }
      else
      {
        OLED_ShowMixedCH(0, 16, "    对比失败    ");
        OLED_ShowMixedCH(0, 32, "                ");
				OLED_RefreshRAM();
        ShowErrMessage(ensure);
        i = 0;
        processnum = 0; //跳回第一步
      }
      HAL_Delay(500);
      break;

    case 3:
      OLED_ShowMixedCH(0, 16, "  生成指纹模板  ");
      OLED_ShowMixedCH(0, 32, "                ");
			OLED_RefreshRAM();
      HAL_Delay(500);
      ensure = PS_RegModel();
      if(ensure == 0x00)
      {
        OLED_ShowMixedCH(0, 16, "生成指纹模板成功");
        OLED_ShowMixedCH(0, 32, "                ");
				OLED_RefreshRAM();
        processnum = 4; //跳到第五步
      }
      else
      {
        processnum = 0;
        ShowErrMessage(ensure);
      }
      HAL_Delay(1000);
      break;

    case 4:
			OLED_FullyClear();
      OLED_ShowMixedCH(0, 0, " 按K4加,按K2减 ");
      OLED_ShowMixedCH(0, 16, "    按K3保存    ");
      OLED_ShowMixedCH(0, 32, "  0=< ID <=99   ");
			OLED_RefreshRAM();
      while(key_num != KEYENTER_PRES)
      {
        key_num = key_scan();
        if(key_num == KEYDOWN_PRES)
        {
          key_num = 0;
          if(ID_NUM > 0)
            ID_NUM--;
        }
        if(key_num == KEYUP_PRES)
        {
          key_num = 0;
          if(ID_NUM < 99)
            ID_NUM++;
        }
				show_ID(40,48,ID_NUM);
      }
      key_num = 0;
      ensure = PS_StoreChar(CharBuffer2, ID_NUM); //储存模板
      if(ensure == 0x00)
      {
        OLED_FullyClear();
        OLED_ShowMixedCH(0, 16, "  录入指纹成功  ");
        OLED_ShowMixedCH(0, 32, "                ");
				OLED_RefreshRAM();
        HAL_Delay(1500);
        OLED_FullyClear();
        OLED_ShowMixedCH(0, 0, "指纹模块测试程序");
        OLED_ShowMixedCH(16, 16, "K1键添加指纹");
        OLED_ShowMixedCH(16, 32, "K3键删除指纹");
        OLED_ShowMixedCH(16, 48, "K5键验证指纹");
				OLED_RefreshRAM();
        return ;
      }
      else
      {
        OLED_FullyClear();
        processnum = 0;
        ShowErrMessage(ensure);
      }
      break;
    }
    HAL_Delay(400);
    if(i == 10) //超过5次没有按手指则退出
    {
      break;
    }
  }
}

SysPara AS608Para;//指纹模块AS608参数
//刷指纹
void press_FR(void)
{
  SearchResult seach;
  uint8_t ensure;
  char str[20];
	uint8_t key_num = 0;
  while(key_num != KEYBACK_PRES)
  {
    key_num = key_scan();
    ensure = PS_GetImage();
    if(ensure == 0x00) //获取图像成功
    {
      ensure = PS_GenChar(CharBuffer1);
      if(ensure == 0x00) //生成特征成功
      {
        ensure = PS_HighSpeedSearch(CharBuffer1, 0, 99, &seach);
        if(ensure == 0x00) //搜索成功
        {
					OLED_FullyClear();
          OLED_ShowMixedCH(0, 16, "  指纹验证成功  ");
          sprintf(str, " ID:%d 得分:%d ", seach.pageID, seach.mathscore);
          OLED_ShowMixedCH(0, 32, (uint8_t*)str);
					OLED_RefreshRAM();
          HAL_Delay(1500);
          HAL_Delay(1500);
        }
        else
        {
          OLED_ShowMixedCH(32, 16, "验证失败");
					OLED_RefreshRAM();
          HAL_Delay(1500);
        }
      }
      else
			{};
      OLED_FullyClear();
      OLED_ShowMixedCH(32, 16, "请按手指");
			OLED_RefreshRAM();
    }
  }
  OLED_FullyClear();
  OLED_ShowMixedCH(0, 0, "指纹模块测试程序");
  OLED_ShowMixedCH(16, 16, "K1键添加指纹");
  OLED_ShowMixedCH(16, 32, "K3键删除指纹");
  OLED_ShowMixedCH(16, 48, "K5键验证指纹");
	OLED_RefreshRAM();
}

//删除指纹
void Del_FR(void)
{
  uint8_t  ensure;
  uint16_t ID_NUM = 0;
	uint8_t key_num = 0;
	OLED_FullyClear();
  OLED_ShowMixedCH(0, 16, "K4加 K2减 K3确认");
  OLED_ShowMixedCH(0, 32, "K1返回 0=);
	OLED_RefreshRAM();
  while(key_num != KEYENTER_PRES)
  {
    key_num = key_scan();
    if(key_num == KEYDOWN_PRES)
    {
      key_num = 0;
      if(ID_NUM > 0)
        ID_NUM--;
    }
    if(key_num == KEYUP_PRES)
    {
      key_num = 0;
      if(ID_NUM < 99)
        ID_NUM++;
    }
    if(key_num == KEYBACK_PRES)
      goto MENU ; //返回主页面
		show_ID(40,48,ID_NUM);
  }
  ensure = PS_DeletChar(ID_NUM, 1); //删除单个指纹
  if(ensure == 0)
  {
    OLED_FullyClear();
    OLED_ShowMixedCH(0, 16, "  删除指纹成功  ");
		OLED_RefreshRAM();
  }
  else
    ShowErrMessage(ensure);
  HAL_Delay(1500);
MENU:
  OLED_FullyClear();
  OLED_ShowMixedCH(0, 0, "指纹模块测试程序");
  OLED_ShowMixedCH(16, 16, "K1键添加指纹");
  OLED_ShowMixedCH(16, 32, "K3键删除指纹");
  OLED_ShowMixedCH(16, 48, "K5键验证指纹");
	OLED_RefreshRAM();
  key_num = 0;
}

void delete_ALL(void)
{
		uint8_t ensure;
   ensure = PS_Empty(); //清空指纹库
   if(ensure == 0)
   {
     OLED_FullyClear();
     OLED_ShowMixedCH(0, 16, " 清空指纹库成功 ");
		 OLED_RefreshRAM();
   }
   else
	 {
      ShowErrMessage(ensure);
      HAL_Delay(1500);
   }
}

上面是官方的代码,我做了一点修改,代码中有很多移位操作,那些就是上面几个图中的指令,感兴趣的可以去移位算一下。
修改的函数:

//判断中断接收的数组有没有应答包
//waittime为等待中断接收数据的时间(单位1ms)
//返回值:数据包首地址
static uint8_t *JudgeStr(uint16_t waittime)
{
  char *data;
  uint8_t 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] = '\0';

		//HAL库的写法
	HAL_UART_Receive(&huart2,(uint8_t *)USART2_RX_BUF,USART2_MAX_RECV_LEN,waittime/4);//串口三接收数据
	if(!memcmp(str,USART2_RX_BUF,7))//比对数据
	{
		  data = strstr((const char*)USART2_RX_BUF, (const char*)str);
      if(data)
        return (uint8_t*)data;
	}
//  USART2_RX_STA = 0;
//  while(--waittime)
//  {
//    HAL_Delay(1);
//    if(USART2_RX_STA & 0X8000) //接收到一次数据
//    {
//      USART2_RX_STA = 0;
//      data = strstr((const char*)USART2_RX_BUF, (const char*)str);
//      if(data)
//        return (uint8_t*)data;
//    }
//  }
  return 0;
}

整个代码中,需要用到HAL库只需要改这一部分就可以使用了。因为官方代码串口接收采用的中断,所以用到HAL库我就改为了阻塞接收,把原来官方给的中断部分删掉即可
as608.h:

#ifndef __AS608_H
#define __AS608_H
#include 
#include "stm32f1xx_hal.h"

//#define PS_Sta   PAin(6)//读指纹模块状态引脚
#define CharBuffer1 0x01
#define CharBuffer2 0x02

#define USART2_MAX_RECV_LEN		400					//最大接收缓存字节数
#define USART2_MAX_SEND_LEN		400					//最大发送缓存字节数
#define USART2_RX_EN 			1					//0,不接收;1,接收.

extern uint32_t AS608Addr;//模块地址

typedef struct  
{
	uint16_t pageID;//指纹ID
	uint16_t mathscore;//匹配得分
}SearchResult;

typedef struct
{
	uint16_t PS_max;//指纹最大容量
	uint8_t  PS_level;//安全等级
	uint32_t PS_addr;
	uint8_t  PS_size;//通讯数据包大小
	uint8_t  PS_N;//波特率基数N
}SysPara;

void PS_StaGPIO_Init(void);//初始化PA6读状态引脚
	
uint8_t PS_GetImage(void); //录入图像 
 
uint8_t PS_GenChar(uint8_t BufferID);//生成特征 

uint8_t PS_Match(void);//精确比对两枚指纹特征 

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);//储存模板 

uint8_t PS_DeletChar(uint16_t PageID,uint16_t N);//删除模板 

uint8_t PS_Empty(void);//清空指纹库 

uint8_t PS_WriteReg(uint8_t RegNum,uint8_t DATA);//写系统寄存器 
 
uint8_t PS_ReadSysPara(SysPara *p); //读系统基本参数 

uint8_t PS_SetAddr(uint32_t addr);  //设置模块地址 

uint8_t PS_WriteNotepad(uint8_t NotePageNum,uint8_t *content);//写记事本 

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);//读有效模板个数 

uint8_t PS_HandShake(uint32_t *PS_Addr); //与AS608模块握手

const char *EnsureMessage(uint8_t ensure);//确认码错误信息解析

void Add_FR(void);

void press_FR(void);

void Del_FR(void);

void delete_ALL(void);

#endif


以上就是通过标准库移植为HAL库之后的代码

标准库代码链接: https://pan.baidu.com/s/1t5-VQ0BpBHGaw2MuOF7azA 提取码: rbwe
HAL库代码链接: https://pan.baidu.com/s/1Bf-6KohsnX4jqrtybxpMNA 提取码: 2iny

你可能感兴趣的:(嵌入式,STM32,stm32,单片机,arm)