#include "RFID.h"
//读寄存器
unsigned char ReadRawRC(unsigned char SpiAddress)
{
u8 ucAddr;
u8 rdata;
ucAddr = ((SpiAddress << 1) & 0x7E) | 0x80;
NSS_L;
while(SPI_SR_TXE == 0);
SPI_DR = ucAddr;
while(SPI_SR_RXNE == 0);
rdata = SPI_DR;
while(SPI_SR_TXE == 0);
SPI_DR = 0x00;
while(SPI_SR_RXNE == 0);
rdata = SPI_DR;
NSS_H;
return(rdata);
}
//写寄存器
void WriteRawRC(unsigned char SpiAddress,unsigned char dat)
{
u8 ucAddr;
u8 rdata;
ucAddr = ((SpiAddress << 1) & 0x7E);
NSS_L;
while(SPI_SR_TXE == 0);
SPI_DR = ucAddr;
while(SPI_SR_RXNE == 0);
rdata = SPI_DR;
while(SPI_SR_TXE == 0);
SPI_DR = dat;
while(SPI_SR_RXNE == 0);
rdata = SPI_DR;
NSS_H;
}
//功 能:置FM17550寄存器位
//参数说明:reg[IN]:寄存器地址
// mask[IN]:置位值
void SetBitMask(unsigned char reg, unsigned char mask)
{
char tmp = 0x00;
tmp = ReadRawRC(reg);
WriteRawRC(reg, tmp | mask); // set bit mask
}
//功 能:清FM17550寄存器位
//参数说明:reg[IN]:寄存器地址
// mask[IN]:清位值
void ClearBitMask(unsigned char reg, unsigned char mask)
{
char tmp = 0x00;
tmp = ReadRawRC(reg);
WriteRawRC(reg, tmp & ~mask); // clear bit mask
}
//开启天线
//每次启动或关闭天险发射之间应至少有1ms的间隔
void PcdAntennaOn(void)
{
u8 i;
i = ReadRawRC(TxControlReg);
if (!(i & 0x03))
{
SetBitMask(TxControlReg, 0x03);
}
}
//关闭天线
void PcdAntennaOff(void)
{
ClearBitMask(TxControlReg, 0x03);
}
//功 能:通过RC522和ISO14443卡通讯
//参数说明:Command[IN]:RC522命令字
// pIn [IN]:通过RC522发送到卡片的数据
// InLenByte[IN]:发送数据的字节长度
// pOut [OUT]:接收到的卡片返回数据
// *pOutLenBit[OUT]:返回数据的位长度
char PcdComMF522(unsigned char Command,
unsigned char *pInData,
unsigned char InLenByte,
unsigned char *pOutData,
unsigned int *pOutLenBit)
{
char status = MI_ERR;
unsigned char irqEn = 0x00;
unsigned char waitFor = 0x00;
unsigned char lastBits;
unsigned char n;
unsigned int i;
switch (Command)
{
case PCD_AUTHENT:
irqEn = 0x12;
waitFor = 0x10;
break;
case PCD_TRANSCEIVE:
irqEn = 0x77;
waitFor = 0x30;
break;
default:
break;
}
WriteRawRC(ComIEnReg, irqEn | 0x80);
ClearBitMask(ComIrqReg, 0x80);
WriteRawRC(CommandReg, PCD_IDLE);
SetBitMask(FIFOLevelReg, 0x80);
for (i = 0; i < InLenByte; i++)
{
WriteRawRC(FIFODataReg, pInData[i]);
}
WriteRawRC(CommandReg, Command);
if (Command == PCD_TRANSCEIVE)
{
SetBitMask(BitFramingReg, 0x80);
}
i = 3200;
do
{
n = ReadRawRC(ComIrqReg);
i--;
} while ((i != 0) && !(n & 0x01) && !(n & waitFor));
ClearBitMask(BitFramingReg, 0x80);
if (i != 0)
{
if (!(ReadRawRC(ErrorReg) & 0x1B))
{
status = MI_OK;
if (n & irqEn & 0x01)
{
status = MI_NOTAGERR;
}
if (Command == PCD_TRANSCEIVE)
{
n = ReadRawRC(FIFOLevelReg);
lastBits = ReadRawRC(ControlReg) & 0x07;
if (lastBits)
{
*pOutLenBit = (n - 1) * 8 + lastBits;
}
else
{
*pOutLenBit = n * 8;
}
if (n == 0)
{
n = 1;
}
if (n > MAXRLEN)
{
n = MAXRLEN;
}
for (i = 0; i < n; i++)
{
pOutData[i] = ReadRawRC(FIFODataReg);
}
}
}
else
{
status = MI_ERR;
}
}
SetBitMask(ControlReg, 0x80); // stop timer now
WriteRawRC(CommandReg, PCD_IDLE);
return status;
}
//功 能:寻卡
//参数说明: req_code[IN]:寻卡方式
// 0x52 = 寻感应区内所有符合14443A标准的卡
// 0x26 = 寻未进入休眠状态的卡
// pTagType[OUT]:卡片类型代码
// 0x4400 = Mifare_UltraLight
// 0x0400 = Mifare_One(S50)
// 0x0200 = Mifare_One(S70)
// 0x0800 = Mifare_Pro(X)
// 0x4403 = Mifare_DESFire
//返 回: 成功返回MI_OK
char PcdRequest(unsigned char req_code, unsigned char *pTagType)
{
char status;
unsigned int unLen;
unsigned char ucComMF522Buf[MAXRLEN];
ClearBitMask(Status2Reg, 0x08); //清RC522寄存位
WriteRawRC(BitFramingReg, 0x07); //写RC623寄存器
SetBitMask(TxControlReg, 0x03); //置RC522寄存位
ucComMF522Buf[0] = req_code;
status = PcdComMF522(PCD_TRANSCEIVE, ucComMF522Buf, 1, ucComMF522Buf, &unLen);
if ((status == MI_OK) && (unLen == 0x10))
{
*pTagType = ucComMF522Buf[0];
*(pTagType + 1) = ucComMF522Buf[1];
}
else
{
status = MI_ERR;
}
return status;
}
//功 能:防冲撞
//参数说明: pSnr[OUT]:卡片序列号,4字节
//返 回: 成功返回MI_OK
char PcdAnticoll(unsigned char *pSnr)
{
char status;
unsigned char i, snr_check = 0;
unsigned int unLen;
unsigned char ucComMF522Buf[MAXRLEN];
ClearBitMask(Status2Reg, 0x08);
WriteRawRC(BitFramingReg, 0x00);
ClearBitMask(CollReg, 0x80);
ucComMF522Buf[0] = PICC_ANTICOLL1;
ucComMF522Buf[1] = 0x20;
status = PcdComMF522(PCD_TRANSCEIVE, ucComMF522Buf, 2, ucComMF522Buf, &unLen);
if (status == MI_OK)
{
for (i = 0; i < 4; i++)
{
*(pSnr + 3 - i) = ucComMF522Buf[i];
snr_check ^= ucComMF522Buf[i];
}
if (snr_check != ucComMF522Buf[i])
{
status = MI_ERR;
}
}
SetBitMask(CollReg, 0x80);
return status;
}
//功 能:复位RC522
//返 回: 成功返回MI_OK
char PcdReset(void)
{
RST_H;
DelayMs(1);
RST_L;
DelayMs(3);
RST_H;
DelayMs(5);
WriteRawRC(CommandReg, PCD_RESETPHASE);
DelayMs(2);
WriteRawRC(ModeReg, 0x3D);
WriteRawRC(TReloadRegL, 30); //定时器计数值低位
WriteRawRC(TReloadRegH, 0); //定时器计数值高位
WriteRawRC(TModeReg, 0x8D); //定时器自动重启计数
WriteRawRC(TPrescalerReg, 0x3E);//定时器相关设置
WriteRawRC(TxAutoReg, 0x40); //
// ClearBitMask(TestPinEnReg, 0x80);
// WriteRawRC(TxAutoReg, 0x40);
return MI_OK;
}
void FM17550Init(void)
{
PcdReset();
DelayMs(10);
PcdAntennaOff();
DelayMs(10);
PcdAntennaOn();
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
STM8S103+FM17550.rar (49个子文件)
IC&ID-WG
ICMK.ewd 16KB
BuildLog.log 3B
ICMK.ewp 57KB
ICMK.eww 158B
bsp
RFID.h 5KB
interrupt.c 1KB
user.h 3KB
I2CEEPROM.c 3KB
mcuinit.c 4KB
RFID.c 7KB
user.c 3KB
RFIDC.c 4KB
ICMK.ewt 157KB
ICMK.dep 8KB
main.c 2KB
Debug
List
ICMK.map 27KB
Obj
RFIDC.pbi.xcl 11KB
interrupt.pbi.xcl 11KB
interrupt.pbi 227KB
RFIDC.pbi 230KB
RFID.pbi 243KB
ICMK.pbd.browse 271KB
RFID.pbi.xcl 11KB
I2CEEPROM.pbi.xcl 11KB
mcuinit.pbi 229KB
RFID.o 29KB
ICMK.pbd 271KB
RFIDC.o 20KB
mcuinit.pbi.xcl 11KB
mcuinit.o 52KB
ICMK.pbd.linf 449B
interrupt.o 13KB
user.pbi.xcl 11KB
I2CEEPROM.o 24KB
user.pbi 231KB
main.o 11KB
main.pbi 237KB
user.o 22KB
I2CEEPROM.pbi 232KB
main.pbi.xcl 11KB
Exe
ICMK.hex 11KB
ICMK.out 86KB
settings
ICMK.Debug.general.xcl 315B
ICMK.dbgdt 69B
ICMK.Debug.cspy.ps1 1KB
ICMK.Debug.driver.xcl 140B
ICMK.wsdt 33KB
ICMK.dnx 2KB
ICMK.Debug.cspy.bat 2KB
共 49 条
- 1
资源评论
锁具之王
- 粉丝: 1
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功