开发工具已经用了,用在磁条卡数据的读取上,虽然内存很小,但我也可以正确地读到了磁条卡的数据。。现发一下源代码,连线就不用了,看源文件里的定义就知道了。如果不明白的,可以直接EMAIL给我。
完全保证源文件可用!
附件怎么传的,我怎么没看到传附件的地方?
把代码贴在这里算了
.h 文件
//#include "mb95200.h"
#include "gpio.h"
/////////////////////////////////////////////////////////////////////////////////////////
// Define I/O port for SPCB006E
/////////////////////////////////////////////////////////////////////////////////////////
#define ioCLD PDR0_P06 // data signal valid
#define ioTrack2Dat PDR0_P01 // data input
#define ioTrack2Clk PDR0_P02 // clock input
/////////////////////////////////////////////////////////////////////////////////////////
// Define macro
/////////////////////////////////////////////////////////////////////////////////////////
#define HI 1
#define LO 0
#define TRUE 1
#define FALSE 0
union byte_bit_s {
unsigned char bb;
struct {
unsigned b7;
unsigned b6;
unsigned b5;
unsigned b4;
unsigned b3;
unsigned b2;
unsigned b1;
unsigned b0;
} bs;
};
typedef union byte_bit_s byte_bit_t;
/////////////////////////////////////////////////////////
// Track Buffer Size
/////////////////////////////////////////////////////////
#define READ_TRACK2BUFSIZE 60
/////////////////////////////////////////////////////////
// Track Start Code & End Code
/////////////////////////////////////////////////////////
#define READ_TRACK1_STARTCODE 0x45
#define READ_TRACK1_ENDCODE 0x7C
#define READ_TRACK23_STARTCODE 0x0B
#define READ_TRACK23_ENDCODE 0x1F
/////////////////////////////////////////////////////////
// Error Code
/////////////////////////////////////////////////////////
#define READ_LRCERROR 0xF1
#define READ_PARITYERROR 0xF2
#define READ_NOENDCODE 0xF3
#define READ_NOSTARTCODE 0xF4
#define READ_NODATA 0xF5
///////////////////////////////////////////////////////////
// Track Data Buffer
///////////////////////////////////////////////////////////
void ReadyToRead(void);
void Track2Check(void);
void Track2Decode(void);
extern unsigned char chTrack2Buffer[READ_TRACK2BUFSIZE];
extern unsigned char chTrack2Buf1; //Track3 Data Buffer1 for recieve data
extern unsigned char chTrack2Buf2; //Track3 Data Buffer2 for recieve data
extern unsigned char chTrack2Index; //Track3 Data Buffer index for recieve data
extern unsigned char chTrack2Counter; //Track3 Data bit counter for recieve data
extern unsigned char nReadTrack2Len;
#define fTrack2Start flag2.bs.b0 //Track3 Start recieve data , it be set by Track3 Receive
#define fTrack2Direction flag2.bs.b1 //Track3 Swipe direction: 1:Forward / 0:Reserve , it be set by Track3Interrupt
#define fTrack2HaveData flag2.bs.b2
#define fTrack2StartEnd flag2.bs.b3
#define ftemp flag2.bs.b4
#define fParity1 flag2.bs.b5
#define fParity2 flag2.bs.b6
.c 文件
#include "mcard.h"
///////////////////////////////////////////////////////////
// Track Data Buffer
///////////////////////////////////////////////////////////
unsigned char chTrack2Buffer[READ_TRACK2BUFSIZE];
unsigned char chTrack2Buf1; //Track3 Data Buffer1 for recieve data
unsigned char chTrack2Buf2; //Track3 Data Buffer2 for recieve data
unsigned char chTrack2Index; //Track3 Data Buffer index for recieve data
unsigned char chTrack2Counter; //Track3 Data bit counter for recieve data
unsigned char chTemp;
unsigned char nReadTrack2Len;
byte_bit_t flag2;
///////////////////////////////////////////////////////////
// Track Data Buffer Define
///////////////////////////////////////////////////////////
void ReadyToRead(void) {
chTrack2Buf1 = 0;
chTrack2Buf2 = 0;
chTrack2Index = 0;
chTrack2Counter = 0;
fTrack2Start = 0;
fTrack2StartEnd = 0;
fTrack2HaveData = 0;
fTrack2Direction = 0;
nReadTrack2Len = 0;
flag2.bb = 0;
}
///////////////////////////////////////////////////////////
// Track Data Decode
///////////////////////////////////////////////////////////
void Track2Decode(void) {
unsigned char i;
__DI();
if( (!gpio_get(ioTrack2Clk)) && (!gpio_get(ioCLD)) ) {
fTrack2HaveData = TRUE;
ftemp = !gpio_get(ioTrack2Dat);
if( fTrack2Start ) {
if( fTrack2Direction ) {
chTrack2Buf1 = chTrack2Buf1 >> 1;
if( ftemp ) {
chTrack2Buf1 = chTrack2Buf1 | 0x10;
}
}
else {
if( ftemp ) {
chTrack2Buf1 = chTrack2Buf1 | 0x01;
}
chTrack2Buf1 = chTrack2Buf1 << 1;
}
chTrack2Counter++;
if( chTrack2Counter == 5 ) {
chTrack2Counter = 0;
if( chTrack2Index < READ_TRACK2BUFSIZE - 1 ) {
chTrack2Buffer[chTrack2Index++] = chTrack2Buf1;
chTrack2Buf1 = 0x00;
}
else {
}
}
}
// waiting for start code or end code
else {
chTemp = chTrack2Buf1 & 0x01 ;
chTrack2Buf1 = chTrack2Buf1 >> 1;
if( ftemp ) {
chTrack2Buf1 = chTrack2Buf1 | 0x10;
}
chTrack2Buf2 = chTrack2Buf2 >> 1;
if( chTemp ) {
chTrack2Buf2 = chTrack2Buf2 | 0x10;
}
// check Track2 end code
if( ( chTrack2Buf1 & 0x1F ) == READ_TRACK23_ENDCODE ) {
// [ Start Code ] + [ End Code ] or [ End Code ] + [ Start Code ]
if( ( chTrack2Buf2 & 0x1F ) == READ_TRACK23_STARTCODE ) {
chTrack2Buffer[chTrack2Index++] = chTrack2Buf2;
chTrack2Buffer[chTrack2Index++] = chTrack2Buf1;
fTrack2Start = TRUE;
fTrack2Direction = TRUE;
fTrack2StartEnd = TRUE;
chTrack2Buf1 = 0x00;
}
// [ LRC ] + [ End Code ]
else {
// Check Parity
//ACC = chTrack2Buf2;
for(i = 0;i < 5; i++) {
if(chTrack2Buf2 & (1<<i)) fParity2 = !fParity2;
}
if( fParity2 ) {
chTrack2Buffer[chTrack2Index++] = chTrack2Buf2;
chTrack2Buffer[chTrack2Index++] = 0x1F;
fTrack2Start = TRUE;
fTrack2Direction = FALSE;
fTrack2StartEnd = FALSE;
chTrack2Buf1 = 0x00;
}
}
}
// check Track2 start code
// [ Start Code ] + [ Data ] + [ End Code ] + [ LRC ]
else {
if( ( chTrack2Buf2 & 0x1F ) == READ_TRACK23_STARTCODE ) {
// Check Parity
//ACC = chTrack2Buf1;
for(i = 0;i < 5; i++) {
if(chTrack2Buf1 & (1<<i)) fParity1 = !fParity1;
}
if( fParity1 ) {
chTrack2Buffer[chTrack2Index++] = chTrack2Buf2;
chTrack2Buffer[chTrack2Index++] = chTrack2Buf1;
fTrack2Start = TRUE;
fTrack2Direction = TRUE;
fTrack2StartEnd = FALSE;
chTrack2Buf1 = 0x00;
}
}
}
}
while(!gpio_get(ioTrack2Clk)) {} // wait end low level
}
__EI();
}
///////////////////////////////////////////////////////////
// Track Data Check
///////////////////////////////////////////////////////////
void Track2Check(void) {
int i = 0,j = 0,k = 0,nTemp = 0,m = 0;
for(i = chTrack2Index; i > 1; i--) {
if( chTrack2Buffer[i] != 0x00 )
break;
}
if( i < 2 ) {
nReadTrack2Len = READ_NODATA;
return;
}
nReadTrack2Len = i+1;
// [ Start Code ] + [ End Code ] + [ LRC ]
// or [ LRC ] = [ Start Code ]
if( fTrack2StartEnd && ( nReadTrack2Len > 3 ) ) {
// LRC
for(i = 0; i < 5; i++) {
nTemp = nTemp << 1;
if( ( chTrack2Buffer[0] >> i ) & 0x01 )
nTemp++;
}
chTrack2Buffer[0] = nTemp;
for(i = 2; i < nReadTrack2Len; i++) {
nTemp = 0;
for(j = 0;j < 5; j++) {
nTemp = nTemp << 1;
if( ( chTrack2Buffer[i] >> j ) & 0x01 )
nTemp++;
}
chTrack2Buffer[i] = nTemp;
}
j = nReadTrack2Len >> 1;
k = nReadTrack2Len - 1;
for(i = 0;i < j; i++) {
nTemp = chTrack2Buffer[i];
chTrack2Buffer[i] = chTrack2Buffer[k];
chTrack2Buffer[k] = nTemp;
k--;
}
}
// Reverse
// [ LRC ] + [ End Code ] + [ Data ] + [ Start Code ]
if( !fTrack2Direction ) {
// LRC Process
for(i = 0; i < 5; i++) {
nTemp = nTemp << 1;
if( ( chTrack2Buffer[0] >> i ) & 0x01 )
nTemp++;
}
chTrack2Buffer[0] = nTemp;
// Data Process
for(i = 2; i < nReadTrack2Len; i++) {
chTrack2Buffer[i] = chTrack2Buffer[i] >> 1;
}
j = nReadTrack2Len>>1;
k = nReadTrack2Len-1;
for(i = 0; i < j; i++) {
nTemp = chTrack2Buffer[i];
chTrack2Buffer[i] = chTrack2Buffer[k];
chTrack2Buffer[k] = nTemp;
k--;
}
for(j = 0; j < 2; j++) {
// Check Start Code ( Error Code = 0xF4 )
if( chTrack2Buffer[0] != READ_TRACK23_STARTCODE ) {
for(i=nReadTrack2Len-1;i>0;i--) {
nTemp = ( chTrack2Buffer[i-1] >> 3 ) & 0x03;
chTrack2Buffer[i] = ( ( chTrack2Buffer[i] << 2 ) + nTemp ) & 0x1F;
}
chTrack2Buffer[0] = ( chTrack2Buffer[0] << 2 ) & 0x1F;
if( chTrack2Buffer[0] == 0x00 ) {
for(i=0;i<nReadTrack2Len;i++)
chTrack2Buffer[i] = chTrack2Buffer[i+1];
nReadTrack2Len--;
}
}
}
}
// Check Start Code ( Error Code = 0xF4 )
if( chTrack2Buffer[0] != READ_TRACK23_STARTCODE ) {
nReadTrack2Len = READ_NOSTARTCODE;
return;
}
// Check End Code ( Error Code = 0xF3 )
if( chTrack2Buffer[nReadTrack2Len-2] != READ_TRACK23_ENDCODE ) {
nReadTrack2Len = READ_NOENDCODE;
return;
}
// Check Parity ( Error Code = 0xF2 )
for(i = 0;i < nReadTrack2Len; i++) {
//nTemp = chTrack2Buffer[i];
//ACC = nTemp;
for(m = 0;m < 5; m++) {
if(chTrack2Buffer[i] & (1<<m)) fParity1 = !fParity1;
}
if( !fParity1 ) {
nReadTrack2Len = READ_PARITYERROR;
return;
}
}
// Check LRC ( Error Code = 0xF1 )
nTemp = chTrack2Buffer[0];
for(i = 1; i < nReadTrack2Len - 1; i++) {
nTemp = nTemp ^ chTrack2Buffer[i];
}
nTemp = nTemp & 0x0F;
if( nTemp != ( chTrack2Buffer[nReadTrack2Len-1] & 0x0F ) ) {
nReadTrack2Len = READ_LRCERROR;
return;
}
// Data Process
for(i = 0; i < nReadTrack2Len - 1; i++) {
chTrack2Buffer[i] = ( chTrack2Buffer[i] & 0x0F ) + 0x30;
}
nReadTrack2Len--;
}
///////////////////////////////////////////////////////////
// Usage
///////////////////////////////////////////////////////////
/*
void Init (void) {
DDR0_P06 = 0; //
ioCLD = 0; //
DDR0_P01 = 0; //
ioTrack2Dat = 0; //
DDR0_P02 = 0; //
ioTrack2Clk = 0; //
}
void main (void) {
Init (); // Config Hardware
ReadyToRead(); // Clear Buffers
while(1) {
// .....mcard start.....
if( !ioCLD ) { // "!ioCLD" replaceed with "!gpio_get(ioCLD)" If Signal Input
while( !ioCLD ) { // "!ioCLD" replaceed with "!gpio_get(ioCLD)"
Track2Decode();
// clrwdt();
} // Wait Signal End
if( fTrack2HaveData ) {
Track2Check();
// clrwdt();
// out_msg(); // print card message here
}
else {
nReadTrack2Len = READ_NODATA;
}
ReadyToRead(); // Clear Buffers For Next Time
}
// ....mcare end......
}
}
*/