Y ESTE ES EL CODIGO DEL MAESTRO
#include <18F452.h>
#device adc=8
#use delay(clock=18432000)
#fuses NOWDT,WDT128,HS, NOPROTECT, NOOSCSEN, BROWNOUT, BORV20, NOPUT, STVREN, NODEBUG, LVP, NOWRT, NOWRTD, NOWRTB, NOWRTC, NOCPD, NOCPB, NOEBTR, NOEBTRB
#use rs232(baud=115200,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#define LEN_BUFF 397
#define Active 1
#define T_SERIAL 5
#define TICKS 60
#include <string.h>
BYTE buffer[400] ;
BYTE bprint[200] ;
BYTE cstring[50] ;
BYTE crlf[3] ;
int8 wserial ;
int8 xstate ;
int16 nrx ;
int16 ntx ;
int8 ni ;
int8 xcmd ;
int8 xptr ;
int8 xact ;
int8 bptr;
int8 nporta[8] ;
int8 nportb[8] ;
int8 nportc[8] ;
int8 nportd[8] ;
int8 nporte[8] ;
int8 nportf[8] ;
int8 nportg[8] ;
int8 nporth[8] ;
int8 nporti[8] ;
int8 nportj[8] ;
int8 nportk[8] ;
int8 nportl[8] ;
int8 ntemp[8] ;
int8 xread ;
int8 xpointer ;
int32 xticks ;
int8 xchip1[24]; //INTERESANTE
int8 xchip2[24]; //INTERESANTE
int8 xchip3[24];
int8 xchip4[24];
int8 nt ;
#int_RDA
void RDA_isr()
{
buffer[nrx] = getc() ;
nrx = nrx + 1;
if( nrx > LEN_BUFF ) nrx = 0 ;
}
int8 checkpoint(int8 xque)
{
if( ( xque == 3) || ( xque == 2 ) || ( xque == 4) ) return(0);
else return(1);
}
void evalpoint()
{
buffer[ntx] = buffer[ntx] - 48 ;
xcmd = xcmd + buffer[ntx] ;
putc('R') ; putc('D') ;
ni = 0 ;
// Puerto A
if(( xcmd > 0 ) && ( xcmd <9 ) )
{
putc('0' ) ; ni = buffer[ntx] + 48 ; putc(ni); ni = xcmd - 1 ; nt = ni ; ni = nporta[ni];
if( checkpoint(ni) == 1 ) ni = xchip1[nt] ;
}
// Puerto B
if(( xcmd > 8 ) && ( xcmd < 17 ) )
{
if( xcmd < 10 ) putc('0') ; else putc('1');
ni = buffer[ntx] + 48 ;
putc( ni ) ;
ni = xcmd - 9 ;
nt = ni ; ni = nportb[ni];
if( checkpoint(ni) == 1 ) ni = xchip1[nt+8] ;
}
// Puerto C
if(( xcmd > 16 ) && ( xcmd < 25 ) )
{
if( xcmd < 20 ) putc('1') ; else putc('2');
ni = buffer[ntx] + 48 ;
putc( ni ) ;
ni = xcmd - 17 ; nt = ni ; ni = nportc[ni];
if( checkpoint(ni) == 1 ) ni = xchip1[nt+16] ;
}
// Puerto D
if(( xcmd > 24 ) && ( xcmd < 33 ) )
{
if( xcmd < 30 ) putc('2') ; else putc('3');
ni = buffer[ntx] + 48 ;
putc( ni ) ;
ni = xcmd - 25 ; nt = ni ; ni = nportd[ni];
if( checkpoint(ni) == 1 ) ni = xchip2[nt] ;
}
// Puerto E
if(( xcmd > 32 ) && ( xcmd < 41 ) )
{
if( xcmd < 40 ) putc('3') ; else putc('4');
ni = buffer[ntx] + 48 ;
putc( ni ) ;
ni = xcmd - 33 ; nt = ni ; ni = nporte[ni];
if( checkpoint(ni) == 1 ) ni = xchip2[nt+8] ;
}
// Puerto F
if(( xcmd > 40 ) && ( xcmd < 49 ) )
{
putc('4') ;
ni = buffer[ntx] + 48 ;
putc( ni ) ;
ni = xcmd - 41 ; nt = ni ; ni = nportf[ni];
if( checkpoint(ni) == 1 ) ni = xchip2[nt+16] ;
}
// Puerto G
if(( xcmd > 48 ) && ( xcmd < 57 ) )
{
if( xcmd < 50 ) putc('4') ; else putc('5');
ni = buffer[ntx] + 48 ;
putc( ni ) ;
ni = xcmd - 49 ; nt = ni ; ni = nportg[ni];
if( checkpoint(ni) == 1 ) ni = xchip3[nt] ;
}
// Puerto H
if(( xcmd > 56 ) && ( xcmd < 65 ) )
{
if( xcmd < 60 ) putc('5') ; else putc('6');
ni = buffer[ntx] + 48 ;
putc( ni ) ;
ni = xcmd - 57 ; nt = ni ; ni = nporth[ni];
if( checkpoint(ni) == 1 ) ni = xchip3[nt+8] ;
}
// Puerto I
if(( xcmd > 64 ) && ( xcmd < 73 ) )
{
if( xcmd < 70 ) putc('6') ; else putc('7');
ni = buffer[ntx] + 48 ;
putc( ni ) ;
ni = xcmd - 65 ; nt = ni ; ni = nporti[ni];
if( checkpoint(ni) == 1 ) ni = xchip3[nt+16] ;
}
// Puerto J
if(( xcmd > 72 ) && ( xcmd < 81 ) )
{
if( xcmd < 80 ) putc('7') ; else putc('8');
ni = buffer[ntx] + 48 ;
putc( ni ) ;
ni = xcmd - 73 ; nt = ni ; ni = nportj[ni];
if( checkpoint(ni) == 1 ) ni = xchip4[nt] ;
}
// Puerto K
if(( xcmd > 80 ) && ( xcmd < 89 ) )
{
putc('8') ;
ni = buffer[ntx] + 48 ;
putc( ni ) ;
ni = xcmd - 81 ; nt = ni ; ni = nportk[ni];
if( checkpoint(ni) == 1 ) ni = xchip4[nt+8] ;
}
// Puerto L
if(( xcmd > 88 ) && ( xcmd < 97 ) )
{
if( xcmd < 90 ) putc('8') ; else putc('9');
ni = buffer[ntx] + 48 ;
putc( ni ) ;
ni = xcmd - 89 ; nt = ni ; ni = nportl[ni];
if( checkpoint(ni) == 1 ) ni = xchip4[nt+16] ;
}
if( ( ni == 3) ) { putc('L' ); }
else
{
if( ( ni == 4) || ( ni == 2 ) ) putc('H' );
else putc( ni + 48 );
}
putc(13) ;
}
void MakeByte()
{
int8 nmake ;
nmake = 0 ;
if( ntemp[0] == 1 ) nmake = nmake + 0x01 ;
if( ntemp[1] == 1 ) nmake = nmake + 0x02 ;
if( ntemp[2] == 1 ) nmake = nmake + 0x04 ;
if( ntemp[3] == 1 ) nmake = nmake + 0x08 ;
if( ntemp[4] == 1 ) nmake = nmake + 0x10 ;
if( ntemp[5] == 1 ) nmake = nmake + 0x20 ;
if( ntemp[6] == 1 ) nmake = nmake + 0x40 ;
if( ntemp[7] == 1 ) nmake = nmake + 0x80 ;
ni = nmake ;
}
void CommandWR()
{
if( xstate == 25 )
{
xstate = 0 ;
if( ( buffer[ntx] == 'L' ) || ( buffer[ntx] == 'l' ) || ( buffer[ntx] == 'H' ) || ( buffer[ntx] == 'h' ) )
{
if( ( buffer[ntx] == 'L' ) || ( buffer[ntx] == 'l' ) ) ni = 3 ; else ni = 4 ;
if( xcmd < 9 ) { xptr = xcmd - 1 ; if( ( nporta[xptr]> 1 ) && ( nporta[xptr] < 5 ) ) nporta[xptr] = ni ; }
if( ( xcmd > 8 ) && ( xcmd < 17 ) ) { xptr = xcmd - 9 ; if( ( nportb[xptr]> 1 ) && ( nportb[xptr] < 5 ) ) nportb[xptr] = ni ; }
if( ( xcmd > 16 ) && ( xcmd < 25 ) ) { xptr = xcmd - 17 ; if( ( nportc[xptr]> 1 ) && ( nportc[xptr] < 5 ) ) nportc[xptr] = ni ; }
if( ( xcmd > 24 ) && ( xcmd < 33 ) ) { xptr = xcmd - 25 ; if( ( nportd[xptr]> 1 ) && ( nportd[xptr] < 5 ) ) nportd[xptr] = ni ; }
if( ( xcmd > 32 ) && ( xcmd < 41 ) ) { xptr = xcmd - 33 ; if( ( nporte[xptr]> 1 ) && ( nporte[xptr] < 5 ) ) nporte[xptr] = ni ; }
if( ( xcmd > 40 ) && ( xcmd < 49 ) ) { xptr = xcmd - 41 ; if( ( nportf[xptr]> 1 ) && ( nportf[xptr] < 5 ) ) nportf[xptr] = ni ; }
if( ( xcmd > 48 ) && ( xcmd < 57 ) ) { xptr = xcmd - 49 ; if( ( nportg[xptr]> 1 ) && ( nportg[xptr] < 5 ) ) nportg[xptr] = ni ; }
if( ( xcmd > 56 ) && ( xcmd < 65 ) ) { xptr = xcmd - 57 ; if( ( nporth[xptr]> 1 ) && ( nporth[xptr] < 5 ) ) nporth[xptr] = ni ; }
if( ( xcmd > 64 ) && ( xcmd < 73 ) ) { xptr = xcmd - 65 ; if( ( nporti[xptr]> 1 ) && ( nporti[xptr] < 5 ) ) nporti[xptr] = ni ; }
if( ( xcmd > 72 ) && ( xcmd < 81 ) ) { xptr = xcmd - 73 ; if( ( nportj[xptr]> 1 ) && ( nportj[xptr] < 5 ) ) nportj[xptr] = ni ; }
if( ( xcmd > 80 ) && ( xcmd < 89 ) ) { xptr = xcmd - 81 ; if( ( nportk[xptr]> 1 ) && ( nportk[xptr] < 5 ) ) nportk[xptr] = ni ; }
if( ( xcmd > 88 ) && ( xcmd < 97 ) ) { xptr = xcmd - 89 ; if( ( nportl[xptr]> 1 ) && ( nportl[xptr] < 5 ) ) nportl[xptr] = ni ; }
sprintf( cstring , "OK") ; puts(cstring);
}
}
if( xstate == 24 )
{
xstate = 0 ;
if(( buffer[ntx] > 47 ) && ( buffer[ntx] < 58 ) )
{
xstate = 25 ;
buffer[ntx] = buffer[ntx] - 48 ;
xcmd = xcmd + buffer[ntx] ;
}
}
if( xstate == 23 )
{
ni = buffer[ntx] ;
if( ( ni & 0x01 ) == 0x01 ) ntemp[0] = 4 ; else ntemp[0] = 3 ;
if( ( ni & 0x02 ) == 0x02 ) ntemp[1] = 4 ; else ntemp[1] = 3 ;
if( ( ni & 0x04 ) == 0x04 ) ntemp[2] = 4 ; else ntemp[2] = 3 ;
if( ( ni & 0x08 ) == 0x08 ) ntemp[3] = 4 ; else ntemp[3] = 3 ;
if( ( ni & 0x10 ) == 0x10 ) ntemp[4] = 4 ; else ntemp[4] = 3 ;
if( ( ni & 0x20 ) == 0x20 ) ntemp[5] = 4 ; else ntemp[5] = 3 ;
if( ( ni & 0x40 ) == 0x40 ) ntemp[6] = 4 ; else ntemp[6] = 3 ;
if( ( ni & 0x80 ) == 0x80 ) ntemp[7] = 4 ; else ntemp[7] = 3 ;
for(ni = 0 ; ni < 8 ; ni ++ )
{
if( ( xptr == 0 ) && ( nporta[ni] > 1 ) && ( nporta[ni] < 5 ) ) nporta[ni] = ntemp[ni];
if( ( xptr == 1 ) && ( nportb[ni] > 1 ) && ( nportb[ni] < 5 ) ) nportb[ni] = ntemp[ni];
if( ( xptr == 2 ) && ( nportc[ni] > 1 ) && ( nportc[ni] < 5 ) ) nportc[ni] = ntemp[ni];
if( ( xptr == 3 ) && ( nportd[ni] > 1 ) && ( nportd[ni] < 5 ) ) nportd[ni] = ntemp[ni];
if( ( xptr == 4 ) && ( nporte[ni] > 1 ) && ( nporte[ni] < 5 ) ) nporte[ni] = ntemp[ni];
if( ( xptr == 5 ) && ( nportf[ni] > 1 ) && ( nportf[ni] < 5 ) ) nportf[ni] = ntemp[ni];
if( ( xptr == 6 ) && ( nportg[ni] > 1 ) && ( nportg[ni] < 5 ) ) nportg[ni] = ntemp[ni];
if( ( xptr == 7 ) && ( nporth[ni] > 1 ) && ( nporth[ni] < 5 ) ) nporth[ni] = ntemp[ni];
if( ( xptr == 8 ) && ( nporti[ni] > 1 ) && ( nporti[ni] < 5 ) ) nporti[ni] = ntemp[ni];
if( ( xptr == 9 ) && ( nportj[ni] > 1 ) && ( nportj[ni] < 5 ) ) nportj[ni] = ntemp[ni];
if( ( xptr == 10 ) && ( nportk[ni] > 1 ) && ( nportk[ni] < 5 ) ) nportk[ni] = ntemp[ni];
if( ( xptr == 11 ) && ( nportl[ni] > 1 ) && ( nportl[ni] < 5 ) ) nportl[ni] = ntemp[ni];
}
xptr ++ ;
if( xptr> 11 )
{
xstate = 0 ;
sprintf( cstring , "OK") ; puts(cstring);
}
}
if( xstate == 22 )
{
xstate = 0 ;
if( ( buffer[ntx] == 'X' ) || ( buffer[ntx] == 'x' ) ) { ntemp[xptr] = 9 ; xstate = 22 ; }
if( ( buffer[ntx] == 'H' ) || ( buffer[ntx] == 'h' ) ) { ntemp[xptr] = 4 ; xstate = 22 ; }
if( ( buffer[ntx] == 'L' ) || ( buffer[ntx] == 'l' ) ) { ntemp[xptr] = 3 ; xstate = 22 ; }
if ( buffer[ntx] == '0' ) { ntemp[xptr] = 5 ; xstate = 22 ; }
if ( buffer[ntx] == '1' ) { ntemp[xptr] = 6 ; xstate = 22 ; }
xptr ++ ;
if( xptr == 8 )
{
xstate = 0;
sprintf( cstring , "OK") ; puts(cstring);
for( ni = 0 ; ni < 8 ; ni ++ )
{
if( ( ntemp[ni] > 2 ) && ( ntemp[ni] < 7 ) && ( xcmd == 1 ) ) nporta[ni] = ntemp[ni];
if( ( ntemp[ni] > 2 ) && ( ntemp[ni] < 7 ) && ( xcmd == 2 ) ) nportb[ni] = ntemp[ni];
if( ( ntemp[ni] > 2 ) && ( ntemp[ni] < 7 ) && ( xcmd == 3 ) ) nportc[ni] = ntemp[ni];
if( ( ntemp[ni] > 2 ) && ( ntemp[ni] < 7 ) && ( xcmd == 4 ) ) nportd[ni] = ntemp[ni];
if( ( ntemp[ni] > 2 ) && ( ntemp[ni] < 7 ) && ( xcmd == 5 ) ) nporte[ni] = ntemp[ni];
if( ( ntemp[ni] > 2 ) && ( ntemp[ni] < 7 ) && ( xcmd == 6 ) ) nportf[ni] = ntemp[ni];
if( ( ntemp[ni] > 2 ) && ( ntemp[ni] < 7 ) && ( xcmd == 7 ) ) nportg[ni] = ntemp[ni];
if( ( ntemp[ni] > 2 ) && ( ntemp[ni] < 7 ) && ( xcmd == 8 ) ) nporth[ni] = ntemp[ni];
if( ( ntemp[ni] > 2 ) && ( ntemp[ni] < 7 ) && ( xcmd == 9 ) ) nporti[ni] = ntemp[ni];
if( ( ntemp[ni] > 2 ) && ( ntemp[ni] < 7 ) && ( xcmd == 10 ) ) nportj[ni] = ntemp[ni];
if( ( ntemp[ni] > 2 ) && ( ntemp[ni] < 7 ) && ( xcmd == 11 ) ) nportk[ni] = ntemp[ni];
if( ( ntemp[ni] > 2 ) && ( ntemp[ni] < 7 ) && ( xcmd == 12 ) ) nportl[ni] = ntemp[ni];
}
}
}
if( xstate == 21 )
{
xstate = 0 ;
xptr = 0 ;
if( ( buffer[ntx] == 'A' ) || ( buffer[ntx] == 'a' ) ) { xcmd = 1 ; xstate = 22 ; }
if( ( buffer[ntx] == 'B' ) || ( buffer[ntx] == 'b' ) ) { xcmd = 2 ; xstate = 22 ; }
if( ( buffer[ntx] == 'C' ) || ( buffer[ntx] == 'c' ) ) { xcmd = 3 ; xstate = 22 ; }
if( ( buffer[ntx] == 'D' ) || ( buffer[ntx] == 'd' ) ) { xcmd = 4 ; xstate = 22 ; }
if( ( buffer[ntx] == 'E' ) || ( buffer[ntx] == 'e' ) ) { xcmd = 5 ; xstate = 22 ; }
if( ( buffer[ntx] == 'F' ) || ( buffer[ntx] == 'f' ) ) { xcmd = 6 ; xstate = 22 ; }
if( ( buffer[ntx] == 'G' ) || ( buffer[ntx] == 'g' ) ) { xcmd = 7 ; xstate = 22 ; }
if( ( buffer[ntx] == 'H' ) || ( buffer[ntx] == 'h' ) ) { xcmd = 8 ; xstate = 22 ; }
if( ( buffer[ntx] == 'I' ) || ( buffer[ntx] == 'i' ) ) { xcmd = 9 ; xstate = 22 ; }
if( ( buffer[ntx] == 'J' ) || ( buffer[ntx] == 'j' ) ) { xcmd = 10 ; xstate = 22 ; }
if( ( buffer[ntx] == 'K' ) || ( buffer[ntx] == 'k' ) ) { xcmd = 11 ; xstate = 22 ; }
if( ( buffer[ntx] == 'L' ) || ( buffer[ntx] == 'l' ) ) { xcmd = 12 ; xstate = 22 ; }
if( ( buffer[ntx] == 'X' ) || ( buffer[ntx] == 'x' ) ) { xstate = 23 ; }
if( ( buffer[ntx] == 'Y' ) || ( buffer[ntx] == 'y' ) ) { xstate = 26 ; }
if(( buffer[ntx] > 47 ) && ( buffer[ntx] < 58 ) )
{
xstate = 24 ;
xcmd = buffer[ntx] - 48 ;
xcmd = xcmd * 10 ;
}
}
if( xstate == 20 )
{
xstate = 0 ;
if( ( buffer[ntx] == 'R' ) || ( buffer[ntx] == 'r' ) ) { xstate = 21 ; }
}
}
void lputc( int8 x_gprint )
{
bprint[bptr] = x_gprint ;
bptr ++ ;
bprint[bptr] = 0 ;
}
void AjusteRD( int8 xajuste )
{
int8 nbuu ; int8 lbuu ; int8 lchip ;
if( ( xajuste == 1 ) || ( xajuste == 2 ) || ( xajuste == 3 ) ) { nbuu = ( xajuste - 1 ) * 8 ; lchip = 1 ; }
if( ( xajuste == 4 ) || ( xajuste == 5 ) || ( xajuste == 6 ) ) { nbuu = ( xajuste - 4 ) * 8 ; lchip = 2 ; }
if( ( xajuste == 7 ) || ( xajuste == 8 ) || ( xajuste == 9 ) ) { nbuu = ( xajuste - 7 ) * 8 ; lchip = 3 ; }
if( ( xajuste == 10 ) || ( xajuste == 11 ) || ( xajuste == 12 ) ) { nbuu = ( xajuste - 10 ) * 8 ; lchip = 4 ; }
for( ni = 0 ; ni < 8 ; ni ++ )
{
lbuu = 0 ;
if( ntemp[ni] == 3) { lputc('L' ); lbuu = 1 ; }
if( ( ntemp[ni] == 4) || ( ntemp[ni] == 2 ) ) { lputc('H' ); lbuu = 1 ; }
if( lbuu == 0 )
{
if( lchip == 1 )
{
if( xchip1[nbuu] == 0 ) { lputc('0' ) ; } else { lputc('1'); }
}
if( lchip == 2 )
{
if( xchip2[nbuu] == 0 ) { lputc('0' ) ; } else { lputc('1'); }
}
if( lchip == 3 )
{
if( xchip3[nbuu] == 0 ) { lputc('0' ) ; } else { lputc('1'); }
}
if( lchip == 4 )
{
if( xchip4[nbuu] == 0 ) { lputc('0' ) ; } else { lputc('1'); }
}
}
nbuu++ ;
}
}
void CommandRD()
{
if( xstate == 12 )
{
if(( buffer[ntx] > 47 ) && ( buffer[ntx] < 58 ) )
{
xstate = 0 ;
evalpoint();
}
}
if( xstate == 11 )
{
xstate = 0 ;
if( ( buffer[ntx] == 'A' ) || ( buffer[ntx] == 'a' ) )
{
bptr = 0 ; lputc('R') ; lputc('D') ; lputc('A') ; for( ni = 0 ; ni < 8 ; ni ++ ) ntemp[ni] = nporta[ni] ; AjusteRD(1); puts( bprint );
}
if( ( buffer[ntx] == 'B' ) || ( buffer[ntx] == 'b' ) )
{
bptr = 0 ; lputc('R') ; lputc('D') ; lputc('B') ; for( ni = 0 ; ni < 8 ; ni ++ ) ntemp[ni] = nportb[ni] ; AjusteRD(2); puts( bprint );
}
if( ( buffer[ntx] == 'C' ) || ( buffer[ntx] == 'c' ) )
{
bptr = 0 ; lputc('R') ; lputc('D') ; lputc('C') ; for( ni = 0 ; ni < 8 ; ni ++ ) ntemp[ni] = nportc[ni] ; AjusteRD(3); puts( bprint );
}
if( ( buffer[ntx] == 'D' ) || ( buffer[ntx] == 'd' ) )
{
bptr = 0 ; lputc('R') ; lputc('D') ; lputc('D') ; for( ni = 0 ; ni < 8 ; ni ++ ) ntemp[ni] = nportd[ni] ; AjusteRD(4); puts( bprint );
}
if( ( buffer[ntx] == 'E' ) || ( buffer[ntx] == 'e' ) )
{
bptr = 0 ; lputc('R') ; lputc('D') ; lputc('E') ; for( ni = 0 ; ni < 8 ; ni ++ ) ntemp[ni] = nporte[ni] ; AjusteRD(5); puts( bprint );
}
if( ( buffer[ntx] == 'F' ) || ( buffer[ntx] == 'f' ) )
{
bptr = 0 ; lputc('R') ; lputc('D') ; lputc('F') ; for( ni = 0 ; ni < 8 ; ni ++ ) ntemp[ni] = nportf[ni] ; AjusteRD(6); puts( bprint );
}
if( ( buffer[ntx] == 'G' ) || ( buffer[ntx] == 'g' ) )
{
bptr = 0 ; lputc('R') ; lputc('D') ; lputc('G') ; for( ni = 0 ; ni < 8 ; ni ++ ) ntemp[ni] = nportg[ni] ; AjusteRD(7); puts( bprint );
}
if( ( buffer[ntx] == 'H' ) || ( buffer[ntx] == 'h' ) )
{
bptr = 0 ; lputc('R') ; lputc('D') ; lputc('H') ; for( ni = 0 ; ni < 8 ; ni ++ ) ntemp[ni] = nporth[ni] ; AjusteRD(

; puts( bprint );
}
if( ( buffer[ntx] == 'I' ) || ( buffer[ntx] == 'i' ) )
{
bptr = 0 ; lputc('R') ; lputc('D') ; lputc('I') ; for( ni = 0 ; ni < 8 ; ni ++ ) ntemp[ni] = nporti[ni] ; AjusteRD(9); puts( bprint );
}
if( ( buffer[ntx] == 'J' ) || ( buffer[ntx] == 'j' ) )
{
bptr = 0 ; lputc('R') ; lputc('D') ; lputc('J') ; for( ni = 0 ; ni < 8 ; ni ++ ) ntemp[ni] = nportj[ni] ; AjusteRD(10); puts( bprint );
}
if( ( buffer[ntx] == 'K' ) || ( buffer[ntx] == 'k' ) )
{
bptr = 0 ; lputc('R') ; lputc('D') ; lputc('K') ; for( ni = 0 ; ni < 8 ; ni ++ ) ntemp[ni] = nportk[ni] ; AjusteRD(11); puts( bprint );
}
if( ( buffer[ntx] == 'L' ) || ( buffer[ntx] == 'l' ) )
{
bptr = 0 ; lputc('R') ; lputc('D') ; lputc('L') ; for( ni = 0 ; ni < 8 ; ni ++ ) ntemp[ni] = nportl[ni] ; AjusteRD(12); puts( bprint );
}
if( ( buffer[ntx] == 'X' ) || ( buffer[ntx] == 'x' ) )
{
xstate = 0 ;
bptr = 0 ;
lputc('R');
lputc('D');
lputc('X');
for( xcmd = 0 ; xcmd < 12 ; xcmd ++ )
{
for( ni = 0 ; ni < 8 ; ni ++ )
{
if( xcmd == 0 ) ntemp[ni] = xchip1[ni];
if( xcmd == 1 ) ntemp[ni] = xchip1[ni+8];
if( xcmd == 2 ) ntemp[ni] = xchip1[ni+16];
if( xcmd == 3 ) ntemp[ni] = xchip2[ni];
if( xcmd == 4 ) ntemp[ni] = xchip2[ni+8];
if( xcmd == 5 ) ntemp[ni] = xchip2[ni+16];
if( xcmd == 6 ) ntemp[ni] = xchip3[ni];
if( xcmd == 7 ) ntemp[ni] = xchip3[ni+8];
if( xcmd == 8 ) ntemp[ni] = xchip3[ni+16];
if( xcmd == 9 ) ntemp[ni] = xchip4[ni];
if( xcmd == 10 ) ntemp[ni] = xchip4[ni+8];
if( xcmd == 11 ) ntemp[ni] = xchip4[ni+16];
}
MakeByte();
lputc(ni);
}
puts( bprint) ;
}
if( ( buffer[ntx] == 'Y' ) || ( buffer[ntx] == 'y' ) )
{
bptr = 0 ; lputc('R') ; lputc('D') ; lputc('Y') ;
for( ni = 0 ; ni < 8 ; ni ++ ) ntemp[ni] = nporta[ni] ; AjusteRD(1);
for( ni = 0 ; ni < 8 ; ni ++ ) ntemp[ni] = nportb[ni] ; AjusteRD(2);
for( ni = 0 ; ni < 8 ; ni ++ ) ntemp[ni] = nportc[ni] ; AjusteRD(3);
for( ni = 0 ; ni < 8 ; ni ++ ) ntemp[ni] = nportd[ni] ; AjusteRD(4);
for( ni = 0 ; ni < 8 ; ni ++ ) ntemp[ni] = nporte[ni] ; AjusteRD(5);
for( ni = 0 ; ni < 8 ; ni ++ ) ntemp[ni] = nportf[ni] ; AjusteRD(6);
for( ni = 0 ; ni < 8 ; ni ++ ) ntemp[ni] = nportg[ni] ; AjusteRD(7);
for( ni = 0 ; ni < 8 ; ni ++ ) ntemp[ni] = nporth[ni] ; AjusteRD(

;
for( ni = 0 ; ni < 8 ; ni ++ ) ntemp[ni] = nporti[ni] ; AjusteRD(9);
for( ni = 0 ; ni < 8 ; ni ++ ) ntemp[ni] = nportj[ni] ; AjusteRD(10);
for( ni = 0 ; ni < 8 ; ni ++ ) ntemp[ni] = nportk[ni] ; AjusteRD(11);
for( ni = 0 ; ni < 8 ; ni ++ ) ntemp[ni] = nportl[ni] ; AjusteRD(12);
puts( bprint ) ;
}
if(( buffer[ntx] > 47 ) && ( buffer[ntx] < 58 ) )
{
xstate = 12 ;
xcmd = buffer[ntx] - 48 ;
xcmd = xcmd * 10 ;
}
}
if( xstate == 10 )
{
xstate = 0 ;
if( ( buffer[ntx] == 'D' ) || ( buffer[ntx] == 'd' ) ) { xstate = 11 ; }
}
}
void SetConfig()
{
// Numero de Serie
if( xstate == 6 )
{
xstate = 0 ;
if( buffer[ntx] == '?' )
{
if ( wserial == 1 ) { sprintf( cstring , "H-M81019-001" ); }
if ( wserial == 2 ) { sprintf( cstring , "H-M81019-002" ); }
if ( wserial == 3 ) { sprintf( cstring , "H-M81019-003" ); }
if ( wserial == 4 ) { sprintf( cstring , "H-M81019-004" ); }
if ( wserial == 5 ) { sprintf( cstring , "H-M81019-005" ); }
puts( cstring );
}
}
if( xstate == 5 )
{
ni = buffer[ntx] ;
if( ( ni & 0x01 ) == 0x01 ) ntemp[0] = 2 ; else ntemp[0] = 1 ;
if( ( ni & 0x02 ) == 0x02 ) ntemp[1] = 2 ; else ntemp[1] = 1 ;
if( ( ni & 0x04 ) == 0x04 ) ntemp[2] = 2 ; else ntemp[2] = 1 ;
if( ( ni & 0x08 ) == 0x08 ) ntemp[3] = 2 ; else ntemp[3] = 1 ;
if( ( ni & 0x10 ) == 0x10 ) ntemp[4] = 2 ; else ntemp[4] = 1 ;
if( ( ni & 0x20 ) == 0x20 ) ntemp[5] = 2 ; else ntemp[5] = 1 ;
if( ( ni & 0x40 ) == 0x40 ) ntemp[6] = 2 ; else ntemp[6] = 1 ;
if( ( ni & 0x80 ) == 0x80 ) ntemp[7] = 2 ; else ntemp[7] = 1 ;
if( xptr == 0 ) { for( ni = 0 ; ni < 8 ; ni ++ ) nporta[ni] = ntemp[ni] ; }
if( xptr == 1 ) { for( ni = 0 ; ni < 8 ; ni ++ ) nportb[ni] = ntemp[ni] ; }
if( xptr == 2 ) { for( ni = 0 ; ni < 8 ; ni ++ ) nportc[ni] = ntemp[ni] ; }
if( xptr == 3 ) { for( ni = 0 ; ni < 8 ; ni ++ ) nportd[ni] = ntemp[ni] ; }
if( xptr == 4 ) { for( ni = 0 ; ni < 8 ; ni ++ ) nporte[ni] = ntemp[ni] ; }
if( xptr == 5 ) { for( ni = 0 ; ni < 8 ; ni ++ ) nportf[ni] = ntemp[ni] ; }
if( xptr == 6 ) { for( ni = 0 ; ni < 8 ; ni ++ ) nportg[ni] = ntemp[ni] ; }
if( xptr == 7 ) { for( ni = 0 ; ni < 8 ; ni ++ ) nporth[ni] = ntemp[ni] ; }
if( xptr == 8 ) { for( ni = 0 ; ni < 8 ; ni ++ ) nporti[ni] = ntemp[ni] ; }
if( xptr == 9 ) { for( ni = 0 ; ni < 8 ; ni ++ ) nportj[ni] = ntemp[ni] ; }
if( xptr == 10 ) { for( ni = 0 ; ni < 8 ; ni ++ ) nportk[ni] = ntemp[ni] ; }
if( xptr == 11 ) { for( ni = 0 ; ni < 8 ; ni ++ ) nportl[ni] = ntemp[ni] ; }
xptr ++ ;
if( xptr == 12 ) { xstate = 0 ; sprintf( cstring , "OK" ); puts(cstring); }
}
if( xstate == 4 )
{
if( ( buffer[ntx] == 'W' ) || ( buffer[ntx] == 'w' ) || ( buffer[ntx] == 'R' ) || ( buffer[ntx] == 'r' ) || ( buffer[ntx] == 'X' ) || ( buffer[ntx] == 'x' ) )
{
if( ( buffer[ntx] == 'W' ) || ( buffer[ntx] == 'w' ) || ( buffer[ntx] == 'R' ) || ( buffer[ntx] == 'r' ) )
{
if(( buffer[ntx] == 'W' ) || ( buffer[ntx] == 'w' ) ) xact = 2 ;
else xact = 1 ;
if( xcmd == 1 ) nporta[xptr] = xact ;
if( xcmd == 2 ) nportb[xptr] = xact ;
if( xcmd == 3 ) nportc[xptr] = xact ;
if( xcmd == 4 ) nportd[xptr] = xact ;
if( xcmd == 5 ) nporte[xptr] = xact ;
if( xcmd == 6 ) nportf[xptr] = xact ;
if( xcmd == 7 ) nportg[xptr] = xact ;
if( xcmd == 8 ) nporth[xptr] = xact ;
if( xcmd == 9 ) nporti[xptr] = xact ;
if( xcmd == 10 ) nportj[xptr] = xact ;
if( xcmd == 11 ) nportk[xptr] = xact ;
if( xcmd == 12 ) nportl[xptr] = xact ;
}
xptr ++ ;
if( xptr == 8 ) { sprintf( cstring , "OK" ); puts(cstring); xstate = 0 ; buffer[ntx] = 0 ; }
}
else { xstate = 0 ; }
}
if( xstate == 3 )
{
xstate = 0 ;
xptr = 0 ;
if( ( buffer[ntx] == 'A' ) || ( buffer[ntx] == 'a' ) ) { xstate = 4 ; xcmd = 1 ; }
if( ( buffer[ntx] == 'B' ) || ( buffer[ntx] == 'b' ) ) { xstate = 4 ; xcmd = 2 ; }
if( ( buffer[ntx] == 'C' ) || ( buffer[ntx] == 'c' ) ) { xstate = 4 ; xcmd = 3 ; }
if( ( buffer[ntx] == 'D' ) || ( buffer[ntx] == 'd' ) ) { xstate = 4 ; xcmd = 4 ; }
if( ( buffer[ntx] == 'E' ) || ( buffer[ntx] == 'e' ) ) { xstate = 4 ; xcmd = 5 ; }
if( ( buffer[ntx] == 'F' ) || ( buffer[ntx] == 'f' ) ) { xstate = 4 ; xcmd = 6 ; }
if( ( buffer[ntx] == 'G' ) || ( buffer[ntx] == 'g' ) ) { xstate = 4 ; xcmd = 7 ; }
if( ( buffer[ntx] == 'H' ) || ( buffer[ntx] == 'h' ) ) { xstate = 4 ; xcmd = 8 ; }
if( ( buffer[ntx] == 'I' ) || ( buffer[ntx] == 'i' ) ) { xstate = 4 ; xcmd = 9 ; }
if( ( buffer[ntx] == 'J' ) || ( buffer[ntx] == 'j' ) ) { xstate = 4 ; xcmd = 10 ; }
if( ( buffer[ntx] == 'K' ) || ( buffer[ntx] == 'k' ) ) { xstate = 4 ; xcmd = 11 ; }
if( ( buffer[ntx] == 'L' ) || ( buffer[ntx] == 'l' ) ) { xstate = 4 ; xcmd = 12 ; }
if( ( buffer[ntx] == 'X' ) || ( buffer[ntx] == 'x' ) ) { xstate = 5 ; xptr = 0 ; }
}
if( xstate == 2 )
{
xstate = 0 ;
if( ( buffer[ntx] == 'T' ) || ( buffer[ntx] == 't' ) ) { xstate = 3 ; }
if( ( buffer[ntx] == 'R' ) || ( buffer[ntx] == 'r' ) ) { xstate = 6 ; }
}
if( xstate == 1 )
{
xstate = 0 ;
if( ( buffer[ntx] == 'E' ) || ( buffer[ntx] == 'e' ) ) { xstate = 2 ; }
}
}
void Lectura_Pines()
{
xticks ++ ;
if( xticks > TICKS )
{
if( xread == 2 )
{
xread = 0 ;
}
if( xread == 1 )
{
xchip1[xpointer] = input(PIN_D0);
xchip2[xpointer] = input(PIN_D1);
xchip3[xpointer] = input(PIN_D2);
xchip4[xpointer] = input(PIN_D3);
xread ++ ;
xticks = 0 ;
output_bit(PIN_C2 , 0 );
xpointer ++ ;
if( xpointer > 23 )
{
xpointer = 0 ;
output_bit(PIN_C3, 1 );
delay_ms(2);
output_bit(PIN_C3, 0 );
}
}
if( xread == 0 )
{
xread ++ ;
output_bit(PIN_C2 , 1 );
xticks = 0 ;
}
}
}
void Reset()
{
if( xstate == 72 )
{
xstate = 0 ;
if( ( buffer[ntx] == 'T' ) || ( buffer[ntx] == 't' ) )
{
for( ni = 0 ; ni < 8 ; ni ++ )
{
nporta[ni] = 0 ;
nportb[ni] = 0 ;
nportc[ni] = 0 ;
nportd[ni] = 0 ;
nporte[ni] = 0 ;
nportf[ni] = 0 ;
nportg[ni] = 0 ;
nporth[ni] = 0 ;
nporti[ni] = 0 ;
nportj[ni] = 0 ;
nportk[ni] = 0 ;
nportl[ni] = 0 ;
}
}
}
if( xstate == 71 )
{
xstate = 0 ;
if( ( buffer[ntx] == 'S' ) || ( buffer[ntx] == 's' ) ) { xstate = 72 ; }
}
if( xstate == 70 )
{
xstate = 0 ;
if( ( buffer[ntx] == 'R' ) || ( buffer[ntx] == 'r' ) ) { xstate = 71 ; }
}
}
void evaluate()
{
Lectura_Pines();
while ( nrx != ntx )
{
Lectura_Pines();
if( xstate == 62 )
{
xstate = 0 ;
if( buffer[ntx] == '?' )
{
sprintf( cstring , "Profit Manufacturing" );
puts(cstring);
if ( wserial == 1 ) { sprintf( cstring , "H-M81019-001" ); }
if ( wserial == 2 ) { sprintf( cstring , "H-M81019-002" ); }
if ( wserial == 3 ) { sprintf( cstring , "H-M81019-003" ); }
if ( wserial == 4 ) { sprintf( cstring , "H-M81019-004" ); }
if ( wserial == 5 ) { sprintf( cstring , "H-M81019-005" ); }
puts( cstring );
sprintf( cstring , "Version 1.81019" );
puts(cstring);
sprintf( cstring , "96-Test-Point" );
puts(cstring);
}
}
if( xstate == 61 )
{
xstate = 0 ; if( ( buffer[ntx] == 'N' ) || ( buffer[ntx] == 'n' ) ) { xstate = 62 ;}
}
if( xstate == 60 )
{
xstate = 0 ; if( ( buffer[ntx] == 'D' ) || ( buffer[ntx] == 'd' ) ) { xstate = 61 ;}
}
if( xstate == 32 )
{
xstate = 0 ;
if( buffer[ntx] == '?' ) { sprintf( cstring , "Version 1.81019" ); puts( cstring ); }
}
if( xstate == 31 )
{
xstate = 0 ;
if( ( buffer[ntx] == 'R' ) || ( buffer[ntx] == 'r' ) ) { xstate = 32 ; }
}
if( xstate == 30 )
{
xstate = 0 ;
if( ( buffer[ntx] == 'E' ) || ( buffer[ntx] == 'e' ) ) { xstate = 31 ; }
}
CommandWR();
CommandRD();
SetConfig();
Reset();
if( xstate == 0 )
{
if( ( buffer[ntx] == 'S' ) || ( buffer[ntx] == 's' ) ) { xstate = 1 ; }
if( ( buffer[ntx] == 'R' ) || ( buffer[ntx] == 'r' ) ) { xstate = 10 ;}
if( ( buffer[ntx] == 'W' ) || ( buffer[ntx] == 'w' ) ) { xstate = 20 ;}
if( ( buffer[ntx] == 'V' ) || ( buffer[ntx] == 'v' ) ) { xstate = 30 ;}
if( ( buffer[ntx] == 'I' ) || ( buffer[ntx] == 'i' ) ) { xstate = 60 ;}
if( ( buffer[ntx] == '*' ) ) { xstate = 70 ;}
}
// Seccion de Donde va ir a Escribir ( Estado 10 ) o a Leer ( Estado 1 )
ntx = ntx + 1 ;
if( ntx > LEN_BUFF ) ntx = 0 ;
}
}
void main()
{
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
// setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_ccp1(ccp_off);
setup_ccp2(ccp_off);
// setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
set_tris_a(0xFF);
set_tris_e(0xFF);
set_tris_c(0xB0);
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
// Definicion de Puertos de Salida
set_tris_b(0x00);
set_tris_d(0xFF);
output_b(0x00);
output_d(0x00);
port_b_pullups (TRUE);
wserial = T_SERIAL ;
ntx = 0 ;
nrx = 0 ;
xstate = 0 ;
crlf[0] = 13 ; crlf[1] = 10 ; crlf[2] = 0 ;
for( ni = 0 ; ni < 8 ; ni ++ )
{
nporta[ni] = 0 ;
nportb[ni] = 0 ;
nportc[ni] = 0 ;
nportd[ni] = 0 ;
nporte[ni] = 0 ;
nportf[ni] = 0 ;
nportg[ni] = 0 ;
nporth[ni] = 0 ;
nporti[ni] = 0 ;
nportj[ni] = 0 ;
nportk[ni] = 0 ;
nportl[ni] = 0 ;
}
output_bit( PIN_C2 , 0 );
xread = 0 ;
xpointer = 0 ;
xticks = 0 ;
output_bit(PIN_C3, 1 );
delay_ms(250);
output_bit(PIN_C3, 0 );
sprintf( cstring , "Profit Manufacturing" );
puts(cstring);
sprintf( cstring , "Hecho en Mexico" );
puts(cstring);
sprintf( cstring , "Version 1.81019" );
puts(cstring);
while(1)
{
evaluate();
}
}