\#include "stm32f10x.h"
\#include "stm32f10x_usart.h"
\#include "misc.h"
unsigned char dat[18];
//FINGERPRINT通信协议定义
unsigned char FP_Pack_Head[6] = {0xEF,0x01,0xFF,0xFF,0xFF,0xFF}; //协议包头
unsigned char FP_Get_Img[6] = {0x01,0x00,0x03,0x01,0x0,0x05}; //获得指纹图像
unsigned char FP_Templete_Num[6] ={0x01,0x00,0x03,0x1D,0x00,0x21 }; //获得模版总数
unsigned char FP_Search[11]={0x01,0x0,0x08,0x04,0x01,0x0,0x0,0x03,0xA1,0x0,0xB2}; //搜索指纹搜索范围0 - 929
unsigned char FP_Search_0_9[11]={0x01,0x0,0x08,0x04,0x01,0x0,0x0,0x0,0x13,0x0,0x21};//搜索0-9号指纹
unsigned char FP_Img_To_Buffer1[7]={0x01,0x0,0x04,0x02,0x01,0x0,0x08}; //将图像放入到BUFFER1
unsigned char FP_Img_To_Buffer2[7]={0x01,0x0,0x04,0x02,0x02,0x0,0x09}; //将图像放入到BUFFER2
unsigned char FP_Reg_Model[6]={0x01,0x0,0x03,0x05,0x0,0x09}; //将BUFFER1跟BUFFER2合成特征模版
unsigned char FP_Delet_All_Model[6]={0x01,0x0,0x03,0x0d,0x00,0x11}; //删除指纹模块里所有的模版
unsigned char FP_Save_Finger[9]={0x01,0x00,0x06,0x06,0x01,0x00,0x0B,0x00,0x19}; //将BUFFER1中的特征码存放到指定的位置
unsigned char FP_Delete_Model[10]={0x01,0x00,0x07,0x0C,0x0,0x0,0x0,0x1,0x0,0x0}; //删除指定的模版
//从 USART1 发送一个字节
void USART1_SendByte(unsigned char temp)
{
USART_SendData(USART1, temp);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}
//从 USART1 读取一个字节
unsigned char USART1_ReceivByte()
{
unsigned char recev;
while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);
recev = USART_ReceiveData(USART1);
return recev;
}
//FINGERPRINT命令字
//FINGERPRINT_获得指纹图像命令
void FINGERPRINT_Cmd_Get_Img(void)
{
unsigned char i;
for(i=0;i<6;i++) //发送包头
USART1_SendByte(FP_Pack_Head[i]);
for(i=0;i<6;i++) //发送命令 0x1d
USART1_SendByte(FP_Get_Img[i]);
for(i=0;i<12;i++)//读回应答信息
dat[i]=USART1_ReceivByte();
}
//删除所有指纹模版
void FINGERPRINT_Cmd_Delete_All_Model(void)
{
unsigned char i;
for(i=0;i<6;i++) //发送包头
USART1_SendByte(FP_Pack_Head[i]);
for(i=0;i<6;i++) //发送命令 0x1d
USART1_SendByte(FP_Delet_All_Model[i]);
for(i=0;i<12;i++)//读回应答信息
dat[i]=USART1_ReceivByte();
}
//讲图像转换成特征码存放在Buffer1中
void FINGERPRINT_Cmd_Img_To_Buffer1(void)
{
unsigned char i;
for(i=0;i<6;i++) //发送包头
{
USART1_SendByte(FP_Pack_Head[i]);
}
for(i=0;i<7;i++) //发送命令 将图像转换成 特征码 存放在 CHAR_buffer1
{
USART1_SendByte(FP_Img_To_Buffer1[i]);
}
for(i=0;i<12;i++)//读应答信息
{
dat[i]=USART1_ReceivByte();//把应答数据存放到缓冲区
}
}
//将图像转换成特征码存放在Buffer2中
void FINGERPRINT_Cmd_Img_To_Buffer2(void)
{
unsigned char i;
for(i=0;i<6;i++) //发送包头
{
USART1_SendByte(FP_Pack_Head[i]);
}
for(i=0;i<7;i++) //发送命令 将图像转换成 特征码 存放在 CHAR_buffer1
{
USART1_SendByte(FP_Img_To_Buffer2[i]);
}
for(i=0;i<12;i++)
{
dat[i]=USART1_ReceivByte();//读回应答信息
}
}
//将BUFFER1 跟 BUFFER2 中的特征码合并成指纹模版
void FINGERPRINT_Cmd_Reg_Model(void)
{
unsigned char i;
for(i=0;i<6;i++) //包头
{
USART1_SendByte(FP_Pack_Head[i]);
}
for(i=0;i<6;i++) //命令合并指纹模版
{
USART1_SendByte(FP_Reg_Model[i]);
}
for(i=0;i<12;i++)
{
dat[i]=USART1_ReceivByte();
}
}
//存储模版到特定地址
void FINGERPRINT_Cmd_Save_Finger( unsigned char ucH_Char, unsigned char ucL_Char)
{
unsigned long temp = 0;
unsigned char i;
FP_Save_Finger[5] = ucH_Char;
FP_Save_Finger[6] = ucL_Char;
for(i=0;i<7;i++) //计算校验和
temp = temp + FP_Save_Finger[i];
FP_Save_Finger[7]=(temp & 0x00FF00) >> 8; //存放校验数据
FP_Save_Finger[8]= temp & 0x0000FF;
for(i=0;i<6;i++)
USART1_SendByte(FP_Pack_Head[i]); //发送包头
for(i=0;i<9;i++)
USART1_SendByte(FP_Save_Finger[i]); //发送命令 将图像转换成 特征码 存放在 CHAR_buffer1
for(i=0;i<12;i++)
dat[i]=USART1_ReceivByte();
}
//获得指纹模板数量
void FINGERPRINT_Cmd_Get_Templete_Num(void)
{
unsigned int i;
for(i=0;i<6;i++) //包头
USART1_SendByte(FP_Pack_Head[i]);
//发送命令 0x1d
for(i=0;i<6;i++)
USART1_SendByte(FP_Templete_Num[i]);
for(i=0;i<12;i++)
dat[i]=USART1_ReceivByte();
}
//搜索全部用户999枚
void FINGERPRINT_Cmd_Search_Finger(void)
{
unsigned char i;
//发送命令搜索指纹库
for(i=0;i<6;i++)
{
USART1_SendByte(FP_Pack_Head[i]);
}
for(i=0;i<11;i++)
{
USART1_SendByte(FP_Search[i]);
}
for(i=0;i<16;i++)
{
dat[i]=USART1_ReceivByte();
}
}
//搜索用户0~9枚
void FINGERPRINT_Cmd_Search_Finger_Admin(void)
{
unsigned char i;
for(i=0;i<6;i++) //发送命令搜索指纹库
{
USART1_SendByte(FP_Pack_Head[i]);
}
for(i=0;i<11;i++)
{
USART1_SendByte(FP_Search_0_9[i]);
}
for(i=0;i<12;i++)
dat[i]=USART1_ReceivByte();
}
//添加一个新的指纹
unsigned char FP_add_new_user(unsigned char ucH_user,unsigned char ucL_user)
{
do
{
FINGERPRINT_Cmd_Get_Img(); //获得指纹图像
} while ( dat[9]!=0x0 ); //检测是否成功的按了指纹
FINGERPRINT_Cmd_Img_To_Buffer1(); //将图像转换成特征码存放在Buffer1中
do
{
FINGERPRINT_Cmd_Get_Img(); //获得指纹图像
} while( dat[9]!=0x0 );
FINGERPRINT_Cmd_Img_To_Buffer2(); //将图像转换成特征码存放在Buffer2中
FINGERPRINT_Cmd_Reg_Model(); //转换成特征码
FINGERPRINT_Cmd_Save_Finger(ucH_user,ucL_user);
return 0;
}