rfid_x5551.c
来自「找的一个用U2270B开发的射频卡项目文件」· C语言 代码 · 共 194 行
C
194 行
// $Id: rfid_x5551.c,v 1.4 2007/01/24 21:44:15 tprescott Exp $
/*****************************************************
Project : rfid_x5551.c
Date : 9/08/2006
Author : Toby Prescott
Company : Atmel
Comments: AVR Studio GCC
Revisions:
v1.0 - Started written for CodeVision
v2.1 - 1/19/06 Changed from ASCII + stored
vars in EEPROM
v2.6 - Clean for WinAVR
*****************************************************/
#include "rfid_x5551.h"
//**** Configuration Block Hard Code ****//
unsigned char x5551_ConfigReg[4] EEMEM = {0xE6,0x08,0x80,0xE8};
unsigned int x5551_SGap EEMEM = 350;
unsigned int x5551_Gap EEMEM = 276;
unsigned int x5551_Zero EEMEM = 80;
unsigned int x5551_One EEMEM = 324;
unsigned int x5551_ShortL EEMEM = 50;
unsigned int x5551_ShortH EEMEM = 210;
unsigned int x5551_LongL EEMEM = 210;
unsigned int x5551_LongH EEMEM = 300;
unsigned int x5551_TermL EEMEM = 300;
unsigned int x5551_TermH EEMEM = 450;
//**** Load New values for Write Timings ****//
void x5551_Set_SGap(unsigned int iSGap){eeprom_write_word(&x5551_SGap,iSGap);}
void x5551_Set_Gap(unsigned int iGap){eeprom_write_word(&x5551_Gap,iGap);}
void x5551_Set_Zero(unsigned int iZero){eeprom_write_word(&x5551_Zero,iZero);}
void x5551_Set_One(unsigned int iOne){eeprom_write_word(&x5551_One,iOne);}
//**** Return Status of Write Timings ****//
unsigned int x5551_Get_SGap(void){return eeprom_read_word(&x5551_SGap);}
unsigned int x5551_Get_Gap(void){return eeprom_read_word(&x5551_Gap);}
unsigned int x5551_Get_Zero(void){return eeprom_read_word(&x5551_Zero);}
unsigned int x5551_Get_One(void){return eeprom_read_word(&x5551_One);}
//**** Load New values for Read Timings ****//
void x5551_Set_ShortL(unsigned int iShortL){eeprom_write_word(&x5551_ShortL,iShortL);}
void x5551_Set_ShortH(unsigned int iShortH){eeprom_write_word(&x5551_ShortH,iShortH);}
void x5551_Set_LongL(unsigned int iLongL){eeprom_write_word(&x5551_LongL,iLongL);}
void x5551_Set_LongH(unsigned int iLongH){eeprom_write_word(&x5551_LongH,iLongH);}
void x5551_Set_TerminatorL(unsigned int iTermL){eeprom_write_word(&x5551_TermL,iTermL);}
void x5551_Set_TerminatorH(unsigned int iTermH){eeprom_write_word(&x5551_TermH,iTermH);}
//**** Return Status of Read Timings ****//
unsigned int x5551_Get_ShortL(void){return eeprom_read_word(&x5551_ShortL);}
unsigned int x5551_Get_ShortH(void){return eeprom_read_word(&x5551_ShortH);}
unsigned int x5551_Get_LongL(void){return eeprom_read_word(&x5551_LongL);}
unsigned int x5551_Get_LongH(void){return eeprom_read_word(&x5551_LongH);}
unsigned int x5551_Get_TerminatorL(void){return eeprom_read_word(&x5551_TermL);}
unsigned int x5551_Get_TerminatorH(void){return eeprom_read_word(&x5551_TermH);}
// *******************************************************************************
// Pass in new Configuration Block data
// ******************************************************************************/
void x5551_Set_Config_Reg(unsigned char *cConfig)
{
unsigned char i;
for(i=0; i<4; i++) // Loop for all bytes in block
{
eeprom_write_byte(&x5551_ConfigReg[i],*cConfig++); // Save new byte
}
}
// *******************************************************************************
// Program the Configuration Block in the 5551 using specified Reader
// ******************************************************************************/
void x5551_WriteConfigReg(unsigned char Target_Reader)
{
unsigned char i,tempByte;
unsigned char writeBuff[5] = {0};
unsigned char cOpCode = 0x04;
writeBuff[0] = cOpCode<<5; // Load the Write Op Code into buffer
for(i=0; i<4; i++)
{
tempByte =eeprom_read_byte(&x5551_ConfigReg[i]); // Get the Configuration Block Data
writeBuff[i] |= (tempByte>>3); // Load into the buffer with shift for Op Code
writeBuff[(unsigned char)(i+1)] |= (tempByte<<5);
}
//writeBuff[4] |= 0x00<<2; // Load Address of Configuration Block in buffer
//Select Reader, Currently only supports U2270
if(Target_Reader == 70){U2270_PPCWriteBlock(38, &writeBuff[0]);} // Call Write Block Routine
Timer_delay_us(10000);
snd_Play(BEEPFREQ1,BEEPFREQ0,BEEPTIME); // return Audio feedback
}
// *******************************************************************************
// Program Block Data in the 5551 using specified Reader
// Pass in the block data and the block address
// ******************************************************************************/
unsigned char x5551_WriteBlock(unsigned char Target_Reader, unsigned char *inBuff, unsigned char inAddr, unsigned char confirm)
{
unsigned char i,tempByte;
unsigned char writeBuff[5] = {0};
unsigned char cOpCode = 0x04;
unsigned char *tmpInBuff;
unsigned char cError=1;
tmpInBuff = &(*inBuff); // Store beginning address pointer
writeBuff[0] = cOpCode<<5; // Load the Write Op Code into buffer
for(i=0; i<4; i++)
{
tempByte =*inBuff++; // Copy each byte from input data
writeBuff[i] |= (tempByte>>3); // Shift and load into the buffer
writeBuff[(unsigned char)(i+1)] |= (tempByte<<5);
}
writeBuff[4] |= inAddr<<2; // Load the correct block address into buffer
//Select Reader, Currently only supports U2270
if(Target_Reader == 70){U2270_PPCWriteBlock(38, &writeBuff[0]);} // Call Write Block Routine
Timer_delay_us(10000);
//IF confirm wanted check for successful write
if(confirm == 1){cError = x5551_ConfirmWrite(Target_Reader, tmpInBuff, inAddr);}
return cError;
}
// *******************************************************************************
// Program Entire Tag Data in the 55571 using specified Reader
// ******************************************************************************/
unsigned char x5551_WriteAll(unsigned char Target_Reader)
{
unsigned char block, i;
unsigned char tempBlock[4];
unsigned char cError=0;
// For Data Blocks 1-7
for(block=1; block<8; block++)
{
for(i=0; i<4; i++)
{
tempBlock[i]=Tag_Get_Block(block,i); //Read the Block Data from EEPROM
}
cError = x5551_WriteBlock(Target_Reader, &tempBlock[0], block, 1); // Call Block Write for Each Block
Timer_delay_us(10000);
if(cError == 0){block=9;}
}
return cError;
}
// *******************************************************************************
// Verify that the Block Data was correctly written
// Pass in the intended block data and the block address
// ******************************************************************************/
unsigned char x5551_ConfirmWrite(unsigned char Target_Reader, unsigned char *inBuff, unsigned char inAddr)
{
unsigned char i, k=0;
unsigned char tmpData;
unsigned char *rDatPtr;
unsigned char cError=0;
//Select Reader, Currently only supports U2270
if(Target_Reader == 70){cError = U2270_ReadData(51,&rDatPtr,MANCHESTER,X555XSYNC,224);} // Call Read Routine (All Blocks)
// IF valid Read
if(cError == 0)
{
rDatPtr = rDatPtr + (unsigned char)((inAddr-1)*4); // Move pointer to requested Block Address
for(i=0; i<4; i++)
{
tmpData = *inBuff++; // Copy attempted block Data byte
if(*rDatPtr++ == tmpData){k = 1;} // Check byte against byte read from tag
else // If data does not match
{
k=0; // Flag mismatch
i=5; // Exit check
}
}
}
if(k==1)
{
snd_Play(BEEPFREQ1,BEEPFREQ0,BEEPTIME); // IF all bytes match, return Audio feedback
return 1;
}
else
{
snd_Play(BUZZFREQ1,BUZZFREQ0,BUZZTIME);
return 0;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?