STM8 读写RC522
时间:10-02
整理:3721RD
点击:
最近在写读卡器案子,,一直寻卡不成功,读写寄存器好像没问题,有没有人做过,指点下。
#define NSS522_1 PB_ODR_ODR4=1 // 从机选择无效
#define NSS522_0 PB_ODR_ODR4=0 // 从机选择有效
#define SCK522_1 PB_ODR_ODR3=1 // 数据时钟置高
#define SCK522_0 PB_ODR_ODR3=0 // 数据时钟置低
#define SI522_1 PB_ODR_ODR2=1 // 数据置高
#define SI522_0 PB_ODR_ODR2=0 // 数据置低
#define SO522 PB_IDR_IDR1 // 数据输入
#define RST522_1 PB_ODR_ODR0=1
#define RST522_0 PB_ODR_ODR0=0
#define _NOP() {asm("nop");asm("nop");asm("nop");}
//==============================================================================
void RC522_IO_Init(void)
{
PB_DDR = 0xFD; //pe5 output
PB_CR1 = 0xFD;
PB_CR2 = 0xFD;
}
unsigned char ReadRawRC(unsigned char Address)
{
unsigned char i, ucAddr;
unsigned char ucResult=0;
SCK522_0;
NSS522_0;
ucAddr = ((Address<<1)&0x7E)|0x80;
for(i=8;i>0;i--)
{
if ((ucAddr&0x80)==0x80)
{ SI522_1; }
else
{ SI522_0; }
_NOP();
SCK522_1;
ucAddr <<= 1;
_NOP();
_NOP();
SCK522_0;
}
for(i=8;i>0;i--)
{
SCK522_1;
_NOP();
ucResult <<= 1;
ucResult |= SO522;
SCK522_0;
_NOP();
_NOP();
}
NSS522_1;
SCK522_1;
return ucResult;
}
//==================================================================
void WriteRawRC(unsigned char Address, unsigned char value)
{
unsigned char i, ucAddr;
SCK522_0;
NSS522_0;
ucAddr = ((Address<<1)&0x7E);
for(i=8;i>0;i--)
{
if ((ucAddr&0x80)==0x80)
{ SI522_1; }
else
{ SI522_0; }
_NOP();
SCK522_1;
_NOP();
_NOP();
ucAddr <<= 1;
_NOP();
SCK522_0;
}
for(i=8;i>0;i--)
{
if ((value&0x80)==0x80)
{ SI522_1; }
else
{ SI522_0; }
_NOP();
SCK522_1;
_NOP();
_NOP();
value <<= 1;
_NOP();
SCK522_0;
}
NSS522_1;
SCK522_1;
}
//======================================================================
unsigned char data1[16] = {0x12,0x34,0x56,0x78,0xED,0xCB,0xA9,0x87,0x12,0x34,0x56,0x78,0x01,0xFE,0x01,0xFE};
//M1卡的某一块写为如下格式,则该块为钱包,可接收扣款和充值命令
//4字节金额(低字节在前)+4字节金额取反+4字节金额+1字节块地址+1字节块地址取反+1字节块地址+1字节块地址取反
unsigned char data2[4] = {0x12,0,0,0};
unsigned char DefaultKey[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
unsigned char g_ucTempbuf[20];
int main(void)
{
Clock_Set_HSI();
GPIO_Init();
RC522_IO_Init();
PcdReset();
PcdAntennaOff();
PcdAntennaOn();
while(1)
{
status = PcdRequest(PICC_REQIDL, g_ucTempbuf);
if (status == MI_OK)
{
PA_ODR_ODR2=1; // Beep=1
}
}
}
#define NSS522_1 PB_ODR_ODR4=1 // 从机选择无效
#define NSS522_0 PB_ODR_ODR4=0 // 从机选择有效
#define SCK522_1 PB_ODR_ODR3=1 // 数据时钟置高
#define SCK522_0 PB_ODR_ODR3=0 // 数据时钟置低
#define SI522_1 PB_ODR_ODR2=1 // 数据置高
#define SI522_0 PB_ODR_ODR2=0 // 数据置低
#define SO522 PB_IDR_IDR1 // 数据输入
#define RST522_1 PB_ODR_ODR0=1
#define RST522_0 PB_ODR_ODR0=0
#define _NOP() {asm("nop");asm("nop");asm("nop");}
//==============================================================================
void RC522_IO_Init(void)
{
PB_DDR = 0xFD; //pe5 output
PB_CR1 = 0xFD;
PB_CR2 = 0xFD;
}
unsigned char ReadRawRC(unsigned char Address)
{
unsigned char i, ucAddr;
unsigned char ucResult=0;
SCK522_0;
NSS522_0;
ucAddr = ((Address<<1)&0x7E)|0x80;
for(i=8;i>0;i--)
{
if ((ucAddr&0x80)==0x80)
{ SI522_1; }
else
{ SI522_0; }
_NOP();
SCK522_1;
ucAddr <<= 1;
_NOP();
_NOP();
SCK522_0;
}
for(i=8;i>0;i--)
{
SCK522_1;
_NOP();
ucResult <<= 1;
ucResult |= SO522;
SCK522_0;
_NOP();
_NOP();
}
NSS522_1;
SCK522_1;
return ucResult;
}
//==================================================================
void WriteRawRC(unsigned char Address, unsigned char value)
{
unsigned char i, ucAddr;
SCK522_0;
NSS522_0;
ucAddr = ((Address<<1)&0x7E);
for(i=8;i>0;i--)
{
if ((ucAddr&0x80)==0x80)
{ SI522_1; }
else
{ SI522_0; }
_NOP();
SCK522_1;
_NOP();
_NOP();
ucAddr <<= 1;
_NOP();
SCK522_0;
}
for(i=8;i>0;i--)
{
if ((value&0x80)==0x80)
{ SI522_1; }
else
{ SI522_0; }
_NOP();
SCK522_1;
_NOP();
_NOP();
value <<= 1;
_NOP();
SCK522_0;
}
NSS522_1;
SCK522_1;
}
//======================================================================
unsigned char data1[16] = {0x12,0x34,0x56,0x78,0xED,0xCB,0xA9,0x87,0x12,0x34,0x56,0x78,0x01,0xFE,0x01,0xFE};
//M1卡的某一块写为如下格式,则该块为钱包,可接收扣款和充值命令
//4字节金额(低字节在前)+4字节金额取反+4字节金额+1字节块地址+1字节块地址取反+1字节块地址+1字节块地址取反
unsigned char data2[4] = {0x12,0,0,0};
unsigned char DefaultKey[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
unsigned char g_ucTempbuf[20];
int main(void)
{
Clock_Set_HSI();
GPIO_Init();
RC522_IO_Init();
PcdReset();
PcdAntennaOff();
PcdAntennaOn();
while(1)
{
status = PcdRequest(PICC_REQIDL, g_ucTempbuf);
if (status == MI_OK)
{
PA_ODR_ODR2=1; // Beep=1
}
}
}