基于cubemx的stm32f103指纹模块(AS608)

寒假这段时间自己做了个指纹锁玩,在这里写一下指纹模块的用法
一.测试
新到手的AS608模块,可以在软件中测试一下功能是否正常。
在使用这个配套软件的时候,注意要搭配TTL转串口使用,连接电源线和串口线四根就可以了,注意在测试的时候,要找对COM口和波特率
图片显示AS608完好,可以正常录入删除指纹
基于cubemx的stm32f103指纹模块(AS608)_第1张图片
基于cubemx的stm32f103指纹模块(AS608)_第2张图片
基于cubemx的stm32f103指纹模块(AS608)_第3张图片
二.cubemx配置

查看引脚,需要在cubemx中配置串口和一个感应引脚。
串口正常打开就可以,很多例子是标准库的中断模式,这里可以用阻塞模式,所以不用配置中断.
在这里插入图片描述
基于cubemx的stm32f103指纹模块(AS608)_第4张图片
三.代码

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

//#include "stm32f1xx_hal.h"
//#include "tim.h"
//#include "gpio.h"
uint32_t AS608Addr = 0XFFFFFFFF; //默认
extern UART_HandleTypeDef huart2;
extern DMA_HandleTypeDef hdma_usart2_rx;
char str2[6] = {0};
uint16_t RX_Size; 
	unsigned char key_num=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]:接收到的数据长度

uint16_t USART2_RX_STA=0;//==ether_st.RX_flag
//uart_rx_flag 中断接收数据标志,接收到数据为1,否则为0
uint8_t uart_rx_flag = 0;

//static void USART2_IRQHandler(void)
//{
//   
//      ETH_UsartReceive_IDLE();
//      HAL_UART_IRQHandler(&huart2);

//}

//串口发送一个字节
static void MYUSART_SendData(uint8_t data)
{
 //   HAL_UART_Transmit(&huart2, (uint8_t *)&data, 1, 0xffff);
  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)
{
//    int a;
  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';

//	while(--waittime)
//	{
//		HAL_Delay(1);
//		 if(USART2_RX_STA&0X8000)//接收到一次数据
//		{
//			USART2_RX_STA=0;
			 for(uint16_t i=0;ibuf2时,返回值>0
	{
		  data = strstr((const char*)USART2_RX_BUF, (const char*)str);//strstr(str1,str2) 函数用于判断字符串str2是否是str1的子串
      if(data)
        return (uint8_t*)data;
	}
//HAL库的写法  中断接收 
//	while(--waittime)
//  {
//    HAL_Delay(1);
//    if(uart_rx_flag && !memcmp(str,USART2_RX_BUF,7)) //接收到一次数据
//    {
//      data = strstr((const char*)USART2_RX_BUF, (const char*)str);
//      if(data)
//        return (uint8_t*)data;
//    }
//  }
//	uart_rx_flag = 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//USART2_RX_BUF==ether_st.RX_pData
    )
    {
    
      *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_ShowNum(16,4,ensure,4,16);
}


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

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

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

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

    case 4:
			OLED_Clear();
		  OLED_ShowNum(40,0,2,1,16);
		  OLED_ShowNum(40,2,8,1,16);
		  OLED_ShowNum(40,6,5,1,16);
	  	OLED_ShowString(0,0,"push",16);
		  OLED_ShowString(0,2,"push",16);
		  OLED_ShowString(0,6,"push",16);
      OLED_Show_Many_Chinese(50, 0, "加",16);//按2加,按8减
	   	OLED_Show_Many_Chinese(50, 2, "减",16);
      OLED_Show_Many_Chinese(50, 6 ,"保存",16);//按K3保存
      //OLED_ShowString(0, 4, " 0= 0)
            ID_NUM--;
        }
        if(key_num == 2)
        {
    
          key_num = 0;
          if(ID_NUM < 99)
            ID_NUM++;
        }
				OLED_ShowNum(40,4,ID_NUM,10,16);
				OLED_ShowString(0,4,"ID",16);
      }
      key_num = 0;
      ensure = PS_StoreChar(CharBuffer2, ID_NUM); //储存模板
      if(ensure == 0x00)
      {
    
        OLED_Clear();
        OLED_Show_Many_Chinese(0, 0, "录入指纹成功",16);//录入指纹成功
        HAL_Delay(1500);
        OLED_Clear();
        return ;
      }
      else
      {
        OLED_Clear();
        processnum = 0;
        ShowErrMessage(ensure);
      }
      break;
    }
    HAL_Delay(400);
    if(i == 10) //超过5次没有按手指则退出
    {
      break;
    }
  }
}

SysPara AS608Para;//指纹模块AS608参数
//刷指纹
int press_FR(void)
{
  SearchResult seach;
  uint8_t ensure;
  char str[20];

  while(key_num != 1)//KEYBACK_PRES
  {
    
    key_num = read_key();
    ensure = PS_GetImage();
    if(ensure == 0x00) //获取图像成功
    {
    
      ensure = PS_GenChar(CharBuffer1);
      if(ensure == 0x00) //生成特征成功
      {
    
        ensure = PS_HighSpeedSearch(CharBuffer1, 0, 99, &seach);
        if(ensure == 0x00) //搜索成功
        {
					OLED_Clear();
         OLED_Show_Many_Chinese(0, 0, "指纹验证成功",16);//指纹验证成功
//          sprintf(str, " ID:%d score:%d ", seach.pageID, seach.mathscore);
					walk_on();
       ShowBmp(BMP2);	
          HAL_Delay(1000);
					OLED_Clear();
					OLED_ShowString(20,4,"WELCOME",16);	
					HAL_Delay(1000);
					Walk_OFF();
					return 0;
        }
        else
        {
					OLED_Clear();
          OLED_Show_Many_Chinese(0, 4, "验证失败",16);//验证失败
					//OLED_RefreshRAM();
          HAL_Delay(1500);
					return -1;
        }
      }
      else
			{  return -1;
			};
     OLED_Clear();
     OLED_Show_Many_Chinese(0, 4, "请按手指",16);//请按手指
			//OLED_RefreshRAM();
    }
 }
  OLED_Clear();
	   OLED_ShowString(0,0,"push",16);
		 OLED_ShowString(0,2,"push",16);
		 OLED_ShowNum(40,0,1,1,16);
		 OLED_ShowNum(40,2,2,1,16);
			OLED_Show_Many_Chinese(50,0,"指纹",16);
			OLED_Show_Many_Chinese(50,2,"按键",16);
 

	//OLED_RefreshRAM();
}

//删除指纹
void Del_FR(void)
{
    
  uint8_t  ensure;
  uint16_t ID_NUM = 0;
	uint8_t key_num = 0;
	OLED_Clear();
			 OLED_ShowNum(40,0,2,1,16);
		  OLED_ShowNum(40,2,8,1,16);
		  OLED_ShowNum(40,6,5,1,16);
	  	OLED_ShowString(0,0,"push",16);
		  OLED_ShowString(0,2,"push",16);
		  OLED_ShowString(0,6,"push",16);
      OLED_Show_Many_Chinese(50, 0, "加",16);//按2加,按8减
	   	OLED_Show_Many_Chinese(50, 2, "减",16);
      OLED_Show_Many_Chinese(50, 6 ,"确认",16);//按K3保存
//  OLED_Show_Many_Chinese(0,0,"加减确认",16);//K2加 K4减 K5确认
  while(key_num != 5)
  {
    
    key_num = read_key();
    if(key_num == 8)
    {
    
      key_num = 0;
      if(ID_NUM > 0)
        ID_NUM--;
    }
    if(key_num == 2)
    {
    
      key_num = 0;
      if(ID_NUM < 99)
        ID_NUM++;
    }
    if(key_num == 1)
      goto MENU ; //返回主页面
		OLED_ShowNum(40,4,ID_NUM,6,16);
  }
  ensure = PS_DeletChar(ID_NUM, 1); //删除单个指纹
  if(ensure == 0)
  {
    
    OLED_Clear();
    OLED_Show_Many_Chinese(0,0,"删除指纹成功",16);//删除指纹成功
	//	OLED_RefreshRAM();
  }
  else
    ShowErrMessage(ensure);
  HAL_Delay(1500);
MENU:
  OLED_Clear();
	   OLED_ShowString(0,0,"push",16);
		 OLED_ShowString(0,2,"push",16);
		 OLED_ShowNum(40,0,1,1,16);
		 OLED_ShowNum(40,2,2,1,16);
			OLED_Show_Many_Chinese(50,0,"指纹",16);
			OLED_Show_Many_Chinese(50,2,"按键",16);
//	OLED_RefreshRAM();
  key_num = 0;
}

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





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

//#define PS_Sta   HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_6);//读指纹模块状态引脚
#define CharBuffer1 0x01
#define CharBuffer2 0x02

#define  data_len  400
  
#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);

int press_FR(void);

void Del_FR(void);

void delete_ALL(void);

#endif

在OLED显示和按键部分可以使用自己的代码,然后在主函数里面调用录指纹刷指纹删指纹就可以了。

你可能感兴趣的:(HAL库,32单片机,stm32,单片机,arm)