//#include <intrins.h>
#include "hardware_defs.h" // requires compiler_defs.h
#include "CPU_init.h" // requires compiler_defs.h
#include "mfrc522.h"
#include <string.h>
#include "ISO14443_4.h"
#define MAXRLEN 64
//void WR_FIFO(unsigned char Address,unsigned char *Buf,unsigned char nlen);
//void RD_FIFO(unsigned char *Buf,unsigned char Len);
//指示灯
//#define LED 6//sbit LED = P1^6;
/////////////////////////////////////////////////////////////////////
//函数原型
/////////////////////////////////////////////////////////////////////
unsigned char SPI_RW(unsigned char value)
{
while(!(SPI_SR & 0X02));//判断发射为不为空
SPI_DR = value; //写值
while(!(SPI_SR & 0X02));//判断发射为不为空
while(!(SPI_SR & 0x01));//判断是否收到数据
return SPI_DR;
}
//#define ucComMF522Buf SerBfr
unsigned char fHasRATS = 0;
void RC522_Init(void)
{
unsigned char Temp;
delay_10ms(1);
WriteRawRC(ControlReg,0x10);//18 10
WriteRawRC(ModeReg,0x3F);//22 3f
WriteRawRC(RFU23,0x00);//46 00
WriteRawRC(RFU25,0x80);//4a 80
WriteRawRC(AutoTestReg,0x40);//6c 40
WriteRawRC(TxAutoReg,0x40);//2a 40
ReadRawRC(TxAutoReg);//aa 40
SetBitMask(TxControlReg,0x03);//a8 80 28 83
WriteRawRC(TPrescalerReg,0x3D);//56 3d
WriteRawRC(TModeReg,0x0D);//54 0d
WriteRawRC(TReloadRegL,0x0A);//5a 0a
WriteRawRC(TReloadRegH,0x00);//58 00
WriteRawRC(ComIrqReg,0x01);//08 01
SetBitMask(ControlReg,0x40);//98 10 18 50
do
{
Temp = ReadRawRC(ComIrqReg);//88 15
}while(!(Temp&0x01));
WriteRawRC(ComIrqReg,0x01);//08 01
WriteRawRC(CommandReg,0x00);//02 00
}
/////////////////////////////////////////////////////////////////////
//功 能:寻卡
//参数说明: 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
/////////////////////////////////////////////////////////////////////
unsigned char PcdRequest(unsigned char req_code,unsigned char *pTagType)
{
/*当选卡时,并未进入寻卡和锁卡,因此要更改
*/
char status;
unsigned int unLen;
unsigned char ucComMF522Buf[MAXRLEN];
char i=0;
if(fHasRATS != 1)
{
do
{
status = ReadRawRC(Status2Reg); //90 00(01,05)
WriteRawRC(Status2Reg,status&0xf7); //10 00
WriteRawRC(CollReg,0x80); //1c 80
ClearBitMask(TxModeReg,0x80); //a4 00 24 00
ClearBitMask(RxModeReg,0x80); //a6 00 26 00
WriteRawRC(BitFramingReg,0x07); //1a 07
SetBitMask(TxControlReg,0x03); //a8 83 28 83
ucComMF522Buf[0] = req_code;
i++;
status = PcdComMF522_P(PCD_TRANSCEIVE,ucComMF522Buf,1,ucComMF522Buf,&unLen,0x0002);
if(i>=2)
break;
Delay1us(5);
}while(status ==MI_TIMEOUT );
}
else
{
SetBitMask(TxModeReg,0x80);//a4 80 24 80
SetBitMask(RxModeReg,0x80);//a6 80 26 80
ucComMF522Buf[0] = 0xCA;
ucComMF522Buf[1] = 0x00;
status = PcdComMF522_P(PCD_TRANSCEIVE,ucComMF522Buf,2,ucComMF522Buf,&unLen,0x00F0);
//add below 3lines
status = ReadRawRC(Status2Reg); //90 00(01,05)
WriteRawRC(Status2Reg,status&0xf7); //10 00
WriteRawRC(CollReg,0x80); //1c 80
ClearBitMask(TxModeReg,0x80); //a4 80 24 00
ClearBitMask(RxModeReg,0x80); //a6 00 26 00
WriteRawRC(BitFramingReg,0x07); //1a 07
//add 1 line
SetBitMask(TxControlReg,0x03);//a8 83 28 83
ucComMF522Buf[0] = req_code;
status = PcdComMF522_P(PCD_TRANSCEIVE,ucComMF522Buf,1,ucComMF522Buf,&unLen,0x0002);
if(status == 0)
fHasRATS = 0;
}
if ((status == MI_OK) && (unLen == 0x10))
{
*pTagType = ucComMF522Buf[0];
*(pTagType+1) = ucComMF522Buf[1];
}
else
{
status = MI_ERR;
}
return status;
}
/////////////////////////////////////////////////////////////////////
//功 能:防冲撞
//参数说明: pSnr[OUT]:卡片序列号,4字节
//返 回: 成功返回MI_OK
/////////////////////////////////////////////////////////////////////
unsigned char PcdAnticoll(unsigned char *pSnr)
{
unsigned char status;
unsigned int unLen;
unsigned char ucComMF522Buf[MAXRLEN];
unsigned char i;
unsigned char snr_check=0;
ClearBitMask(TxModeReg,0x80);//a4 00 24 00
ClearBitMask(RxModeReg,0x80);//a6 00 26 00
WriteRawRC(CollReg,0x00); //1c 00
Delay1us(200);
WriteRawRC(BitFramingReg,0x00);//1a 00
ucComMF522Buf[0] = PICC_ANTICOLL1;
ucComMF522Buf[1] = 0x20;
status = PcdComMF522_P(PCD_TRANSCEIVE,ucComMF522Buf,2,ucComMF522Buf,&unLen,0x0020);
Delay1us(100);
WriteRawRC(BitFramingReg,0x00);//1a 00
WriteRawRC(CollReg,0x80);//1c 80
if (status == MI_OK)
{
for (i=0; i<4; i++)
{
*(pSnr+i) = ucComMF522Buf[i];
snr_check ^= ucComMF522Buf[i];
}
if (snr_check != ucComMF522Buf[i])
{
status = MI_ERR;
}
}
return status;
}
/////////////////////////////////////////////////////////////////////
//功 能:选定卡片
//参数说明: pSnr[IN]:卡片序列号,4字节
//返 回: 成功返回MI_OK
/////////////////////////////////////////////////////////////////////
unsigned char PcdSelect(unsigned char *pSnr)
{
char status;
unsigned char ucComMF522Buf[MAXRLEN];
unsigned char i;
unsigned int unLen;
/*
status= PcdRequest(PICC_REQALL,ucComMF522Buf);
if(status!=0)
{
status= PcdRequest(PICC_REQALL,ucComMF522Buf);
if(status!=0)
{
status = MI_ERR;
}
}
status = PcdAnticoll(pSnr);
if(status!=0)
{
return MI_ERR;
}
*/
SetBitMask(TxModeReg,0x80);//a4 80 24 80
SetBitMask(RxModeReg,0x80);//a6 00 26 80
ucComMF522Buf[0] = PICC_ANTICOLL1;
ucComMF522Buf[1] = 0x70;
ucComMF522Buf[6] = 0;
for (i=0; i<4; i++)
{
ucComMF522Buf[i+2] = *(pSnr+i);
ucComMF522Buf[6] ^= *(pSnr+i);
}
status = PcdComMF522_P(PCD_TRANSCEIVE,ucComMF522Buf,7,ucComMF522Buf,&unLen,0x0010);
if ((status == MI_OK))//&& (unLen == 0x18))
{
pSnr[0] = ucComMF522Buf[0];
}
else
{
status = MI_ERR;
}
return status;
}
/////////////////////////////////////////////////////////////////////
//功 能:读RC632寄存器
//参数说明:Address[IN]:寄存器地址
//返 回:读出的值
/////////////////////////////////////////////////////////////////////
unsigned char ReadRawRC(unsigned char Address)//Kevin modify
{
#if 0
unsigned char xdata i, ucAddr;
unsigned char xdata ucResult=0;
SetIO_DATA(MF522_SCK,0);//MF522_SCK = 0;
SetIO_DATA(MF522_NSS,0);//MF522_NSS = 0;
// Delay1us(1);
ucAddr = ((Address<<1)&0x7E)|0x80;
for(i=8;i>0;i--)
{
SetIO_DATA(MF522_SI,((ucAddr&0x80)));//MF522_SI = ((ucAddr&0x80)==0x80);
//Delay1us(1);
SetIO_DATA(MF522_SCK,1);//MF522_SCK = 1;
//Delay1us(1);
ucAddr <<= 1;
SetIO_DATA(MF522_SCK,0);//MF522_SCK = 0;
//Delay1us(1);
}
// Delay1us(15);
for(i=8;i>0;i--)
{
SetIO_DATA(MF522_SCK,1);//MF522_SCK = 1;
//Delay1us(1);
ucResult <<= 1;
ucResult|=(bit)GetIO_DATA(MF522_SO);
//Delay1us(1);
SetIO_DATA(MF522_SCK,0);//MF522_SCK = 0;
//Delay1us(1);
}
//Delay1us(1);
SetIO_DATA(MF522_NSS,1);//MF522_NSS = 1;
//SetIO_DATA(MF522_SCK,1);//MF522_SCK = 1;
SetIO_DATA(MF522_SCK,0);//MF522_SCK = 0;//by jack add 091127
return ucResult;
#else
unsigned char ucResult;
unsigned char ucAddr;
ucAddr = ((Address<<1)&0x7E)|0x80;
NSS1 = 0;
asm("nop");
asm
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
STM8S103F方案.rar (54个子文件)
STM8S103F方案
QUEUE_FIFO.C 6KB
Release
List
Obj
UART_FIFO .o 9KB
ppPhy_const.o 1KB
CPU_init.o 6KB
ppPhy.o 15KB
main.o 16KB
Power_Tester.o 3KB
stm8_test.pbd 572B
Exe
stm8_test.out 38KB
settings
stm_test.wsdt 4KB
STM8S方案.wsdt 4KB
stm8_test.cspy.bat 2KB
stm8_test.dni 1KB
stm8_test.dbgdt 8KB
CPU_init.h 327B
ISO14443_4.c 12KB
STM8S方案.eww 163B
cspycomm.log 39B
hardware_defs.h 4KB
ISO14443_4.h 5KB
stm8_test.dep 16KB
Debug
List
CPU_init.lst 14KB
QUEUE_FIFO.lst 13KB
Power_Tester.lst 5KB
stm8_test.map 22KB
UART_FIFO .lst 11KB
ppPhy.lst 34KB
main.lst 13KB
RC522.lst 58KB
ISO14443_4.lst 22KB
ppPhy_const.lst 12KB
Obj
ISO14443_4.o 17KB
UART_FIFO .o 23KB
QUEUE_FIFO.o 30KB
RC522.o 69KB
ppPhy_const.o 3KB
CPU_init.o 31KB
ppPhy.o 45KB
main.o 20KB
Power_Tester.o 7KB
stm8_test.pbd 570B
Exe
stm8_test.out 92KB
yyy.out 21KB
stm8.out 21KB
stm8_test.hex 16KB
stm8_test.elf 21KB
stm8_test.o 21KB
mfrc522.h 8KB
stm8_test.ewp 41KB
stm8_test.ewd 10KB
RC522.C 35KB
main.c 6KB
CPU_init.c 7KB
QUEUE_FIFO.h 2KB
共 54 条
- 1
资源评论
- zhangchongjin2019-01-16不能用啊,我用STM8S不能用
- 宁海金城武2019-05-16学习一下学习一下
林时有空
- 粉丝: 2
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- html+css+'青春献礼二十大 红色旅游助乡村'为主题的网页设计 2022年参与学校网页设计比赛时完成的
- Unity 模型布尔运算插件
- eCANtools工具,CAN资料包
- 129335283047061xiazaigongjuxiang(去重软件).apk
- Android环境检测工具,检测ksu,lsp,magisk等
- WordPress后台美化插件QuarterAdmin分享
- PCB_Project单片机绘制 (2024-5-11 22-22-13).zip
- nccl-local-repo-ubuntu2204-2.21.5-cuda12.4-1.0-1-amd64
- STM32移植LVGL源码工程 stm32移植GUI-Guider源码 LVGL移植源码
- Screenshot_20240522_084328_com.tencent.mm.jpg
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功