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