• 大小: 6KB
    文件类型: .c
    金币: 1
    下载: 0 次
    发布日期: 2021-06-08
  • 语言: 其他
  • 标签: mfrc522  

资源简介

mfrc522 树莓派C代码 自己按代码链接设备或修改代码中的io

资源截图

代码片段和文件信息

#include 
#include “RFID.h“
#include 
#include 

void writeMFRC522(unsigned char Address unsigned char value);
void antennaOn(void);
void RFID_init()
{
bcm2835_spi_begin();
 
bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST);
bcm2835_spi_setDataMode(BCM2835_SPI_MODE0);
bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_65536);
bcm2835_spi_chipSelect(BCM2835_SPI_CS0);
bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0 LOW);
 
writeMFRC522(CommandReg PCD_RESETPHASE);
writeMFRC522(TModeReg 0x8D);
writeMFRC522(TPrescalerReg 0x3E);
writeMFRC522(TReloadRegL 30);
writeMFRC522(TReloadRegH 0);
writeMFRC522(TxAutoReg 0x40);
writeMFRC522(ModeReg 0x3D);
antennaOn();
}

void writeMFRC522(unsigned char Address unsigned char value)
{
    char buff[2];
    buff[0] = (char)((Address<<1)&0x7E);
    buff[1] = (char)value;
    
    bcm2835_spi_transfern(buff2); 
}

unsigned char readMFRC522(unsigned char Address)
{
    char buff[2];
    buff[0] = ((Address<<1)&0x7E)|0x80;
    bcm2835_spi_transfern(buff2);
    return (uint8_t)buff[1];
}

void setBitMask(unsigned char reg unsigned char mask)
{
unsigned char tmp;
tmp = readMFRC522(reg);
writeMFRC522(reg tmp | mask);
}

void clearBitMask(unsigned char reg unsigned char mask)
{
unsigned char tmp;
tmp = readMFRC522(reg);
writeMFRC522(reg tmp & (~mask));
}

void antennaOn(void)
{
unsigned char temp;
temp = readMFRC522(TxControlReg);
if(!(temp & 0x03))
{
setBitMask(TxControlReg 0x03);
}
}

void antennaOff(void)
{
unsigned char temp;
temp = readMFRC522(TxControlReg);
if(!(temp & 0x03))
{
clearBitMask(TxControlReg 0x03);
}
}

void calculateCRC(unsigned char *pIndata unsigned char len unsigned char *pOutData)
{
unsigned char i n;
clearBitMask(DivIrqReg 0x04);
setBitMask(FIFOLevelReg 0x80);
for (i=0; i {
writeMFRC522(FIFODataReg *(pIndata+i));
}
writeMFRC522(CommandReg PCD_CALCCRC);
i = 0xFF;
do
{
n = readMFRC522(DivIrqReg);
i--;
}
while ((i!=0) && !(n&0x04));
pOutData[0] = readMFRC522(CRCResultRegL);
pOutData[1] = readMFRC522(CRCResultRegM);
}

unsigned char MFRC522ToCard(unsigned char command unsigned char *sendData unsigned char sendLen unsigned char *backData unsigned int *backLen)
{
unsigned char status = MI_ERR;
unsigned char irqEn = 0x00;
unsigned char waitIRq = 0x00;
unsigned char lastBits;
unsigned char n;
unsigned int i;
switch(command)
{
case PCD_AUTHENT:     //认证卡密
{
irqEn = 0x12;
waitIRq = 0x10;
break;
}
case PCD_TRANSCEIVE:  //发送FIFO中数据
{
irqEn = 0x77;
waitIRq = 0x30;
break;
}
default:
break;
  }
writeMFRC522(CommIEnReg irqEn|0x80); //允许中断请求
clearBitMask(CommIrqReg 0x80);       //清除所有中断请求位
setBitMask(FIFOLevelReg 0x80);       //Flush

评论

共有 条评论