這是我寫的基于校園卡的電子鎖
附件里面有源代碼
單片機源程序如下:
- #include "rc522.h"
- #include <reg52.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit CS = P2 ^ 0; //片選信號 //lcd屏的引腳
- sbit SID = P2 ^ 1; //數(shù)據(jù)信號
- sbit SCLK = P2 ^ 2; //時鐘信號
- sbit CH = P2 ^ 3; //并行、串行選擇信號
- sbit LED=P1^7;//蜂鳴器
- sbit SDA = P0^0 ; //PF0 SDA rc522的引腳定義
- sbit SCK = P0^1 ; //PF1
- sbit MOSI =P0^2 ; //PF2
- sbit MISO =P0^3 ; //PF3
- sbit RST =P0^4 ; //PF4
- /**串口數(shù)據(jù)使用**/
- #define RX1_Lenth 32 //串口接收緩沖長度
- uchar t=0;
- uchar status;
- uchar a=0x13, b=0x8C, c=0xBE, d=0x27 ;
- uchar e,f,g,h;
- uchar idata RX1_Buffer[32]; //接收緩沖
- uchar TX1_Cnt; //發(fā)送計數(shù)
- uchar RX1_Cnt; //接收計數(shù)
- bit B_TX1_Busy; //發(fā)送忙標志
- uchar buff[4] = { 0 };
- uchar send[4]=0;
- int i=0;
- unsigned char g_ucTempbuf[6];
- void delayms(uint t) //1毫秒延時
- {
- uint i, j;
- for (i = 0; i<t; i++)
- for (j = 0; j<112; j++);
- }
- void delay(uint t) //0.1毫秒延時
- {
- uint i, j;
- for (i = 0; i<t; i++)
- for (j = 0; j<10; j++);
- }
- /////////////////////////////////////////////////////////////////////
- //功 能:尋卡
- //參數(shù)說明: req_code[IN]:尋卡方式
- // 0x52 = 尋感應區(qū)內(nèi)所有符合14443A標準的卡
- // 0x26 = 尋未進入休眠狀態(tài)的卡
- // pTagType[OUT]:卡片類型代碼
- // 0x4400 = Mifare_UltraLight
- // 0x0400 = Mifare_One(S50)
- // 0x0200 = Mifare_One(S70)
- // 0x0800 = Mifare_Pro(X)
- // 0x4403 = Mifare_DESFire
- //返 回: 成功返回MI_OK
- /////////////////////////////////////////////////////////////////////
- char PcdRequest(unsigned char req_code,unsigned char *pTagType)
- {
- char status;
- unsigned int unLen;
- unsigned char ucComMF522Buf[MAXRLEN];
- ClearBitMask(Status2Reg,0x08);
- WriteRawRC(BitFramingReg,0x07);
- SetBitMask(TxControlReg,0x03);
-
- ucComMF522Buf[0] = req_code; //尋卡方式
- status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,1,ucComMF522Buf,&unLen);
- //rc522命令字,發(fā)給卡片的數(shù)據(jù),發(fā)送數(shù)據(jù)的字節(jié)長,接收的數(shù)據(jù),數(shù)據(jù)長
-
- if ((status == MI_OK) && (unLen == 0x10))
- {
- *pTagType = ucComMF522Buf[0];
- *(pTagType+1) = ucComMF522Buf[1];
- }
- else
- { status = MI_ERR;
- }
-
- return status;
- }
- /////////////////////////////////////////////////////////////////////
- //功 能:防沖撞
- //參數(shù)說明: pSnr[OUT]:卡片序列號,4字節(jié)
- //返 回: 成功返回MI_OK
- /////////////////////////////////////////////////////////////////////
- char PcdAnticoll(unsigned char *pSnr)
- {
- char status;
- unsigned char i;
- unsigned int unLen;
- unsigned char ucComMF522Buf[MAXRLEN];
-
- ClearBitMask(Status2Reg,0x08);
- WriteRawRC(BitFramingReg,0x00);
- ClearBitMask(CollReg,0x80);
-
- ucComMF522Buf[0] = PICC_ANTICOLL1;
- ucComMF522Buf[1] = 0x20;
- status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,2,ucComMF522Buf,&unLen);
- if (status == MI_OK)
- {
- for (i=0; i<16; i++)
- {
- *(pSnr+i) = ucComMF522Buf[i];
-
- }
- status=1;
- }
-
- SetBitMask(CollReg,0x80);
- return status;
- }
- char PcdReset(void) //初始化
- {
-
- RST=1;
- delay(1);
- RST=0;
- delay(1);
- RST=1;
- delay(1);
- WriteRawRC(CommandReg,PCD_RESETPHASE);
- delay(1);
-
- WriteRawRC(ModeReg,0x3D); //和Mifare卡通訊,CRC初始值0x6363
- WriteRawRC(TReloadRegL,30);
- WriteRawRC(TReloadRegH,0);
- WriteRawRC(TModeReg,0x8D);
- WriteRawRC(TPrescalerReg,0x3E);
- WriteRawRC(TxAutoReg,0x40);
- return MI_OK;
- }
- /////////////////////////////////////////////////////////////////////
- //功 能:讀RC522寄存器
- //參數(shù)說明:Address[IN]:寄存器地址
- //返 回:讀出的值
- /////////////////////////////////////////////////////////////////////
- unsigned char ReadRawRC(unsigned char Address)
- {
- unsigned char i, ucAddr;
- unsigned char ucResult=0;
- SCK = 0;
- SDA = 0;
- ucAddr = ((Address<<1)&0x7E)|0x80;
- for(i=8;i>0;i--)
- {
- MOSI = ((ucAddr&0x80)==0x80);
- SCK = 1;
- ucAddr <<= 1;
- SCK = 0;
-
- }
- for(i=8;i>0;i--)
- {
- SCK = 1;
- ucResult <<= 1;
- ucResult|=MISO;
- SCK = 0;
-
- }
- SDA = 1;
- SCK = 1;
- return ucResult;
- }
- /////////////////////////////////////////////////////////////////////
- //功 能:寫RC522寄存器
- //參數(shù)說明:Address[IN]:寄存器地址
- // value[IN]:寫入的值
- /////////////////////////////////////////////////////////////////////
- void WriteRawRC(unsigned char Address, unsigned char value)
- {
- unsigned char i, ucAddr;
- SCK = 0;
- SDA = 0;
- ucAddr = ((Address<<1)&0x7E);
- for(i=8;i>0;i--)
- {
- MOSI = ((ucAddr&0x80)==0x80);
- SCK = 1;
- ucAddr <<= 1;
- SCK = 0;
-
- }
- for(i=8;i>0;i--)
- {
- MOSI = ((value&0x80)==0x80);
- SCK = 1;
- value <<= 1;
- SCK = 0;
-
- }
- SDA = 1;
- SCK = 1;
- }
- /////////////////////////////////////////////////////////////////////
- //功 能:置RC522寄存器位
- //參數(shù)說明:reg[IN]:寄存器地址
- // mask[IN]:置位值
- /////////////////////////////////////////////////////////////////////
- void SetBitMask(unsigned char reg,unsigned char mask)
- {
- char tmp = 0x0;
- tmp = ReadRawRC(reg);
- WriteRawRC(reg,tmp | mask); // set bit mask
- }
- /////////////////////////////////////////////////////////////////////
- //功 能:清RC522寄存器位
- //參數(shù)說明:reg[IN]:寄存器地址
- // mask[IN]:清位值
- /////////////////////////////////////////////////////////////////////
- void ClearBitMask(unsigned char reg,unsigned char mask)
- {
- char tmp = 0x0;
- tmp = ReadRawRC(reg);
- WriteRawRC(reg, tmp & ~mask); // clear bit mask
- }
- /////////////////////////////////////////////////////////////////////
- //功 能:通過RC522和ISO14443卡通訊
- //參數(shù)說明:Command[IN]:RC522命令字
- // pInData[IN]:通過RC522發(fā)送到卡片的數(shù)據(jù)
- // InLenByte[IN]:發(fā)送數(shù)據(jù)的字節(jié)長度
- // pOutData[OUT]:接收到的卡片返回數(shù)據(jù)
- // *pOutLenBit[OUT]:返回數(shù)據(jù)的位長度
- /////////////////////////////////////////////////////////////////////
- char PcdComMF522(unsigned char Command, //命令
- unsigned char *pInData, //rc522到卡片的數(shù)據(jù)
- unsigned char InLenByte,//發(fā)送數(shù)據(jù)的字節(jié)長度
- unsigned char *pOutData, //收到的數(shù)據(jù)
- unsigned int *pOutLenBit)//接受數(shù)據(jù)的字節(jié)長度長度
- {
- char status = MI_ERR;
- unsigned char irqEn = 0x00;
- unsigned char waitFor = 0x00;
- unsigned char lastBits;
- unsigned char n;
- unsigned int i;
- switch (Command)
- {
- case PCD_AUTHENT: //驗證密鑰
- irqEn = 0x12;
- waitFor = 0x10;
- break;
- case PCD_TRANSCEIVE://發(fā)送并接受數(shù)據(jù)
- irqEn = 0x77;
- waitFor = 0x30;
- break;
- default:
- break;
- }
-
- WriteRawRC(ComIEnReg,irqEn|0x80);
- ClearBitMask(ComIrqReg,0x80);
- WriteRawRC(CommandReg,PCD_IDLE);
- SetBitMask(FIFOLevelReg,0x80);
-
- for (i=0; i<InLenByte; i++)
- { WriteRawRC(FIFODataReg, pInData[i]); }
- WriteRawRC(CommandReg, Command);
-
-
- if (Command == PCD_TRANSCEIVE)
- { SetBitMask(BitFramingReg,0x80); }
-
- i = 1000;//根據(jù)時鐘頻率調(diào)整,操作M1卡最大等待時間25ms
- do
- {
- n = ReadRawRC(ComIrqReg);
- i--;
- }
- while ((i!=0) && !(n&0x01) && !(n&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 + lastBits; }
- else
- { *pOutLenBit = n*8; }
- if (n == 0)
- { n = 1; }
- if (n > MAXRLEN)
- { n = MAXRLEN; }
- for (i=0; i<n; i++)
- { pOutData[i] = ReadRawRC(FIFODataReg); }
- }
- }
- else
- { status = MI_ERR; }
-
- }
-
- SetBitMask(ControlReg,0x80); // stop timer now
- WriteRawRC(CommandReg,PCD_IDLE);
- return status;
- }
- /////////////////////////////////////////////////////////////////////
- //開啟天線
- //每次啟動或關(guān)閉天險發(fā)射之間應至少有1ms的間隔
- /////////////////////////////////////////////////////////////////////
- void PcdAntennaOn()
- {
- unsigned char i;
- i = ReadRawRC(TxControlReg);
- if (!(i & 0x03))
- {
- SetBitMask(TxControlReg, 0x03);
- }
- }
- /////////////////////////////////////////////////////////////////////
- //關(guān)閉天線
- /////////////////////////////////////////////////////////////////////
- void PcdAntennaOff()
- {
- ClearBitMask(TxControlReg, 0x03);
- }
- /********************************************************************
- * 名稱 : Uart_Init()
- * 功能 : 串口初始化,晶振11.0592,波特率9600,使能了串口中斷
- * 輸入 : 無
- * 輸出 : 無
- ***********************************************************************/
- void Uart_Init(void)
- {
- TMOD = 0x20; //定時器工作在定時器1的方式2
- PCON = 0x00; //不倍頻
- SCON = 0x50; //串口工作在方式1,并且啟動串行接收
- TH1 = 0xFD; //設置波特率 9600
- TL1 = 0xFD;
- TR1 = 1; //啟動定時器1
- ES = 1; //開串口中斷
- EA = 1; //開總中斷
- RX1_Cnt=0; //接受計數(shù)清零
- TX1_Cnt = 0; //發(fā)送計數(shù)清零
- B_TX1_Busy = 0;//初始化判忙標志位
- }
- void main()
- {
-
- PcdReset(); //復位
- PcdAntennaOff(); //關(guān)天線
- delay(100); //延時10ms
- PcdAntennaOn(); //開天線
- delay(100);
- Uart_Init(); //串口通信初始化
-
- //連接云服務器
- //uchar idata buff1[47]="AT+CIPSTART=\"TCP\",\"60.205.224.195\",8234\r\n";
- //uchar idata buff2[16]="AT+CGACT=1,1\r\n";
- //uchar idata buff3[17]="AT+CIPTMODE=1\r\n";
- // for(t=0;t<14;t++)
- // {
- // SBUF=buff4[t];delay(10);
- // }
- // delayms(1000);
- // delayms(1000);
- //
- // for(t=0;t<16;t++)
- // {
- // SBUF=buff2[t];delay(10);
- // }
- // delayms(1000);
- // delayms(1000);
- // flag=0;
- //
- // for(t=0;t<47;t++)
- // {
- // SBUF=buff1[t];delay(10);
- // }
- // delayms(1000);
- // delayms(1000);
- // delayms(1000);
- // delayms(1000);
- //
- // flag=1;
- //
- // for(t=0;t<17;t++)
- // {
- // SBUF=buff3[t];delay(10);
- // }
- // delayms(1000);
- // flag=0;
- // write_com(0x80);delay(10);
- // hzkdis("開始簽到");
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
所有資料51hei提供下載:
rfid電子鎖.zip
(80.39 KB, 下載次數(shù): 27)
2018-6-19 20:30 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|