找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1818|回复: 0
打印 上一主题 下一主题
收起左侧

mfrc522 51单片机代码

[复制链接]
跳转到指定楼层
楼主
ID:290546 发表于 2018-3-24 17:26 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
/**********************RC522实验开发板例程************************
*  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卡的某一块写为如下格式,则该块为钱包,可接收扣款和充值命令
//4字节金额(低字节在前)+4字节金额取反+4字节金额+1字节块地址+1字节块地址取反+1字节块地址+1字节块地址取反
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)//串口发送程序
{
        SBUF = A;
        while(!TI)                   //如果发送完毕,硬件会置位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("卡的类型:");
            for(i=0;i<2;i++)
                                        {
                                                temp=g_ucTempbuf[i];
                                                printf("%X",temp);
                                               
                                        }
                       
         status = PcdAnticoll(g_ucTempbuf);//防冲撞
         if (status != MI_OK)
         {    continue;    }


                                        ////////以下为超级终端打印出的内容////////////////////////
                               
                                //        printf("卡序列号:");        //超级终端显示,
                                        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);//验证卡片密码
         if (status != MI_OK)
         {    continue;    }

         status = PcdWrite(1, data1);//写块
         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);//验证卡片密码
         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("卡读块:");        //超级终端显示,
//         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();
               
    }
}


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

        TMOD=0x21;                   //设T0为方式1,GATE=1;
        SCON=0x50;
        TH1=0xF4;          //波特率为2400bps
        TL1=0xF4;
        TH0=0;
        TL0=0;
        TR0=1;  
        ET0=1;             //允许T0中断
        TR1=1;                           //开启定时器
        TI=1;
        EA=1;                                   //开启总中断
       
        ES = 1;
        RI = 1;


}







MFRC522测试程序.zip

71.69 KB, 下载次数: 14, 下载积分: 黑币 -5

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表