Hola amigos de todo pic , estoy realizando un proyecto que captura los datos de tarjetas magneticas por el protocolo de los teclados y que lo muestre por el USART RS232
Tengo entendido que los lectores de tarjetas son identicos a los teclados osea funcionan bajo el mismo concepto , si conectan esto a un teclado funciona perfecto el data en el pin 19 y el clock en el pin 17, pero si lo conectan al lector muestra los datos de forma desordenada y si pasan denuevo la tarjeta muestra los datos desordenados , estoy usando un cristal de 20Mhz.Si me pudieran dar una mano con esto porfavor! se los agradeceria mucho
#include <p18f452.h>
#include <capture.h>
#include <timers.h>
#include <delays.h>
#include <usart.h>
#pragma config OSC = HS
#pragma config WDT = OFF
#pragma config LVP = OFF
void ConfiguraUSART(void);
void ProcesaScanCode(unsigned char scancode);
void InterruptHandlerHigh (void);
static unsigned char htoaH(unsigned char dato);
static unsigned char htoaL(unsigned char dato);
//unsigned char z;
unsigned char bit_counter=0,i=0,hex=0;
unsigned char x[11];
unsigned char scancode=0x00;
unsigned int current_scan_code = 0;
unsigned int scan_code_buf_cnt = 0;
unsigned int scan_code_buf[8];
unsigned int buf_ready = 0;
//******************************
//INTERRUPT CONTROL
#pragma code InterruptVectorHigh = 0x08
void InterruptVectorHigh (void)
{
_asm
goto InterruptHandlerHigh
_endasm
}
#pragma code
#pragma interrupt InterruptHandlerHigh
void InterruptHandlerHigh(void) {
if(PIR1bits.CCP1IF){
if(bit_counter < 10){
x[bit_counter]=PORTDbits.RD0;
bit_counter++;
}
else if(bit_counter == 10){
scancode=x[8]*128+x[7]*64+x[6]*32+x[5]*16+x[4]*8+x[3]*4+x[2]*2+x[1]*1;
while(BusyUSART());
putcUSART( '*' );
//el siguiente par de while's muestran el scancode convertido a hexa
while(BusyUSART());
putcUSART( htoaH(scancode) );
while(BusyUSART());
putcUSART( htoaL(scancode) );
while(BusyUSART());
putcUSART( ' ' );
//el siguiente for, muestra la trama en binario (los 11 bits)
for(i=0x00;i<0x0B;i++){
while(BusyUSART());
putcUSART(x+0x30);
}
while(BusyUSART());
putcUSART( ' ' );
ProcesaScanCode(scancode);
while(BusyUSART());
putrsUSART( "\r\n" );
//reinicio el contador de la trama
bit_counter=0;
}
WriteTimer1( 0x0000 );
PIR1bits.CCP1IF = 0;
}
/* else if(PIR1bits.TMR1IF){
bit_counter = 0;
PIR1bits.TMR1IF = 0;
}
INTCONbits.GIE = 1;*/
}
//****************************************************
//Funciones para convertir un unsigned char a su equivalente hexa
//ejemplo: A=0x41
//htoaH('A')='4'
//htoaL('A')='1'
static unsigned char htoaH(unsigned char dato){
hex=dato>>4;
if(hex>0x09){
return hex+'A'-10;
}
else{
return hex+'0';
}
}
static unsigned char htoaL(unsigned char dato){
hex=dato&=0x0F;
if(hex>0x09){
return hex+'A'-10;
}
else{
return hex+'0';
}
}
//***************************
void main(void){
ConfiguraUSART();
TRISC = 0b10111111;
TRISD = 0b11111111;
//Delay10KTCYx(10);
INTCON = 0b11000000;
OpenCapture1( C1_EVERY_FALL_EDGE & CAPTURE_INT_ON );
OpenTimer1( TIMER_INT_ON & T1_SOURCE_INT & T1_PS_1_1 & T1_16BIT_RW );
//WriteTimer1( 0x0000 );
while(1){
//ProcesaScanCode();
}
}
//***************************
void ConfiguraUSART(void){
TRISC = 0b1011111 ; // bit 7 input 6 output
OpenUSART(USART_TX_INT_OFF & USART_RX_INT_OFF &
USART_ASYNCH_MODE & USART_EIGHT_BIT &
USART_CONT_RX & USART_BRGH_HIGH, 131);
while(BusyUSART());
putrsUSART("USART LISTO \r\n");
}
void ProcesaScanCode(unsigned char scancode){
switch(scancode){
case 0x05 : while(BusyUSART());
putrsUSART( "%" );
break;
case 0x1C : while(BusyUSART());
putrsUSART( "a" );
break;
case 0x32 : while(BusyUSART());
putrsUSART( "b" );
break;
case 0x21 : while(BusyUSART());
putrsUSART( "c" );
break;
case 0x23 : while(BusyUSART());
putrsUSART( "d" );
break;
case 0x24 : while(BusyUSART());
putrsUSART( "e" );
break;
case 0x2B : while(BusyUSART());
putrsUSART( "f" );
break;
case 0x34 : while(BusyUSART());
putrsUSART( "g" );
break;
case 0x33 : while(BusyUSART());
putrsUSART( "h" );
break;
case 0x43 : while(BusyUSART());
putrsUSART( "i" );
break;
case 0x3B : while(BusyUSART());
putrsUSART( "j" );
break;
case 0x42 : while(BusyUSART());
putrsUSART( "k" );
break;
case 0x4B : while(BusyUSART());
putrsUSART( "l" );
break;
case 0x3A : while(BusyUSART());
putrsUSART( "m" );
break;
case 0x31 : while(BusyUSART());
putrsUSART( "n" );
break;
case 0x44 : while(BusyUSART());
putrsUSART( "o" );
break;
case 0x4D : while(BusyUSART());
putrsUSART( "p" );
break;
case 0x15 : while(BusyUSART());
putrsUSART( "q" );
break;
case 0x2D : while(BusyUSART());
putrsUSART( "r" );
break;
case 0x1B : while(BusyUSART());
putrsUSART( "s" );
break;
case 0x2C : while(BusyUSART());
putrsUSART( "t" );
break;
case 0x3C : while(BusyUSART());
putrsUSART( "u" );
break;
case 0x2A : while(BusyUSART());
putrsUSART( "v" );
break;
case 0x1D : while(BusyUSART());
putrsUSART( "w" );
break;
case 0x22 : while(BusyUSART());
putrsUSART( "x" );
break;
case 0x35 : while(BusyUSART());
putrsUSART( "y" );
break;
case 0x1A : while(BusyUSART());
putrsUSART( "z" );
break;
case 0x16 : while(BusyUSART());
putrsUSART( "1" );
break;
case 0x1E : while(BusyUSART());
putrsUSART( "2" );
break;
case 0x26 : while(BusyUSART());
putrsUSART( "3" );
break;
case 0x25 : while(BusyUSART());
putrsUSART( "4" );
break;
case 0x2E : while(BusyUSART());
putrsUSART( "5" );
break;
case 0x36 : while(BusyUSART());
putrsUSART( "6" );
break;
case 0x3D : while(BusyUSART());
putrsUSART( "7" );
break;
case 0x3E : while(BusyUSART());
putrsUSART( "8" );
break;
case 0x46 : while(BusyUSART());
putrsUSART( "9" );
break;
case 0x45 : while(BusyUSART());
putrsUSART( "0" );
break;
default :
break;
}
}