Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7723293
  • 博文数量: 961
  • 博客积分: 15795
  • 博客等级: 上将
  • 技术积分: 16612
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-07 14:23
文章分类

全部博文(961)

文章存档

2016年(1)

2015年(61)

2014年(41)

2013年(51)

2012年(235)

2011年(391)

2010年(181)

分类: 嵌入式

2012-10-31 15:38:09

基于STC89C52+21006541的磁卡读取数据源码, 是STC系列,AT系列的没有XDATA ,,烧写进去运行不了别怨我, 我用的是上海玫格泰科技发展有限公司生产的磁条卡刷卡器 (90mm&43mm)

点击(此处)折叠或打开

  1. #include <reg52.h>
  2. #define uchar unsigned char
  3. #define uint unsigned int

  4. sbit b=P0^4;
  5. sbit c=P0^3;
  6. uchar z, count;
  7. uchar String[20];
  8. //xdata uchar ryubCardMsg[MSR_ASIC_PREAMBLE_AND_ALL_3_TKACKS_LEN_IN_BYTES];

  9. // Track A - 76 characters, 7 bits per alphanumberic character including parity.
  10. char char7bit[64] =
  11.            " !'#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_";
  12. //磁道B译码磁道B的编码和磁道A相似,只是采用了5位(4位数据和1位奇偶校验位)编码,而不是7位编码。
  13. //磁道B的字符集只含有数字字符和符号,如下面的char5bit[16]字符阵列所示。 0123456789012345
  14. char char5bit[16] = "0123456789:;<=>?";
  15. // Generate a long delay for card reset and read intervals.
  16. void longDelay()
  17. {
  18.    uint j, i=5;
  19.     while(i--)
  20.     {
  21.         for(j = 0; j < 5000; j++);
  22.      }
  23. }
  24. // Generate a shorter delay (used between STROBE/DATA transitions).
  25. void delay()
  26. {
  27.    uint i;

  28.    for (i = 1; i < 1000; i++) {
  29.       ;
  30.    }
  31. }

  32. // Release the DATA line (P0.0) and allow it to float high.    
  33. void dataHigh()
  34. {
  35.    P0 |= 0x01;
  36.    delay();
  37. }

  38. // Drive the DATA line (P0.0) low.    
  39. void dataLow()
  40. {
  41.    P0 &= 0xFE;
  42.    delay();
  43. }
  44.                 
  45. // Release the STROBE line (P0.1) and allow it to float high.

  46. void strobeHigh()
  47. {
  48.    P0 |= 0x02;
  49.    delay();
  50. }

  51. // Drive the STROBE line (P0.1) low.

  52. void strobeLow()
  53. {
  54.    P0 &= 0xFD;
  55.    delay();
  56. }

  57. void resetCardReader()
  58. {
  59.    dataHigh();
  60.    strobeHigh();
  61.    longDelay();

  62.    dataLow(); // Force DATA low.
  63.    longDelay();
  64.    strobeLow(); // Drive STROBE low, then high again.
  65.    strobeHigh();
  66.    strobeLow(); // Drive STROBE low, then release DATA.
  67.    dataHigh();
  68.    longDelay();

  69.    strobeHigh(); // Drive STROBE low and high again two more times
  70.    strobeLow(); // to complete the reset and leave the card reader
  71.    strobeHigh(); // in the ready state, prepared to scan a card.
  72.    strobeLow();
  73. }


  74. // Wait for the DATA line to be driven low by the card reader.    
  75. void waitForDataLow()
  76. {
  77.    int i = 0xFF;

  78.    dataHigh(); // Make sure that DATA is floating high.
  79.      c=1;
  80.    longDelay();
  81.      c=0;
  82.    longDelay();
  83.      c=1;
  84.    longDelay();
  85.      c=0;
  86.    longDelay();
  87.    while ((i & 1) == 1) {
  88.       i = P0;
  89.    }
  90. }


  91.  // Clock a single bit value out of the card reader by driving STROBE high,
  92. // then low, and reading the DATA line.

  93. int readBit()
  94. {
  95.    int i;

  96.    strobeHigh(); // Drive STROBE high.
  97.    strobeLow(); // Drive STROBE low (DATA line now contains bit).

  98.    i = P0;
  99.    if ((i & 1) == 0) {
  100.       return 1; // Low on DATA line indicates a 1 bit.
  101.    } else {
  102.       return 0; // High on DATA line indicates a 0 bit.
  103.    }
  104. }

  105. //译码A***********************************************
  106. // Clock out and decode a 7-bit character from the track memory, returning the
  107. // character value. 7-bit (alphanumeric) characters are found on Track A only.

  108. char read7BitChar()
  109. {
  110.    int i, c;

  111.    // Each character is composed of 7 bits, which we clock out of the track memory
  112.    // beginning with the least significant bit. Bit 7 is parity, which is ignored.

  113.    c = 0;
  114.    for (i = 1; i < 128; i *= 2) {
  115.       c |= (readBit() * i);
  116.    }
  117.    c &= 0x3F;

  118.    return char7bit[c]; // Decode/return the character using the 7-bit table.
  119. }
  120. // Clock out and decode a 5-bit character from the track memory, returning the
  121. // character value. 5-bit (numeric+symbol) characters are found on Tracks B and C.

  122. char read5BitChar()
  123. {
  124.    int i, c;

  125.    // Each character is composed of 5 bits, which we clock out of the track memory
  126.    // beginning with the least significant bit. Bit 5 is parity, which is ignored.

  127.    c = 0;
  128.    for (i = 1; i < 32; i *= 2) {
  129.       c |= (readBit() * i);
  130.    }
  131.    c &= 0x0F;

  132.    return char5bit[c]; // Decode/return the character using the 5-bit table.
  133. }

  134. void UART_init()
  135. {     
  136.     TL1 = (65536-50000)%256; // 得到定时器寄存器低位
  137.     TH1 = (65536-50000)/256; // 得到定时器寄存器高位
  138.      //初始化串行口和波特率发生器
  139.     SCON =0x50; //选择串口工作方式1,打开接收允许
  140.     TMOD =0x20; //定时器1工作在方式2,定时器0工作在方式1
  141.     TH1 =0xfA; //实现波特率9600(系统时钟11.0592MHZ)
  142.     PCON = 0x80;
  143.     TR1 =1; //启动定时器T1
  144.     ET1 =1;
  145.     ES=1; //允许串行口中断
  146.     PS=1; //设计串行口中断优先级
  147.     EA =1; //单片机中断允许
  148. }
  149. //*****************************************************


  150. //**************串口发送字符函数*************
  151. uchar SendOneByte(uchar unchByte)
  152. {
  153.     SBUF=unchByte;
  154.     while(!TI){}
  155.     TI=0;
  156. }
  157. //*****************************************************

  158. //**************串口发送字符串函数*************
  159. uchar UartPrint(uchar len,uchar *string)
  160. {
  161.     for(;len>0;len--) SendOneByte(*string++);
  162.     SendOneByte(0x0d);
  163. }
  164. //*****************************************************


  165.  void main()
  166. {
  167.     
  168.    uint i;
  169.    P0=0X00;
  170.    UART_init();         
  171.    
  172.    while(1)    
  173.    {
  174.      resetCardReader();
  175.      UartPrint(24,"Waiting for card swipe :");
  176.      waitForDataLow(); // DATA low indicates that card swipe has begun.
  177.           b=1;
  178.      longDelay();
  179.      b=0;
  180.      longDelay();
  181.      b=1;
  182.      longDelay();
  183.      b=0;
  184.      longDelay();
  185.      strobeHigh();
  186.      longDelay();
  187.      strobeLow();
  188.      longDelay();
  189.      waitForDataLow(); // DATA low indicates that card swipe is complete.
  190.      UartPrint(14,"Track1 Data = ");
  191.      //if( readBit() & 0x01 == 1 )
  192.      {
  193.          for (i = 0; i < 16; i++)
  194.          {
  195.          readBit();
  196.             
  197.          }
  198.          for (i = 0; i < 76; i++) {
  199.          SendOneByte(read7BitChar());    
  200.          }
  201.          // At this point, we have read 532 bits of the 704-bit Track A memory on the
  202.          // card reader IC. Flush the remaining 172 bits.
  203.         
  204.          for (i = 0; i < 172; i++) {
  205.          readBit();
  206.             
  207.          }
  208.         
  209.          // Track B - 37 characters, 5 bits per numeric/symbol character including parity.
  210.         
  211.          UartPrint(14,"Track2 Data = ");
  212.         
  213.          for (i = 0; i < 37; i++) {
  214.          SendOneByte(read5BitChar());
  215.          }
  216.          for (i = 0; i < 519; i++) {     //读取剩下的位编码的数据[ 704 -(37*5)] = 519bits
  217.              readBit();
  218.              }
  219.         
  220.         
  221.          UartPrint(14,"Track3 Data = ");
  222.              for (i = 0; i < 104; i++) {
  223.              SendOneByte(read5BitChar());
  224.              }
  225.         
  226.          for (i = 0; i < 184; i++) {     //读取剩下的位编码的数据[ 704 -(104*5)] = 184bits
  227.          readBit();
  228.         
  229.                 }
  230.         }    
  231.    }
  232. }
  233. /*
  234. void timer0() interrupt 1
  235. {
  236.   // 定时50ms,要知道51单片机最长只能定时65.536ms
  237.   TL0 = (65536-50000)%256; // 得到定时器寄存器低位
  238.   TH0 = (65536-50000)/256; // 得到定时器寄存器高位
  239.   count++;
  240.   if(count == 2) //两次即100ms
  241.   {     count = 0;
  242.     MSR_ub100msTickDownCntTimeToReceiveBufferReadySignal--;
  243.   }
  244. }
  245.     */
  246. //函数串口接收中断处理函数
  247. void com_interrupt(void) interrupt 4 using 3
  248. {
  249.   unsigned char RECEIVR_buffer;
  250.   bit flag1=1;
  251.   if(RI)                     //处理接收中断     查询接收标志位(有数据发送过来时置为1)
  252.   {    
  253.     RI=0;                    //清除中断标志位
  254.     RECEIVR_buffer=SBUF; //接受串口数据
  255.     String[z]=SBUF;            

  256.     if(RECEIVR_buffer=='$')
  257.         { char i=0;
  258.          ES=0;
  259.          String[z]='\0';
  260.          SCON =0X40; //接受不允许
  261.          for(i=0;i!='$';i++)
  262.          {
  263.          //SendOneByte( ryubCardMsg[ Sendbyte ] );
  264.          SendOneByte( String[ i ] );
  265.          }
  266.          //COM_send();//发送数据
  267.          ES=1;
  268.          z=0;
  269.          flag1=0;
  270.          SCON=0X50; //接收允许
  271.         }    
  272.     if(flag1)
  273.     z++;
  274.   }
  275. }
源码: 21006541读取磁卡数据.zip   
阅读(4270) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

fengshao13702014-09-29 11:51:41

问下,楼主,P0.3和P0.4是干嘛的?