点击(此处)折叠或打开
- #include <reg52.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit b=P0^4;
- sbit c=P0^3;
- uchar z, count;
- uchar String[20];
- //xdata uchar ryubCardMsg[MSR_ASIC_PREAMBLE_AND_ALL_3_TKACKS_LEN_IN_BYTES];
- // Track A - 76 characters, 7 bits per alphanumberic character including parity.
- char char7bit[64] =
- " !'#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_";
- //磁道B译码磁道B的编码和磁道A相似,只是采用了5位(4位数据和1位奇偶校验位)编码,而不是7位编码。
- //磁道B的字符集只含有数字字符和符号,如下面的char5bit[16]字符阵列所示。 0123456789012345
- char char5bit[16] = "0123456789:;<=>?";
- // Generate a long delay for card reset and read intervals.
- void longDelay()
- {
- uint j, i=5;
- while(i--)
- {
- for(j = 0; j < 5000; j++);
- }
- }
- // Generate a shorter delay (used between STROBE/DATA transitions).
- void delay()
- {
- uint i;
- for (i = 1; i < 1000; i++) {
- ;
- }
- }
- // Release the DATA line (P0.0) and allow it to float high.
- void dataHigh()
- {
- P0 |= 0x01;
- delay();
- }
- // Drive the DATA line (P0.0) low.
- void dataLow()
- {
- P0 &= 0xFE;
- delay();
- }
-
- // Release the STROBE line (P0.1) and allow it to float high.
- void strobeHigh()
- {
- P0 |= 0x02;
- delay();
- }
- // Drive the STROBE line (P0.1) low.
- void strobeLow()
- {
- P0 &= 0xFD;
- delay();
- }
- void resetCardReader()
- {
- dataHigh();
- strobeHigh();
- longDelay();
- dataLow(); // Force DATA low.
- longDelay();
- strobeLow(); // Drive STROBE low, then high again.
- strobeHigh();
- strobeLow(); // Drive STROBE low, then release DATA.
- dataHigh();
- longDelay();
- strobeHigh(); // Drive STROBE low and high again two more times
- strobeLow(); // to complete the reset and leave the card reader
- strobeHigh(); // in the ready state, prepared to scan a card.
- strobeLow();
- }
- // Wait for the DATA line to be driven low by the card reader.
- void waitForDataLow()
- {
- int i = 0xFF;
- dataHigh(); // Make sure that DATA is floating high.
- c=1;
- longDelay();
- c=0;
- longDelay();
- c=1;
- longDelay();
- c=0;
- longDelay();
- while ((i & 1) == 1) {
- i = P0;
- }
- }
- // Clock a single bit value out of the card reader by driving STROBE high,
- // then low, and reading the DATA line.
- int readBit()
- {
- int i;
- strobeHigh(); // Drive STROBE high.
- strobeLow(); // Drive STROBE low (DATA line now contains bit).
- i = P0;
- if ((i & 1) == 0) {
- return 1; // Low on DATA line indicates a 1 bit.
- } else {
- return 0; // High on DATA line indicates a 0 bit.
- }
- }
- //译码A***********************************************
- // Clock out and decode a 7-bit character from the track memory, returning the
- // character value. 7-bit (alphanumeric) characters are found on Track A only.
- char read7BitChar()
- {
- int i, c;
- // Each character is composed of 7 bits, which we clock out of the track memory
- // beginning with the least significant bit. Bit 7 is parity, which is ignored.
- c = 0;
- for (i = 1; i < 128; i *= 2) {
- c |= (readBit() * i);
- }
- c &= 0x3F;
- return char7bit[c]; // Decode/return the character using the 7-bit table.
- }
- // Clock out and decode a 5-bit character from the track memory, returning the
- // character value. 5-bit (numeric+symbol) characters are found on Tracks B and C.
- char read5BitChar()
- {
- int i, c;
- // Each character is composed of 5 bits, which we clock out of the track memory
- // beginning with the least significant bit. Bit 5 is parity, which is ignored.
- c = 0;
- for (i = 1; i < 32; i *= 2) {
- c |= (readBit() * i);
- }
- c &= 0x0F;
- return char5bit[c]; // Decode/return the character using the 5-bit table.
- }
- void UART_init()
- {
- TL1 = (65536-50000)%256; // 得到定时器寄存器低位
- TH1 = (65536-50000)/256; // 得到定时器寄存器高位
- //初始化串行口和波特率发生器
- SCON =0x50; //选择串口工作方式1,打开接收允许
- TMOD =0x20; //定时器1工作在方式2,定时器0工作在方式1
- TH1 =0xfA; //实现波特率9600(系统时钟11.0592MHZ)
- PCON = 0x80;
- TR1 =1; //启动定时器T1
- ET1 =1;
- ES=1; //允许串行口中断
- PS=1; //设计串行口中断优先级
- EA =1; //单片机中断允许
- }
- //*****************************************************
- //**************串口发送字符函数*************
- uchar SendOneByte(uchar unchByte)
- {
- SBUF=unchByte;
- while(!TI){}
- TI=0;
- }
- //*****************************************************
- //**************串口发送字符串函数*************
- uchar UartPrint(uchar len,uchar *string)
- {
- for(;len>0;len--) SendOneByte(*string++);
- SendOneByte(0x0d);
- }
- //*****************************************************
- void main()
- {
-
- uint i;
- P0=0X00;
- UART_init();
-
- while(1)
- {
- resetCardReader();
- UartPrint(24,"Waiting for card swipe :");
- waitForDataLow(); // DATA low indicates that card swipe has begun.
- b=1;
- longDelay();
- b=0;
- longDelay();
- b=1;
- longDelay();
- b=0;
- longDelay();
- strobeHigh();
- longDelay();
- strobeLow();
- longDelay();
- waitForDataLow(); // DATA low indicates that card swipe is complete.
- UartPrint(14,"Track1 Data = ");
- //if( readBit() & 0x01 == 1 )
- {
- for (i = 0; i < 16; i++)
- {
- readBit();
-
- }
- for (i = 0; i < 76; i++) {
- SendOneByte(read7BitChar());
- }
- // At this point, we have read 532 bits of the 704-bit Track A memory on the
- // card reader IC. Flush the remaining 172 bits.
-
- for (i = 0; i < 172; i++) {
- readBit();
-
- }
-
- // Track B - 37 characters, 5 bits per numeric/symbol character including parity.
-
- UartPrint(14,"Track2 Data = ");
-
- for (i = 0; i < 37; i++) {
- SendOneByte(read5BitChar());
- }
- for (i = 0; i < 519; i++) { //读取剩下的位编码的数据[ 704 -(37*5)] = 519bits
- readBit();
- }
-
-
- UartPrint(14,"Track3 Data = ");
- for (i = 0; i < 104; i++) {
- SendOneByte(read5BitChar());
- }
-
- for (i = 0; i < 184; i++) { //读取剩下的位编码的数据[ 704 -(104*5)] = 184bits
- readBit();
-
- }
- }
- }
- }
- /*
- void timer0() interrupt 1
- {
- // 定时50ms,要知道51单片机最长只能定时65.536ms
- TL0 = (65536-50000)%256; // 得到定时器寄存器低位
- TH0 = (65536-50000)/256; // 得到定时器寄存器高位
- count++;
- if(count == 2) //两次即100ms
- { count = 0;
- MSR_ub100msTickDownCntTimeToReceiveBufferReadySignal--;
- }
- }
- */
- //函数串口接收中断处理函数
- void com_interrupt(void) interrupt 4 using 3
- {
- unsigned char RECEIVR_buffer;
- bit flag1=1;
- if(RI) //处理接收中断 查询接收标志位(有数据发送过来时置为1)
- {
- RI=0; //清除中断标志位
- RECEIVR_buffer=SBUF; //接受串口数据
- String[z]=SBUF;
- if(RECEIVR_buffer=='$')
- { char i=0;
- ES=0;
- String[z]='\0';
- SCON =0X40; //接受不允许
- for(i=0;i!='$';i++)
- {
- //SendOneByte( ryubCardMsg[ Sendbyte ] );
- SendOneByte( String[ i ] );
- }
- //COM_send();//发送数据
- ES=1;
- z=0;
- flag1=0;
- SCON=0X50; //接收允许
- }
- if(flag1)
- z++;
- }
- }