版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、#include "stm8s.h"#include "uart.h"void Delay(u32 nCount);extern u8 RxBufferRxBufferSize;extern u8 UART_RX_NUM;unsigned char CT2;/卡類型unsigned char SN4; /卡號(hào)unsigned char write16 = 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,0x10;unsigned char read16
2、= 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00;unsigned char key6 = 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_BufferBufferSize;u32 FLASH_ID ;void
3、 cardNo2String(u8 *cardNo, u8 *str);void main(void) unsigned char status; /*設(shè)置內(nèi)部時(shí)鐘16M為主時(shí)鐘*/ 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(); UART
4、2_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("The card Id is:",15); /串口發(fā)送卡號(hào) cardNo2String(SN, Tx_Buffer); UART2_SendString(Tx_Buffer, 17); Re
5、set_RC522(); else GPIO_HIGH(GPIOA, GPIO_PIN_4); void Delay(u32 nCount) while (nCount != 0) nCount-; void Hex2String(u8 hex,u8 *str) str0 = (hex / 100) + '0' str1 = (hex % 100 / 10) + '0' str2 = (hex % 10) + '0'void cardNo2String(u8 *cardNo, u8 *str) u8 Count = 0; for(Count =
6、0; Count < 4; Count+) Hex2String(cardNoCount, str + Count * 4); if(Count = 3) str15 = 'n' else strCount * 4 + 3 = ':' void delay_ns(u32 ns) u32 i; for(i=0;i<ns;i+) asm("nop"); asm("nop"); asm("nop"); u8 SPIWriteByte(u8 Byte) u8 tmp; while (SPI_GetF
7、lagStatus( 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, /幀模式,先發(fā)送MSB還是LSB SPI_BAUDRATEPRESCALER_8, /波特率分頻值 SPI_MODE_MASTER, /模式,主從模式 SPI_CLOCKPOLARITY_LOW, /時(shí)鐘極性,空閑時(shí)
8、SCK為高/空閑時(shí)SCK為低 SPI_CLOCKPHASE_1EDGE, /數(shù)據(jù)采樣的邊沿選擇 SPI_DATADIRECTION_2LINES_FULLDUPLEX, /雙線單向數(shù)據(jù)模式,使用C6/C7兩條線,數(shù)據(jù)傳輸方向位單向 SPI_NSS_SOFT, /軟件從設(shè)備 0x07); SPI_Cmd(ENABLE);void InitRc522(void) SPI2_Init(); PcdReset(); PcdAntennaOff(); PcdAntennaOn(); M500PcdConfigISOType( 'A' );void Reset_RC522(void) Pc
9、dReset(); PcdAntennaOff(); PcdAntennaOn(); /功 能:尋卡/參數(shù)說明: req_codeIN:尋卡方式/ 0x52 = 尋感應(yīng)區(qū)內(nèi)所有符合14443A標(biāo)準(zhǔn)的卡/ 0x26 = 尋未進(jìn)入休眠狀態(tài)的卡/ pTagTypeOUT:卡片類型代碼/ 0x4400 = Mifare_UltraLight/ 0x0400 = Mifare_One(S50)/ 0x0200 = Mifare_One(S70)/ 0x0800 = Mifare_Pro(X)/ 0x4403 = Mifare_DESFire/返 回: 成功返回MI_OK/char PcdRequest(
10、u8 req_code,u8 *pTagType)char status; u8 unLen;u8 ucComMF522BufMAXRLEN; ClearBitMask(Status2Reg,0x08);WriteRawRC(BitFramingReg,0x07);SetBitMask(TxControlReg,0x03); ucComMF522Buf0 = req_code;status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,1,ucComMF522Buf,&unLen);if (status = MI_OK) && (
11、unLen = 0x10) *pTagType = ucComMF522Buf0;*(pTagType+1) = ucComMF522Buf1;else status = MI_ERR; return status;/功 能:防沖撞/參數(shù)說明: pSnrOUT:卡片序列號(hào),4字節(jié)/返 回: 成功返回MI_OK/ char PcdAnticoll(u8 *pSnr) char status; u8 i,snr_check=0; u8 unLen; u8 ucComMF522BufMAXRLEN; ClearBitMask(Status2Reg,0x08); WriteRawRC(BitFrami
12、ngReg,0x00); ClearBitMask(CollReg,0x80); ucComMF522Buf0 = PICC_ANTICOLL1; ucComMF522Buf1 = 0x20; status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,2,ucComMF522Buf,&unLen); if (status = MI_OK) for (i=0; i<4; i+) *(pSnr+i) = ucComMF522Bufi; snr_check = ucComMF522Bufi; if (snr_check != ucComMF52
13、2Bufi) status = MI_ERR; SetBitMask(CollReg,0x80); return status;/功 能:選定卡片/參數(shù)說明: pSnrIN:卡片序列號(hào),4字節(jié)/返 回: 成功返回MI_OK/char PcdSelect(u8 *pSnr) char status; u8 i; u8 unLen; u8 ucComMF522BufMAXRLEN; ucComMF522Buf0 = PICC_ANTICOLL1; ucComMF522Buf1 = 0x70; ucComMF522Buf6 = 0; for (i=0; i<4; i+) ucComMF522B
14、ufi+2 = *(pSnr+i); ucComMF522Buf6 = *(pSnr+i); CalulateCRC(ucComMF522Buf,7,&ucComMF522Buf7); ClearBitMask(Status2Reg,0x08); status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,9,ucComMF522Buf,&unLen); if (status = MI_OK) && (unLen = 0x18) status = MI_OK; else status = MI_ERR; return st
15、atus;/功 能:驗(yàn)證卡片密碼/參數(shù)說明: auth_modeIN: 密碼驗(yàn)證模式/ 0x60 = 驗(yàn)證A密鑰/ 0x61 = 驗(yàn)證B密鑰 / addrIN:塊地址/ pKeyIN:密碼/ pSnrIN:卡片序列號(hào),4字節(jié)/返 回: 成功返回MI_OK/ char PcdAuthState(u8 auth_mode,u8 addr,u8 *pKey,u8 *pSnr) char status; u8 unLen; u8 ucComMF522BufMAXRLEN; ucComMF522Buf0 = auth_mode; ucComMF522Buf1 = addr; memcpy(&uc
16、ComMF522Buf2, pKey, 6); memcpy(&ucComMF522Buf8, pSnr, 4); status = PcdComMF522(PCD_AUTHENT,ucComMF522Buf,12,ucComMF522Buf,&unLen); if (status != MI_OK) | (!(ReadRawRC(Status2Reg) & 0x08) status = MI_ERR; return status;/功 能:讀取M1卡一塊數(shù)據(jù)/參數(shù)說明: addrIN:塊地址/ p OUT:讀出的數(shù)據(jù),16字節(jié)/返 回: 成功返回MI_OK/ char
17、 PcdRead(u8 addr,u8 *p ) char status; u8 unLen; u8 i,ucComMF522BufMAXRLEN; ucComMF522Buf0 = PICC_READ; ucComMF522Buf1 = addr; CalulateCRC(ucComMF522Buf,2,&ucComMF522Buf2); status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,4,ucComMF522Buf,&unLen); if (status = MI_OK) && (unLen = 0x90)
18、 for (i=0; i<16; i+) *(p +i) = ucComMF522Bufi; else status = MI_ERR; return status;/功 能:寫數(shù)據(jù)到M1卡一塊/參數(shù)說明: addrIN:塊地址/ p IN:寫入的數(shù)據(jù),16字節(jié)/返 回: 成功返回MI_OK/ char PcdWrite(u8 addr,u8 *p ) char status; u8 unLen; u8 i,ucComMF522BufMAXRLEN; ucComMF522Buf0 = PICC_WRITE; ucComMF522Buf1 = addr; CalulateCRC(ucCom
19、MF522Buf,2,&ucComMF522Buf2); status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,4,ucComMF522Buf,&unLen); if (status != MI_OK) | (unLen != 4) | (ucComMF522Buf0 & 0x0F) != 0x0A) status = MI_ERR; if (status = MI_OK) for (i=0; i<16; i+) ucComMF522Bufi = *(p +i); CalulateCRC(ucComMF522Buf,1
20、6,&ucComMF522Buf16); status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,18,ucComMF522Buf,&unLen); if (status != MI_OK) | (unLen != 4) | (ucComMF522Buf0 & 0x0F) != 0x0A) status = MI_ERR; return status;/功 能:命令卡片進(jìn)入休眠狀態(tài)/返 回: 成功返回MI_OK/char PcdHalt(void) u8 status; u8 unLen; u8 ucComMF522BufMA
21、XRLEN; ucComMF522Buf0 = PICC_HALT; ucComMF522Buf1 = 0; CalulateCRC(ucComMF522Buf,2,&ucComMF522Buf2); status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,4,ucComMF522Buf,&unLen); return MI_OK;/用MF522計(jì)算CRC16函數(shù)/void CalulateCRC(u8 *pIn ,u8 len,u8 *pOut ) u8 i,n; ClearBitMask(DivIrqReg,0x04); Writ
22、eRawRC(CommandReg,PCD_IDLE); SetBitMask(FIFOLevelReg,0x80); for (i=0; i<len; i+) WriteRawRC(FIFODataReg, *(pIn +i); WriteRawRC(CommandReg, PCD_CALCCRC); i = 0xFF; do n = ReadRawRC(DivIrqReg); i-; while (i!=0) && !(n&0x04); pOut 0 = ReadRawRC(CRCResultRegL); pOut 1 = ReadRawRC(CRCResul
23、tRegM);/功 能:復(fù)位RC522/返 回: 成功返回MI_OK/char PcdReset(void) GPIO_HIGH(RC522RST_GPIO_PORT,RC522RST_GPIO_PIN); delay_ns(10); GPIO_LOW(RC522RST_GPIO_PORT,RC522RST_GPIO_PIN); delay_ns(10); GPIO_HIGH(RC522RST_GPIO_PORT,RC522RST_GPIO_PIN); delay_ns(10); WriteRawRC(CommandReg,PCD_RESETPHASE); delay_ns(10); Writ
24、eRawRC(ModeReg,0x3D); /和Mifare卡通訊,CRC初始值0x6363 WriteRawRC(TReloadRegL,30); WriteRawRC(TReloadRegH,0); WriteRawRC(TModeReg,0x8D); WriteRawRC(TPrescalerReg,0x3E); WriteRawRC(TxAutoReg,0x40);/必須要 return MI_OK;/設(shè)置RC632的工作方式 /char M500PcdConfigISOType(u8 type) if (type = 'A') /ISO14443_A ClearBit
25、Mask(Status2Reg,0x08); WriteRawRC(ModeReg,0x3D); WriteRawRC(RxSelReg,0x86); WriteRawRC(RFCfgReg,0x7F); WriteRawRC(TReloadRegL,30); WriteRawRC(TReloadRegH,0); WriteRawRC(TModeReg,0x8D); WriteRawRC(TPrescalerReg,0x3E); delay_ns(1000); PcdAntennaOn(); else return 1; return MI_OK;/功 能:讀RC632寄存器/參數(shù)說明:Add
26、ressIN:寄存器地址/返 回:讀出的值/u8 ReadRawRC(u8 Address) u8 ucAddr; u8 ucResult=0; GPIO_LOW(RC522NSS_GPIO_PORT,RC522NSS_GPIO_PIN); ucAddr = (Address<<1)&0x7E)|0x80; SPIWriteByte(ucAddr); ucResult=SPIReadByte(); GPIO_HIGH(RC522NSS_GPIO_PORT,RC522NSS_GPIO_PIN); return ucResult;/功 能:寫RC632寄存器/參數(shù)說明:Addr
27、essIN:寄存器地址/ valueIN:寫入的值/void WriteRawRC(u8 Address, u8 value) u8 ucAddr; GPIO_LOW(RC522NSS_GPIO_PORT,RC522NSS_GPIO_PIN); ucAddr = (Address<<1)&0x7E); SPIWriteByte(ucAddr); SPIWriteByte(value);GPIO_HIGH(RC522NSS_GPIO_PORT,RC522NSS_GPIO_PIN);/功 能:置RC522寄存器位/參數(shù)說明:regIN:寄存器地址/ maskIN:置位值/voi
28、d SetBitMask(u8 reg,u8 mask) char tmp = 0x0; tmp = ReadRawRC(reg); WriteRawRC(reg,tmp | mask); / set bit mask/功 能:清RC522寄存器位/參數(shù)說明:regIN:寄存器地址/ maskIN:清位值/void ClearBitMask(u8 reg,u8 mask) char tmp = 0x0; tmp = ReadRawRC(reg); WriteRawRC(reg, tmp & mask); / clear bit mask /功 能:通過RC522和ISO14443卡通訊
29、/參數(shù)說明:CommandIN:RC522命令字/ pIn IN:通過RC522發(fā)送到卡片的數(shù)據(jù)/ InLenByteIN:發(fā)送數(shù)據(jù)的字節(jié)長(zhǎng)度/ pOut OUT:接收到的卡片返回?cái)?shù)據(jù)/ *pOutLenBitOUT:返回?cái)?shù)據(jù)的位長(zhǎng)度/char PcdComMF522(u8 Command, u8 *pIn , u8 InLenByte, u8 *pOut , u8 *pOutLenBit) char status = MI_ERR; u8 irqEn = 0x00; u8 waitFor = 0x00; u8 lastBits; u8 n; u16 i; switch (Command) c
30、ase PCD_AUTHENT: irqEn = 0x12; waitFor = 0x10; break; case PCD_TRANSCEIVE: irqEn = 0x77; waitFor = 0x30; break; default: break; WriteRawRC(ComIEnReg,irqEn|0x80); ClearBitMask(ComIrqReg,0x80);/清所有中斷位 WriteRawRC(CommandReg,PCD_IDLE); SetBitMask(FIFOLevelReg,0x80); /清FIFO緩存 for (i=0; i<InLenByte; i+
31、) WriteRawRC(FIFODataReg, pIn i); WriteRawRC(CommandReg, Command); / n = ReadRawRC(CommandReg); if (Command = PCD_TRANSCEIVE) SetBitMask(BitFramingReg,0x80); /開始傳送 /i = 600;/根據(jù)時(shí)鐘頻率調(diào)整,操作M1卡最大等待時(shí)間25ms i = 10000; do n = ReadRawRC(ComIrqReg); i-; while (i!=0) && !(n&0x01) && !(n&
32、waitFor); ClearBitMask(BitFramingReg,0x80); if (i!=0) if(!(ReadRawRC(ErrorReg)&0x1B) status = MI_OK; if (n & irqEn & 0x01) status = MI_NOTAGERR; if (Command = PCD_TRANSCEIVE) n = ReadRawRC(FIFOLevelReg); lastBits = ReadRawRC(ControlReg) & 0x07; if (lastBits) *pOutLenBit = (n-1)*8 + l
33、astBits; else *pOutLenBit = n*8; if (n = 0) n = 1; if (n > MAXRLEN) n = MAXRLEN; for (i=0; i<n; i+) pOuti = ReadRawRC(FIFODataReg); else status = MI_ERR; SetBitMask(ControlReg,0x80); / stop timer now WriteRawRC(CommandReg,PCD_IDLE); return status;/開啟天線 /每次啟動(dòng)或關(guān)閉天險(xiǎn)發(fā)射之間應(yīng)至少有1ms的間隔/void PcdAntennaOn(void) u8 i; i = ReadRawRC(TxControlReg); if (!(i & 0x03) SetBitMask(TxControlReg, 0x03); /關(guān)閉天線/void PcdAntennaOf
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024財(cái)務(wù)外包服務(wù)合同協(xié)議書
- 二零二五版電商直播領(lǐng)域主播形象使用權(quán)合同3篇
- 2024電影拍攝化妝服務(wù)合同3篇
- 2024版中介第三方擔(dān)保合同
- 2024版勞務(wù)用工合同
- 2024水電能源開發(fā)協(xié)議
- 2024版工程建設(shè)合同補(bǔ)充協(xié)議范本
- 二零二五年度法律援助居間服務(wù)合同范本正規(guī)范本2篇
- 2024版知識(shí)產(chǎn)權(quán)許可使用協(xié)議
- 二零二五年度網(wǎng)絡(luò)游戲開發(fā)合作經(jīng)營(yíng)合同協(xié)議書3篇
- 2024年08月云南省農(nóng)村信用社秋季校園招考750名工作人員筆試歷年參考題庫(kù)附帶答案詳解
- 防詐騙安全知識(shí)培訓(xùn)課件
- 心肺復(fù)蘇課件2024
- 2024年股東股權(quán)繼承轉(zhuǎn)讓協(xié)議3篇
- 2024年認(rèn)證行業(yè)法律法規(guī)及認(rèn)證基礎(chǔ)知識(shí)
- 江蘇省建筑與裝飾工程計(jì)價(jià)定額(2014)電子表格版
- 鑄造工廠設(shè)備管理(共21頁(yè))
- 農(nóng)產(chǎn)品收購(gòu)臺(tái)賬(登記經(jīng)營(yíng)單位及個(gè)體經(jīng)營(yíng)者投售的農(nóng)產(chǎn)品
- 分紅保險(xiǎn)精算規(guī)定
- Proud-of-you中英文歌詞
- 基因的表達(dá)與調(diào)控.ppt
評(píng)論
0/150
提交評(píng)論