可以參考一下,keil代碼。
RC522在STC89C52上開發實例
STC-ISP V39.exe下載就不會擦出eeprom,STC_ISP_V479.exe下載會將eeprom擦出
單片機源程序如下:
- /****************************************Copyright (c)**************************************************
- M1卡使用情況:
- 1.卡號對應不同人員
- 2.塊號1存放該人員可進入的區域編號,從第一字節表示區域編號為1,
- 3.如果通過,則紅燈亮,否則,不亮。
- 4.每次刷卡,RC522上傳卡號以及塊號1內容到上位機
- ********************************************************************************************************/
- #include "STC12C54xx.h"
- #include "eeprom.h"
- #include "mfrc522.h"
- //uchar 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字節塊地址取反
- //uchar code money[4] = {1,0,0,0};
- uchar code DefaultKey[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; //初始密碼
- uchar code administer_key[6]={0,0,0,0,0,0};//管理員卡密碼
- uchar code this=0x01;
- uchar code bianhao[16]={0x01,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
- //*********全局變量定義*********************************//
- static uchar xdata Rec_Buffer[REC_BUFFER_SIZE]; //串口接收緩沖區
- static uchar Recvd_Count; //串口已接收數據的個數
- unsigned char g_ucTempbuf1[4]; //返回卡號
- unsigned char g_ucTempbuf2[16]; //返回塊中16位數據
- bit time_flag=0;//1秒時間標志位
- bit mode =0;//模式選擇標志位,0為定時計費模式,1為流量計費模式
- static uint gather_money=0;//匯總金額變量,需要充分考慮停電問題????????????????????
- /*****************************************************************************
- *原型:void time0_init(void)
- *功能:定時器0初始化
- *input:無
- *ouput:無
- 用于定時計費
- ******************************************************************************/
- void time0_init(void)
- {
- if(mode==0)
- {
- TMOD = TMOD|0x01;
- TH0 = 0x10;
- TL0 = 0x00;//4096,定時15次就是1S,11.0592M
- }
- if(mode==1)
- {
- TMOD = TMOD|0x05;//計外部脈沖
- TH0 = 0xff;
- TL0 = 0xfe;//一個脈沖來則溢出
- TR0 =1;
- }
- // TR0 =1;
- ET0 =1;
- }
- void timer0() interrupt 1
- {
- static uchar overflow=0;
- if(mode==0)
- {
- TH0 = 0x10;
- TL0 = 0x00;//4096,定時15次就是1S,11.0592M
- overflow++;
- if(overflow>=15)//注意:如果overflow未付初值,此處if(overflow==15),第一次得不到立即響應,改成>=則可以立即響應
- {
- overflow=0;
- time_flag=1;
- LED=~LED;//綠色指示燈閃爍,表示正在計費
- }
- }
- if(mode==1)
- {
- TH0 = 0xff;
- TL0 = 0xfe;//一個脈沖來則溢出
- time_flag=1;
- }
-
- }
- //-------------------------------- ------------------------------------------------------------------
- // 函數名稱: delay
- // 入口參數: N
- // 函數功能:延時子程序,實現(16*N+24)us的延時
- // 系統采用11.0592MHz的時鐘時,延時滿足要求,其它情況需要改動
- //--------------------------------------------------------------------------------------------------
- void Delay_ms(uint N)
- {
- uint i,j;
- for(j=0;j<1000;j++)
- for(i=0;i<=N;i++);
- }
- void Pass()
- {
- beep=0;
- Delay_ms(50);
- beep=1;
-
- }
- /*******************************************************************************
- * Function Name : UART_Init
- * Description : 初始化串行口和波特率發生器(用定時器1)
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- void UART_Init(void)
- {
- SCON =0x50; //選擇串口工作方式,打開接收允許
- TMOD =TMOD|0x21; //定時器1工作在方式2,定時器0工作在方式1
- PCON|=0x80;
- TH1=0xff; //BaudRate=57600(系統時鐘11.0592MHZ)
- TL1=0xff;
- TR1 =1; //啟動定時器T1
- ET1 =0;
- TR0 =1; //啟動定時器T0
- ET0 =1; //允許定時器0中斷
- ES =1; //允許串行口中斷
-
- }
- /*******************************************************************************
- * Function Name : Sent_Byte
- * Description : 發送一個字節到主機
- * Input : Sdata:要發送的字節
- * Output : None
- * Return : None
- *******************************************************************************/
- void Sent_Byte(unsigned char Sdata)
- {
- TI=0;
- SBUF=Sdata;
- while(TI==0) ;
- TI=0;
- }
- /*******************************************************************************
- * Function Name : Sent_String
- * Description : 發送字節串到主機
- * Input : pt_send:要發送的字符串
- * Output : None
- * Return : None
- *******************************************************************************/
- void Sent_String(unsigned char *pt_send)
- {
- TI=0;
- while(*pt_send!='\0')
- {
- Sent_Byte(*pt_send++);
- }
- }
- /*******************************************************************************
- * Function Name : Sent_Buffer
- * Description : 向串口1發送指定長度的數據
- * Input : string:發送緩沖區指針;len:發送長度.
- * Output : None
- * Return : None
- *******************************************************************************/
- void Sent_Buffer(uchar *string,uchar len)
- {uchar i;
- ES = 0;
- for(i=0;i<len;i++)
- {
- Sent_Byte(*string++);
- }
- ES = 1;
- }
- //********************主函數******************************************
- main()
- {
-
- uchar status;
- unsigned long int display_data=0;
- WDT_Disable;
- UART_Init(); //初始化串口
- EA =1; //單片機中斷允許
- //初始化射頻芯片
- PcdReset();
- PcdAntennaOff();
- PcdAntennaOn();
- Delay_ms(100);
- time0_init(); //初始化定時器0,要在讀mode以后
- //復位芯片,打開天線
- PcdReset();
- PcdAntennaOff();
- PcdAntennaOn();
- Delay_ms(100);
-
- while(1)
- {
- //尋卡,輸出為卡類型
- status = PcdRequest(PICC_REQALL, g_ucTempbuf1);//*PICC_REQALL=0x52:尋天線區內所有符合14443A標準的卡 PICC_REQIDL=0x26:只尋未進入休眠狀態的卡
- if (status == MI_OK)
- status = PcdAnticoll(g_ucTempbuf1); //防沖撞處理,輸出卡片序列號,4字節
- if (status == MI_OK)
- status = PcdSelect(g_ucTempbuf1); //選擇卡片,輸入卡片序列號,4字節
- if (status == MI_OK)
- status = PcdAuthState(PICC_AUTHENT1A, 1, DefaultKey, g_ucTempbuf1);
- if (status == MI_OK)
- status = PcdRead(1, g_ucTempbuf2);//讀卡
-
- // if (status == MI_OK)
- //{
- //status=PcdWrite(1,bianhao);
- // }
- if (status == MI_OK)
- status = PcdRead(1,g_ucTempbuf2);//讀卡
- if(status==MI_OK)
- {
- Pass();
- //Sent_Byte(this);
- Sent_Byte(g_ucTempbuf1[0]);Sent_Byte(g_ucTempbuf1[1]);Sent_Byte(g_ucTempbuf1[2]);Sent_Byte(g_ucTempbuf1[3]); //卡號
- Sent_Byte(g_ucTempbuf2[0]);Sent_Byte(g_ucTempbuf2[1]);Sent_Byte(g_ucTempbuf2[2]);Sent_Byte(g_ucTempbuf2[3]); //卡中第一塊數據內容
- Sent_Byte(g_ucTempbuf2[4]);Sent_Byte(g_ucTempbuf2[5]);Sent_Byte(g_ucTempbuf2[6]);Sent_Byte(g_ucTempbuf2[7]);
- Sent_Byte(g_ucTempbuf2[8]);Sent_Byte(g_ucTempbuf2[9]);Sent_Byte(g_ucTempbuf2[10]);Sent_Byte(g_ucTempbuf2[11]);
- Sent_Byte(g_ucTempbuf2[12]);Sent_Byte(g_ucTempbuf2[13]);Sent_Byte(g_ucTempbuf2[14]);Sent_Byte(g_ucTempbuf2[15]);
- }
-
- }
- }
- /*******************************************************************************
- * Function Name : Serial ()
- * Description : 串口中斷函數
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- void Serial () interrupt 4 using 2
- {
- EA = 0;
- if(RI)
- { RI=0;
- if(Recvd_Count>=REC_BUFFER_SIZE) Recvd_Count=REC_BUFFER_SIZE-1;
- Rec_Buffer[Recvd_Count++]=SBUF;
- }
- EA = 1;
- }
復制代碼
所有資料51hei提供下載:
RFID&#143;.rar
(77.9 KB, 下載次數: 10)
2018-10-7 09:20 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|