/* Nick Chernyy */ /* simple, three control line card reader */ /* not optimized, so read card slowly */ #include #include #include #include /* stolen from http://www.cse.cuhk.edu.hk/~khwong/ceg3430/mrobot5.pdf */ char getch() { char cin; _asm push ACC _endasm; _asm lcall 0x0032; _endasm; cin=ACC; _asm pop ACC _endasm; return(cin); } void putchar(char cout) { /* send '\n' for a new line and '\r' for a return */ ACC=cout; _asm lcall 0x0030; _endasm; } void phex(char i) { /* prints a 8 bit number as 2 digit hex */ ACC=i; _asm lcall 0x0034; _endasm; } void putstr(char *c) { int i = 0; while (c[i] != 0) { putchar(c[i]); i++; } } /* CLD2 -> P1.0 (goes low when card is present)*/ /* RCL2 -> P1.1 (goes low when clock is "high")*/ /* RDT2 -> P1.2 (low for data 1 and high for data 0)*/ void main (void) { /* to wait for hardware to initialize */ int i,j,l; int k[16]; char c, e[2]; char d[] = "magnetic card reader v10^-6 (nico@routed.net)\n\r\0"; putstr(d); P1 = 0xFF; while (1) { for (i = 0; i < 255; i++) { for (j = 0; j < 128; j++) { c = P1; } } j = 0; while (P1_0 == 1); /* we have a card! */ while (P1_0 == 0) { for (i = 0; i < 16; i++) { while ((P1_1 == 1) && (P1_0 == 0)); if (P1_2 == 0) { k[j]++; } k[j] = k[j] << 1; while ((P1_1 == 0) && (P1_0 == 0)); if (P1_0 == 1) { break; } } j++; } /* we read all of the bits, but they dont get shifted out right */ /* this is because we had to wait for serial io for each bit */ memcpy(e,&j,2); /* to print the number of bytes read */ phex(e[1]); phex(e[0]); putchar(':'); putchar('0'); putchar('x'); /* this next code will skip the leading zeros and the trailing zeros */ i = 0; while ((k[i] == 0) && (i < 16)) { i++; } j = i; l = 0; while (j < 16) { if (k[j] != 0) { l = j; } j++; } for (; i < l; i++ ) { memcpy(e, &k[i], 2); phex(e[1]); /* high byte */ phex(e[0]); /* low byte */ } putchar('\n'); putchar('\r'); for (i = 0; i < 16; i++) { k[i] = 0; /* dont want to include bzero */ } } }