rc522.h文件
[mw_shl_code=c,true]#ifndef __RC522_H
#define __RC522_H
#include "sys.h"
#include "spi.h"
/////////////////////////////////////////////////////////////////////
//MF522ÃüÁî×Ö
/////////////////////////////////////////////////////////////////////
#define PCD_IDLE 0x00 //È¡Ïûµ±Ç°ÃüÁî
#define PCD_AUTHENT 0x0E //ÑéÖ¤ÃÜÔ¿
#define PCD_RECEIVE 0x08 //½ÓÊÕÊý¾Ý
#define PCD_TRANSMIT 0x04 //·¢ËÍÊý¾Ý
#define PCD_TRANSCEIVE 0x0C //·¢ËͲ¢½ÓÊÕÊý¾Ý
#define PCD_RESETPHASE 0x0F //¸´Î»
#define PCD_CALCCRC 0x03 //CRC¼ÆËã
/////////////////////////////////////////////////////////////////////
//Mifare_One¿¨Æ¬ÃüÁî×Ö
/////////////////////////////////////////////////////////////////////
#define PICC_REQIDL 0x26 //Ñ°ÌìÏßÇøÄÚδ½øÈëÐÝÃß״̬
#define PICC_REQALL 0x52 //Ñ°ÌìÏßÇøÄÚÈ«²¿¿¨
#define PICC_ANTICOLL1 0x93 //·À³åײ
#define PICC_ANTICOLL2 0x95 //·À³åײ
#define PICC_AUTHENT1A 0x60 //ÑéÖ¤AÃÜÔ¿
#define PICC_AUTHENT1B 0x61 //ÑéÖ¤BÃÜÔ¿
#define PICC_READ 0x30 //¶Á¿é
#define PICC_WRITE 0xA0 //д¿é
#define PICC_DECREMENT 0xC0 //¿Û¿î
#define PICC_INCREMENT 0xC1 //³äÖµ
#define PICC_RESTORE 0xC2 //µ÷¿éÊý¾Ýµ½»º³åÇø
#define PICC_TRANSFER 0xB0 //±£´æ»º³åÇøÖÐÊý¾Ý
#define PICC_HALT 0x50 //ÐÝÃß
/////////////////////////////////////////////////////////////////////
//MF522 FIFO³¤¶È¶¨Òå
/////////////////////////////////////////////////////////////////////
#define DEF_FIFO_LENGTH 64 //FIFO size=64byte
#define MAXRLEN 18
/////////////////////////////////////////////////////////////////////
//MF522¼Ä´æÆ÷¶¨Òå
/////////////////////////////////////////////////////////////////////
// PAGE 0
#define RFU00 0x00
#define CommandReg 0x01
#define ComIEnReg 0x02
#define DivlEnReg 0x03
#define ComIrqReg 0x04
#define DivIrqReg 0x05
#define ErrorReg 0x06
#define Status1Reg 0x07
#define Status2Reg 0x08
#define FIFODataReg 0x09
#define FIFOLevelReg 0x0A
#define WaterLevelReg 0x0B
#define ControlReg 0x0C
#define BitFramingReg 0x0D
#define CollReg 0x0E
#define RFU0F 0x0F
// PAGE 1
#define RFU10 0x10
#define ModeReg 0x11
#define TxModeReg 0x12
#define RxModeReg 0x13
#define TxControlReg 0x14
#define TxAutoReg 0x15
#define TxSelReg 0x16
#define RxSelReg 0x17
#define RxThresholdReg 0x18
#define DemodReg 0x19
#define RFU1A 0x1A
#define RFU1B 0x1B
#define MifareReg 0x1C
#define RFU1D 0x1D
#define RFU1E 0x1E
#define SerialSpeedReg 0x1F
// PAGE 2
#define RFU20 0x20
#define CRCResultRegM 0x21
#define CRCResultRegL 0x22
#define RFU23 0x23
#define ModWidthReg 0x24
#define RFU25 0x25
#define RFCfgReg 0x26
#define GsNReg 0x27
#define CWGsCfgReg 0x28
#define ModGsCfgReg 0x29
#define TModeReg 0x2A
#define TPrescalerReg 0x2B
#define TReloadRegH 0x2C
#define TReloadRegL 0x2D
#define TCounterValueRegH 0x2E
#define TCounterValueRegL 0x2F
// PAGE 3
#define RFU30 0x30
#define TestSel1Reg 0x31
#define TestSel2Reg 0x32
#define TestPinEnReg 0x33
#define TestPinValueReg 0x34
#define TestBusReg 0x35
#define AutoTestReg 0x36
#define VersionReg 0x37
#define AnalogTestReg 0x38
#define TestDAC1Reg 0x39
#define TestDAC2Reg 0x3A
#define TestADCReg 0x3B
#define RFU3C 0x3C
#define RFU3D 0x3D
#define RFU3E 0x3E
#define RFU3F 0x3F
/////////////////////////////////////////////////////////////////////
//ºÍMF522ͨѶʱ·µ»ØµÄ´íÎó´úÂë
/////////////////////////////////////////////////////////////////////
#define MI_OK 0
#define MI_NOTAGERR (1)
#define MI_ERR (2)
#define SHAQU1 0X01
#define KUAI4 0X04
#define KUAI7 0X07
#define REGCARD 0xa1
#define CONSUME 0xa2
#define READCARD 0xa3
#define ADDMONEY 0xa4
//
//#define spi_cs 1;
//sbit spi_ck=P0^6;
//sbit spi_mosi=P0^7;
//sbit spi_miso=P4^1;
//sbit spi_rst=P2^7;
#define SPIReadByte() SPIWriteByte(0)
u8 SPIWriteByte(u8 byte);
void SPI1_Init(void);
#define SET_SPI_CS (GPIOF->BSRR=0X01)
#define CLR_SPI_CS (GPIOF->BRR=0X01)
#define SET_RC522RST GPIOF->BSRR=0X02
#define CLR_RC522RST GPIOF->BRR=0X02
/***********************RC522 º¯Êýºê¶¨Òå**********************/
#define RC522_CS_Enable() HAL_GPIO_WritePin(GPIOG,GPIO_PIN_10,GPIO_PIN_RESET)
#define RC522_CS_Disable() HAL_GPIO_WritePin(GPIOG,GPIO_PIN_10,GPIO_PIN_SET)
#define RC522_Reset_Enable() HAL_GPIO_WritePin(GPIOG,GPIO_PIN_12,GPIO_PIN_RESET)
#define RC522_Reset_Disable() HAL_GPIO_WritePin(GPIOG,GPIO_PIN_12,GPIO_PIN_SET)
#define RC522_SCK_0() HAL_GPIO_WritePin(GPIOB,GPIO_PIN_13,GPIO_PIN_RESET)
#define RC522_SCK_1() HAL_GPIO_WritePin(GPIOB,GPIO_PIN_13,GPIO_PIN_SET)
#define RC522_MOSI_0() HAL_GPIO_WritePin(GPIOB,GPIO_PIN_15,GPIO_PIN_RESET)
#define RC522_MOSI_1() HAL_GPIO_WritePin(GPIOB,GPIO_PIN_15,GPIO_PIN_RESET)
#define RC522_MISO_GET() HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_14)
void RC522_Handel (void);
void RC522_Init ( void ); //³õʼ»¯
void PcdReset ( void ); //¸´Î»
void M500PcdConfigISOType ( u8 type ); //¹¤×÷·½Ê½
char PcdRequest ( u8 req_code, u8 * pTagType ); //Ñ°¿¨
char PcdAnticoll ( u8 * pSnr); //¶Á¿¨ºÅ
void PcdAntennaOn( void );
void PcdAntennaOff(void);
char PcdSelect ( u8 * pSnr );
char PcdAuthState ( u8 ucAuth_mode, u8 ucAddr, u8 * pKey, u8 * pSnr );
char PcdWrite ( u8 ucAddr, u8 * pData );
char PcdRead ( u8 ucAddr, u8 * pData );
void ShowID(u16 x,u16 y, u8 *p, u16 charColor, u16 bkColor); //ÏÔʾ¿¨µÄ¿¨ºÅ£¬ÒÔÊ®Áù½øÖÆÏÔʾ
#endif
[/mw_shl_code]、
RC522.C
[mw_shl_code=c,true]#include "sys.h"
#include "rc522.h"
#include "delay.h"
#include "usart.h"
#include "spi.h"
#include "string.h"
//////////////////////////////////////////////////////////
// M1¿¨·ÖΪ16¸öÉÈÇø£¬Ã¿¸öÉÈÇøÓÉËĸö¿é£¨¿é0¡¢¿é1¡¢¿é2¡¢¿é3£©×é³É
// ½«16¸öÉÈÇøµÄ64¸ö¿é°´¾ø¶ÔµØÖ·±àºÅΪ£º0~63
// µÚ0¸öÉÈÇøµÄ¿é0£¨¼´¾ø¶ÔµØÖ·0¿é£©£¬ÓÃÓÚ´æ·Å³§ÉÌ´úÂ룬ÒѾ­¹Ì»¯²»¿É¸ü¸Ä
// ÿ¸öÉÈÇøµÄ¿é0¡¢¿é1¡¢¿é2ΪÊý¾Ý¿é£¬¿ÉÓÃÓÚ´æ·ÅÊý¾Ý
// ÿ¸öÉÈÇøµÄ¿é3Ϊ¿ØÖƿ飨¾ø¶ÔµØַΪ:¿é3¡¢¿é7¡¢¿é11.....£©°üÀ¨ÃÜÂëA£¬´æÈ¡¿ØÖÆ¡¢ÃÜÂëBµÈ
/*******************************
*Á¬Ïß˵Ã÷£º
*1--SDA <----->A4
*2--SCK <----->A5
*3--MOSI <----->A7
*4--MISO <----->A6
*5--Ðü¿Õ
*6--GND <----->GND
*7--RST <----->B0
*8--VCC <----->VCC
************************************/
/*È«¾Ö±äÁ¿*/
#if 1
unsigned char CT[2];//¿¨ÀàÐÍ
unsigned char SN[4]; //¿¨ºÅ
unsigned char RFID[16]; //´æ·ÅRFID
unsigned char lxl_bit=0;
unsigned char card1_bit=0;
unsigned char card2_bit=0;
unsigned char card3_bit=0;
unsigned char card4_bit=0;
unsigned char total=0;
unsigned char lxl[4]={196,58,104,217};
unsigned char card_1[4]={83,106,11,1};
unsigned char card_2[4]={208,121,31,57};
unsigned char card_3[4]={176,177,143,165};
unsigned char card_4[4]={5,158,10,136};
u8 KEY[6]={0xff,0xff,0xff,0xff,0xff,0xff};
u8 AUDIO_OPEN[6] = {0xAA, 0x07, 0x02, 0x00, 0x09, 0xBC};
unsigned char RFID1[16]={0x00,0x00,0x00,0x00,0x00,0x00,0xff,0x07,0x80,0x29,0xff,0xff,0xff,0xff,0xff,0xff};
/*º¯ÊýÉùÃ÷*/
unsigned char status;
unsigned char s=0x08;
#define RC522_DELAY() delay_us(50)
void RC522_Handel(void)
{
status = PcdRequest(PICC_REQALL,CT);//Ñ°¿¨
printf("start rfid
");
//printf("
status>>>>>>%d
", status);
while(status!=MI_OK)//Ñ°¿¨³É¹¦
{
PcdReset();
PcdAntennaOff();
HAL_Delay(1);
PcdAntennaOn();
status = PcdRequest(PICC_REQALL,CT);//Ñ°¿¨
}
status=MI_ERR;
status = PcdAnticoll(SN);//·À³åײ
printf("card is in
");
if (status==MI_OK)//·ÀÐnײ³É¹¦
{
status=MI_ERR;
printf("cardid:%s
",SN);
if((SN[0]==lxl[0])&&(SN[1]==lxl[1])&&(SN[2]==lxl[2])&&(SN[3]==lxl[3]))
{
lxl_bit=1;
printf("
The User is:card_0
");
}else
if((SN[0]==card_1[0])&&(SN[1]==card_1[1])&&(SN[2]==card_1[2])&&(SN[3]==card_1[3]))
{
card1_bit=1;
printf("
The User is:card_1
");
}else
if((SN[0]==card_2[0])&&(SN[1]==card_2[1])&&(SN[2]==card_2[2])&&(SN[3]==card_2[3]))
{
card2_bit=1;
printf("
The User is:card_2
");
}else
if((SN[0]==card_3[0])&&(SN[1]==card_3[1])&&(SN[2]==card_3[2])&&(SN[3]==card_3[3]))
{
card3_bit=1;
printf("
The User is:card_3
");
}else
if((SN[0]==card_4[0])&&(SN[1]==card_4[1])&&(SN[2]==card_4[2])&&(SN[3]==card_4[3]))
{
card4_bit=1;
printf("
The User is:card_4
");
}else
{
printf("
The User is no
");
return;
}
//total=card1_bit+card2_bit+card3_bit+card4_bit+lxl_bit;
status =PcdSelect(SN);
}
else
{
// TODO
}
if(status==MI_OK)//ßx¿¨³É¹¦
{
status=MI_ERR;
status =PcdAuthState(0x60,0x09,KEY,SN);
}
if(status==MI_OK)//òž×C³É¹¦
{
status=MI_ERR;
status=PcdRead(s,RFID);
}
if(status==MI_OK)//×x¿¨³É¹¦
{
status=MI_ERR;
delay_ms(100);
}
}
void RC522_Init(void )
{
GPIO_InitTypeDef GPIO_Initure;
__HAL_RCC_GPIOG_CLK_ENABLE(); //å¼
一周热门 更多>