找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 1849|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

mfrc522 51單片機(jī)代碼

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:290546 發(fā)表于 2018-3-24 17:26 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
/**********************RC522實(shí)驗(yàn)開(kāi)發(fā)板例程************************
*  CPU:STC89C52RC
*  晶振:11.0592MHZ
------------------------------------------------*/
#include <reg52.h>
#include  <STDIO.H>
#include <string.h>
#include "main.h"
#include "mfrc522.h"
#include<intrins.h>
unsigned char code data1[16] = {0x12,0x34,0x56,0x78,0xED,0xCB,0xA9,0x87,0x12,0x34,0x56,0x78,0x01,0xFE,0x01,0xFE};
//M1卡的某一塊寫(xiě)為如下格式,則該塊為錢(qián)包,可接收扣款和充值命令
//4字節(jié)金額(低字節(jié)在前)+4字節(jié)金額取反+4字節(jié)金額+1字節(jié)塊地址+1字節(jié)塊地址取反+1字節(jié)塊地址+1字節(jié)塊地址取反
unsigned char code data2[4]  = {0,0,0,0x01};
unsigned char code DefaultKey[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};


unsigned char g_ucTempbuf[20];                        
void delay1(unsigned int z)
{
        unsigned int x,y;
        for(x=z;x>0;x--)
        for(y=110;y>0;y--);       
}  
void send(char A)//串口發(fā)送程序
{
        SBUF = A;
        while(!TI)                   //如果發(fā)送完畢,硬件會(huì)置位TI
        {
                _nop_();       
        }
        TI = 0;                                //TI清零
}

void main( )
{   
     unsigned char status,i,j;
          unsigned int temp;
     InitializeSystem( );
     PcdReset();
     PcdAntennaOff();
     PcdAntennaOn();
     while ( 1 )
     {   


         status = PcdRequest(PICC_REQALL, g_ucTempbuf);//尋卡
         if (status != MI_OK)
         {   
              InitializeSystem( );
     PcdReset();
     PcdAntennaOff();
     PcdAntennaOn();
                          continue;
         }
                             
                printf("卡的類(lèi)型:");
            for(i=0;i<2;i++)
                                        {
                                                temp=g_ucTempbuf[i];
                                                printf("%X",temp);
                                               
                                        }
                       
         status = PcdAnticoll(g_ucTempbuf);//防沖撞
         if (status != MI_OK)
         {    continue;    }


                                        ////////以下為超級(jí)終端打印出的內(nèi)容////////////////////////
                               
                                //        printf("卡序列號(hào):");        //超級(jí)終端顯示,
                                        for(i=0;i<1;i++)
                                        {
                                                temp=g_ucTempbuf[i];
                                                if(temp==0xE0)
                                                {
                                                        temp=1;
                                                        send(1);               
                                                }
                                           if(temp==0xB3)
                                                {
                                                        temp=2;
                                                        send(2);
                                                       
                                                }
                                                 if(temp==0x80)
                                                {
                                                        temp=3;
                                                        send(3);
                                               
                                                }
                                                if(temp==0xC1)
                                                {
                                                        temp=4;
                                                        send(4);
                                                       
                                                }
                                                if(temp==0xA2)
                                                {
                                                        temp=5;
                                                        send(5);
                                               
                                                }
                                                if(temp==0x24)
                                                {
                                                        temp=6;
                                                        send(6);
                                                       
                                                }
                                                if(temp==0x90)
                                                {
                                                        temp=7;
                                                        send(7);
                                               
                                                }
                                                if(temp==0x31)
                                                {
                                                        temp=8;
                                                        send(8);
                                               
                                                }
                                                if(temp==0x63)
                                                {
                                                        temp=9;
                                                        send(9);
                                       
                                                }
                                               
                                        }

                                        ///////////////////////////////////////////////////////////

         status = PcdSelect(g_ucTempbuf);//選定卡片
         if (status != MI_OK)
         {    continue;    }

         status = PcdAuthState(PICC_AUTHENT1A, 1, DefaultKey, g_ucTempbuf);//驗(yàn)證卡片密碼
         if (status != MI_OK)
         {    continue;    }

         status = PcdWrite(1, data1);//寫(xiě)塊
         if (status != MI_OK)
         {    continue;    }

               
         status = PcdRequest(PICC_REQALL, g_ucTempbuf);//尋卡
         if (status != MI_OK)
         {   
                 InitializeSystem( );
     PcdReset();
     PcdAntennaOff();
     PcdAntennaOn();
                          continue;
         }
                 status = PcdAnticoll(g_ucTempbuf);//防沖撞
         if (status != MI_OK)
         {    continue;    }
                status = PcdSelect(g_ucTempbuf);//選定卡片
         if (status != MI_OK)
         {    continue;    }

         status = PcdAuthState(PICC_AUTHENT1A, 1, DefaultKey, g_ucTempbuf);//驗(yàn)證卡片密碼
         if (status != MI_OK)
         {    continue;    }


         status = PcdValue(PICC_DECREMENT,1,data2);//扣款
         if (status != MI_OK)
         {    continue;    }
                 
         status = PcdBakValue(1, 2);//塊備份
         if (status != MI_OK)
         {    continue;    }

         status = PcdRead(2, g_ucTempbuf);//讀塊
         if (status != MI_OK)
         {    continue;    }
//                printf("卡讀塊:");        //超級(jí)終端顯示,
//         for(i=0;i<16;i++)
//                                        {
//                                                temp=g_ucTempbuf[i];
//                                                printf("%X",temp);
//                                               
//                                        }
//
//                 printf("\n");
                LED_GREEN = 0;
            for(i=0;i<250;i++)//先輸出一種頻率的聲音
        {
         for(j=0;j<80;j++);
         Speaker=~Speaker;
        }
                delay1(100);
                LED_GREEN = 1;
                delay1(100);
                LED_GREEN = 0;
                delay1(200);
                LED_GREEN = 1;                                                         
                 PcdHalt();
               
    }
}


/////////////////////////////////////////////////////////////////////
//系統(tǒng)初始化
/////////////////////////////////////////////////////////////////////
void InitializeSystem()
{
/*        P0M1 = 0x0; P0M2 = 0x0;
        P1M1 = 0x0;        P1M2 = 0x0;                     
        P3M1 = 0x0; P3M2 = 0xFF;
*/        P0 = 0xFF; P1 = 0xFF; P3 = 0xFF;P2 = 0xFF;

        TMOD=0x21;                   //設(shè)T0為方式1,GATE=1;
        SCON=0x50;
        TH1=0xF4;          //波特率為2400bps
        TL1=0xF4;
        TH0=0;
        TL0=0;
        TR0=1;  
        ET0=1;             //允許T0中斷
        TR1=1;                           //開(kāi)啟定時(shí)器
        TI=1;
        EA=1;                                   //開(kāi)啟總中斷
       
        ES = 1;
        RI = 1;


}







MFRC522測(cè)試程序.zip

71.69 KB, 下載次數(shù): 14, 下載積分: 黑幣 -5

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表