|
基于stm8的ds1302模塊千萬別把數(shù)據(jù)寫在第3個(gè)區(qū),不然卡就鎖住了,只能寫在第2個(gè)區(qū)
電路原理圖如下:
0.jpg (111.98 KB, 下載次數(shù): 63)
下載附件
2018-4-4 04:45 上傳
IMG_20180322_145435.jpg (167.7 KB, 下載次數(shù): 43)
下載附件
2018-4-4 04:46 上傳
IMG_20180322_145440.jpg (149.32 KB, 下載次數(shù): 56)
下載附件
2018-4-4 04:46 上傳
IMG_20180322_145505.jpg (281.48 KB, 下載次數(shù): 45)
下載附件
2018-4-4 04:46 上傳
IMG_20180322_145513.jpg (304.48 KB, 下載次數(shù): 47)
下載附件
2018-4-4 04:46 上傳
stm8單片機(jī)源碼:
- /* Includes ------------------------------------------------------------------*/
- /* Includes ------------------------------------------------------------------*/
- #include "stm8s.h"
- #include "stm8s_clk.h"
- #include "intrinsics.h"
- #include "stm8s_uart1.h"
- #include "uart.h"
- #include "rc522.h"
- #include "string.h"
- #include "IWDG.h"
- #include "tim2.h"
- #include "1602i2c.h"
- void Delay(u32 nCount);
- extern u8 RxBuffer[RxBufferSize];
- extern u8 UART_RX_NUM;
- extern int time2;
- extern int tim2test;
- extern int tim2test1;
- unsigned char CT[2];//卡類型
- unsigned char SN[4]; //卡號
- unsigned char M1UID[8]={0x2e,0x55,0,0,0,0,0,0};//讀卡器向上發(fā)送,讀到卡后自動(dòng)發(fā)
- extern unsigned char controlflag;//接收到控制LED beep指令標(biāo)志 01表示收到
- extern unsigned char sendflag;//發(fā)送標(biāo)志 =1表示還未發(fā)送過 =0表示已經(jīng)發(fā)送一次 等待回復(fù) 如果超時(shí)重新發(fā)送
- unsigned char write[16] = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,0x10};
- unsigned char read[16] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
- extern unsigned char receive[8];
- u8 read_flag=0;//讀卡標(biāo)志位 1;讀到卡 0;還沒讀到卡
- u8 time_flag=0;//超時(shí)標(biāo)志 0;未超時(shí) 1:超時(shí)
- //unsigned char key[6] = {0x19,0x84,0x07,0x15,0x76,0x14};
- unsigned char key[6] = {0xff,0xff,0xff,0xff,0xff,0xff};
- int sum=0;
- //unsigned char key[6] = {0x00,0x00,0x00,0x00,0x00,0x00};
- /* Private macro -------------------------------------------------------------*/
- #define countof(a) (sizeof(a) / sizeof(*(a)))
- #define BufferSize (countof(Tx_Buffer)-1)
- /* Private variables ---------------------------------------------------------*/
- u8 Tx_Buffer[] = "請刷卡";
- u8 Rx_Buffer[BufferSize];
- u32 FLASH_ID ;
- /* Private defines -----------------------------------------------------------*/
- /* Private function prototypes -----------------------------------------------*/
- void cardNo2String(u8 *cardNo, u8 *str);
- /* Private functions ---------------------------------------------------------*/
- #define ReadWriteAddr 3 //讀寫扇區(qū)
- void SN_UID(void)
- {
- unsigned int tempUID;
- M1UID[2]=SN[0];
- M1UID[3]=SN[1];
- M1UID[4]=SN[2];
- M1UID[5]=SN[3];
- tempUID=M1UID[1]+M1UID[2]+M1UID[3]+M1UID[4]+M1UID[5];
- M1UID[6]=tempUID&0x00ff;
- M1UID[7]=tempUID>>8;
-
- }
- void rxfromPIC(void)
- {
-
- u8 i;
- sum=receive[1]+receive[2]+receive[3]+receive[4]+receive[5];
- if((receive[0]==0x7e)&&(receive[1]==0x55)&&(receive[6]==(sum&0x00ff))&&(receive[7]==(sum&0xff00)>>8))
- {
-
- read_flag=0;
- if(receive[2]==0x01)
- {
-
- }
- else if(receive[2]!=0x01)
- {
-
- }
- if(receive[3]==0x01)
- {
-
- }
- else if(receive[3]!=0x01)
- {
-
- }
-
- if(receive[4]==0x01)
- {
-
- }
- receive[0]=0x7e;
- UART1_SendString(receive, 8);
- for(i=2;i<8;i++)
- {
- M1UID[i]=0;
- }
- tim2test1=0;//啟動(dòng)計(jì)時(shí)
- }
-
- for(i=0;i<8;i++)
- {
- receive[i]=0;
- }
- if(tim2test1>5000)//超時(shí)全部關(guān)閉
- {
- tim2test1=0;
- // GPIO_HIGH(GPIOD, GPIO_PIN_3);
- // GPIO_HIGH(GPIOD, GPIO_PIN_7);
- // GPIO_LOW(GPIOD, GPIO_PIN_2);
- }
- }
- void main(void)
- {
- unsigned char status;
- /*設(shè)置內(nèi)部時(shí)鐘16M為主時(shí)鐘*/
- CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV2);
- status = memcmp(read,write,16);//清0
- InitRc522();//rc522初始化
- sendflag=1;
- I2C1602_Configuration();
- init1602_i2c();
- __enable_interrupt();//開啟總中斷
-
- //////////////
- write[0] =1;//
- write[1] =3;//
- write[2] =2;//
- write[3] =6;//
- write[4] =7;//
- write[5] =7;//
- write[6] =6;//
- write[7] =0;//
- write[8] =2;//
- write[9] =8;//
- write[10] =5;//
- ///////////////// 要寫入的值,最多16個(gè)///////
- while(1)
- {
- status = PcdRequest(PICC_REQALL,CT); // 掃描卡
- status= PcdAnticoll(SN);
- //防沖撞
- rxfromPIC();
- if (status==MI_OK)
- {
- SN_UID();
- status = PcdSelect(SN); //選擇要操作的卡
- if (status==MI_OK)
- {
- if( MI_OK == PcdAuthState(0x61,ReadWriteAddr,key,SN) ) //驗(yàn)證A密碼
- {
- // status = PcdWrite(2,write);//寫入M1卡第2塊塊數(shù)據(jù)
- status = PcdRead(2,read);//讀取M1卡第2塊塊數(shù)據(jù)
- if(MI_OK == status)
- {
- zl_i2c(0x80);
- sj_i2c(0x30+ read[0]);
- sj_i2c(0x30+ read[1]);
- sj_i2c(0x30+ read[2]);
- sj_i2c(0x30+ read[3]);
- sj_i2c(0x30+ read[4]);
- sj_i2c(0x30+ read[5]);
- sj_i2c(0x30+ read[6]);
- sj_i2c(0x30+ read[7]);
- sj_i2c(0x30+ read[8]);
- sj_i2c(0x30+ read[9]);
- sj_i2c(0x30+ read[10]);
- }
- }
- }
- }
-
-
- }
-
-
-
- }
- void Delay(u32 nCount)
- {
- /* Decrement nCount value */
- while (nCount != 0)
- {
- nCount--;
- }
- }
- void Hex2String(u8 hex,u8 *str)
- {
- str[0] = (hex / 100) + '0';
- str[1] = (hex % 100 / 10) + '0';
- str[2] = (hex % 10) + '0';
- }
- void cardNo2String(u8 *cardNo, u8 *str)
- {
- u8 Count = 0;
- for(Count = 0; Count < 4; Count++)
- {
- Hex2String(cardNo[Count], str + Count * 4);
- if(Count == 3)
- {
- str[15] = '\n';
- }
- else
- {
- str[Count * 4 + 3] = ':';
- }
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復(fù)制代碼
0.jpg (23.81 KB, 下載次數(shù): 49)
下載附件
2018-4-4 04:47 上傳
所有資料51hei提供下載:
第5周任務(wù)及代碼.rar
(15.54 MB, 下載次數(shù): 209)
2018-4-3 14:48 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
|
|