STM8 RC522 读写IC卡程序

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

#include "stm8s.h"

#include "uart.h"

void Delay(u32 nCount);

extern u8 RxBuffer[RxBufferSize];

extern u8 UART_RX_NUM;

unsigned char CT[2];//卡类型

unsigned char SN[4]; //卡号

unsigned char write[16] =

{0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,0x10}; unsigned char read[16] =

{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; unsigned char key[6] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};

#define countof(a) (sizeof(a) / sizeof(*(a)))

#define BufferSize (countof(Tx_Buffer)-1)

u8 Tx_Buffer[] = "STM8S RFID TEST";

u8 Rx_Buffer[BufferSize];

u32 FLASH_ID ;

void cardNo2String(u8 *cardNo, u8 *str);

void main(void)

{

unsigned char status;

/*设置内部时钟16M为主时钟*/

CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);

status = memcmp(read,write,16);

GPIO_DeInit(GPIOA);

GPIO_DeInit(GPIOC);

Uart_Init();

GPIO_Init( GPIOA, GPIO_PIN_4, GPIO_MODE_OUT_PP_HIGH_FAST);

InitRc522();

UART2_SendString(Tx_Buffer,BufferSize);

while(1)

{

status = PcdRequest(PICC_REQALL,CT); /*扫描卡*/

status = PcdAnticoll(SN); /*防冲撞*/

if (status==MI_OK)

{

GPIO_LOW(GPIOA, GPIO_PIN_4); //寻卡成功

UART2_SendString(Tx_Buffer, 17);

Reset_RC522();

}

else

{

GPIO_HIGH(GPIOA, GPIO_PIN_4);

}

}

}

void Delay(u32 nCount)

{

while (nCount != 0)

{

nCount--;

}

}

void Hex2String(u8 hex,u8 *str)

{

str[0] = (hex / 100) + '0';

str[1] = (hex % 100 / 10) + '0';

str[2] = (hex % 10) + '0';

}

void cardNo2String(u8 *cardNo, u8 *str)

{

u8 Count = 0;

for(Count = 0; Count < 4; Count++)

{

Hex2String(cardNo[Count], str + Count * 4);

if(Count == 3)

{

str[15] = '\n';

}

else

{

str[Count * 4 + 3] = ':';

}

}

}

void delay_ns(u32 ns)

for(i=0;i

{

asm("nop");

asm("nop");

asm("nop");

}

}

u8 SPIWriteByte(u8 Byte)

{

u8 tmp;

while (SPI_GetFlagStatus( SPI_FLAG_TXE) == RESET);

SPI_SendData(Byte);

if (SPI_GetFlagStatus(SPI_FLAG_RXNE))

{

tmp = SPI_ReceiveData();

}

return tmp;

}

void SPI2_Init(void)

{

SPI_DeInit();

SPI_Init(

SPI_FIRSTBIT_MSB, //帧模式,先发送MSB还是LSB

SPI_BAUDRATEPRESCALER_8, //波特率分频值

SPI_MODE_MASTER, //模式,主从模式

SPI_CLOCKPOLARITY_LOW, //时钟极性,空闲时SCK为高/空闲时SCK为低

SPI_CLOCKPHASE_1EDGE, //数据采样的边沿选择

SPI_DATADIRECTION_2LINES_FULLDUPLEX, //双线单向数据模式,使用C6/C7两条线,数据传输方向位单向

SPI_NSS_SOFT, //软件从设备

0x07);

SPI_Cmd(ENABLE);

}

void InitRc522(void)

{

SPI2_Init();

PcdReset();

PcdAntennaOff();

PcdAntennaOn();

M500PcdConfigISOType( 'A' );

}

相关文档
最新文档