/**************************************************************************\ | | | Codigo Fuente de PRN_CNT | | | \**************************************************************************/ /* A2 01 -> !1 Strobe A1 14 -> !2 AutoFeed A0 16 -> 4 Inicializaci¢n READ/WRITE !8 */ #include "c:\backup\fuentes\prn_cnt\prn_cnt.h" #include #include #include #include char Salida[10]; int Port_Address[3] = { 0x378, 0x278, 0x3BC }; char Lpt_usado = 1; /**************************************************************************\ | | | Esta rutina debe ser llamada al iniciar el programa que use las librerias. | | | | Es la encargada de poner a cero todas la salidas, al principio y al salir. | | | | | | Parametros de Entrada: ( ninguno ) | | Parametros de Salida: | | OK Todo ha ido bien. | | ERROR_EN_ATEXIT Error registrando funci¢n | | FUNCION_YA_REGISTRADA No es la 1¦ vez | | | \**************************************************************************/ //Û int Init_all(void) { static char vez = 0; if ( vez == 0 ) { vez = 1; Reset_all(); if ( atexit( Reset_all ) != 0 ) return ERROR_EN_ATEXIT; else return OK; } else return FUNCION_YA_REGISTRADA; } /**************************************************************************\ | | | WARNING: | | El uso de la siguiente funci¢n esta indicada solo para usuarios | | experimentados, se debe usar en lugar de la funci¢n anterior. | | | | Parametros de Entrada: | | ( LEER/ESCRIBIR ) // L/E los registros del fich| | ( %s ) // _ero %s | | | | Parametros de Salida: | | 1 Ok! OK | | -1 Fallo desconocido FALLO_DESCONOCIDO | | -2 Archivo no existe NO_ARCHIVO | | -3 Error archivo no valido ARCHIVO_INCORRECTO | | | \**************************************************************************/ int Register( char leer_escribir, char *file ) { FILE *fich; char Buffer[10]; if ( leer_escribir == ESCRIBIR ) { if ( (fich = fopen( file, "wb" ) ) == NULL ) return ARCHIVO_INCORRECTO; strcpy( Buffer, "PRN_CNT->\0" ); fwrite( Buffer, sizeof( char ), 10, fich ); fwrite( Salida, sizeof( char ), 10, fich ); fclose(fich); } else { if ( (fich = fopen( file, "rb" ) ) == NULL ) return NO_ARCHIVO; fread( Buffer, sizeof( char ), 10, fich ); if ( strncmp( Buffer, "PRN_CNT->", 9 ) != 0 ) { fclose( fich ); return ARCHIVO_INCORRECTO; } fread( Salida, sizeof( char ), 10, fich ); fclose(fich); } return OK; } /**************************************************************************\ | | | Resetea todas las salidas y las pone a cero. | | | \**************************************************************************/ //Û void Reset_all(void) { char ampl; outportb( 888, 0x00 ); outportb( 890, 0x03 ); for ( ampl=0; ampl<10; ampl++ ) Escribe_Salidas( ampl, NULL, 0 ); outportb( 888, 0x00 ); outportb( 890, 0x03 ); } /**************************************************************************\ | | | Obtiene el estado de las salidas ( On/Off ), de la ampliacion AMPL. | | | | Parametros de Entrada: | | ampl Ampliaci¢n donde debemos mirar | | estados Puntero a estructura con los estados | | | | Parametros de Salida: | | char 1Byte con los estados | | | | NOTA: si &estados es NULL, solo se devolvera el char de salida. | \**************************************************************************/ //Û unsigned char Lee_Salidas( char ampl, ESTADOS *estados ) { char desp; if ( ampl < 1 || ampl > 9 ) return 0; if ( estados != NULL ) { estados -> Salida0 = ( ( Salida[ ampl - 1 ] & 0x01 ) != 0 ); estados -> Salida1 = ( ( Salida[ ampl - 1 ] & 0x02 ) != 0 ); estados -> Salida2 = ( ( Salida[ ampl - 1 ] & 0x04 ) != 0 ); estados -> Salida3 = ( ( Salida[ ampl - 1 ] & 0x08 ) != 0 ); estados -> Salida4 = ( ( Salida[ ampl - 1 ] & 0x10 ) != 0 ); estados -> Salida5 = ( ( Salida[ ampl - 1 ] & 0x20 ) != 0 ); estados -> Salida6 = ( ( Salida[ ampl - 1 ] & 0x40 ) != 0 ); estados -> Salida7 = ( ( Salida[ ampl - 1 ] & 0x80 ) != 0 ); } return Salida[ ampl - 1 ]; } /**************************************************************************\ | | | Obtiene el estado de una salida ( On/Off ), cualquiera. | | | | Parametros de Entrada: | | salida ( Si 1 ampl. 0 a 7 ) | | ( Si 10 ampl. 0 a 80 ) | | | | Parametros de Salida: | | char 1Byte con el estado | | | \**************************************************************************/ //Û unsigned char Lee_Salida( char salida ) { // estados -> Salida0 = ( ( Salida[ ampl - 1 ] & 0x01 ) != 0 ); return ( ( Salida[ (salida / 8) ] & ( 0x01 << ( salida - ( (salida/8)*8 ) ) ) ) != 0 ); } /**************************************************************************\ | | | Escribe la variable 'estados' ( On/Off ), en la ampliacion AMPL. | | | | Parametros de Entrada: | | ampl Ampliaci¢n donde debemos mirar | | estados Puntero a estructura con los estados | | estadosb 1byte con los estados | | | | Parametros de Salida: | | char 1Byte con los estados | | | | NOTA: si &estados es NULL, solo se utilizar  la variable estadosb. | \**************************************************************************/ //Û unsigned char Escribe_Salidas( char ampl, ESTADOS *estados, char estadosb ) { char byte_out=0, cont; if ( estados == NULL ) { outportb( Port_Address[Lpt_usado-1], estadosb ); Salida[ ampl - 1 ] = estadosb; } else { byte_out = estados -> Salida0; byte_out = ( byte_out | ( ( estados -> Salida1 ) << 1) ); byte_out = ( byte_out | ( ( estados -> Salida1 ) << 2) ); byte_out = ( byte_out | ( ( estados -> Salida1 ) << 3) ); byte_out = ( byte_out | ( ( estados -> Salida1 ) << 4) ); byte_out = ( byte_out | ( ( estados -> Salida1 ) << 5) ); byte_out = ( byte_out | ( ( estados -> Salida1 ) << 6) ); byte_out = ( byte_out | ( ( estados -> Salida1 ) << 7) ); outportb( Port_Address[Lpt_usado-1], byte_out ); Salida[ ampl - 1] = byte_out; } outportb( Port_Address[Lpt_usado-1] + 2, ( 0x00 | ( ( ampl & 0x01 ) << 2) | ( !( ampl & 0x02 ) << 1) | ( !( ampl & 0x03 ) << 0) ) ); outportb( Port_Address[Lpt_usado-1] , 0x00 ); outportb( Port_Address[Lpt_usado-1] + 2, 0x03 ); return byte_out; } /**************************************************************************\ | | | Escribe el estado de una salida ( On/Off ), cualquiera. | | | | Parametros de Entrada: | | salida ( Si 1 ampl. 0 a 3 ) | | ( Si 10 ampl. 0 a 80 ) | | | | Parametros de Salida: | | char 1Byte con el estado | | | \**************************************************************************/ //Û unsigned char Escribe_Salida( char salida, char estado ) { char ampl = (salida / 8) + 1; char byte_out; byte_out = Lee_Salidas( ampl, NULL ); byte_out = ( byte_out & ~( 0x01 << ( salida - (ampl-1)*8 ) ) ) | ( estado << ( salida - (ampl-1)*8 ) ); Escribe_Salidas( ampl, NULL, byte_out ); return byte_out; } /**************************************************************************\ | | | Lee las entradas y la almacena en la variable estados. | | | | Parametros de Entrada: | | ampl Ampliaci¢n donde debemos mirar | | estados Puntero a estructura con los estados | | | | Parametros de Salida: | | char 1Byte con los estados | | | \**************************************************************************/ unsigned char Lee_Entradas( char ampl, ESTADOS *estados ) { unsigned char byte_out=0; outportb( Port_Address[Lpt_usado-1] , 0xFF ); outportb( Port_Address[Lpt_usado-1] + 2, ( 0x08 | ( ( ampl & 0x01 ) << 2) | ( !( ampl & 0x02 ) << 1) | ( !( ampl & 0x03 ) << 0) ) ); byte_out = inportb ( Port_Address[Lpt_usado-1] ); outportb( Port_Address[Lpt_usado-1] , 0x00 ); outportb( Port_Address[Lpt_usado-1] + 2, 0x03 ); if ( estados != NULL ) { estados -> Salida0 = ( 0x01 & byte_out ) != 0; estados -> Salida1 = ( 0x02 & byte_out ) != 0; estados -> Salida2 = ( 0x04 & byte_out ) != 0; estados -> Salida3 = ( 0x08 & byte_out ) != 0; estados -> Salida4 = ( 0x10 & byte_out ) != 0; estados -> Salida5 = ( 0x20 & byte_out ) != 0; estados -> Salida6 = ( 0x40 & byte_out ) != 0; estados -> Salida7 = ( 0x80 & byte_out ) != 0; } return byte_out; } /**************************************************************************\ | Lee una entrada cualquiera. | | | | Parametros de Entrada: | | entrada ( Si 1 ampl. 0 a 3 ) | | ( Si 10 ampl. 0 a 80 ) | | | | Parametros de Salida: | | char 1Byte con el estado | | | \**************************************************************************/ unsigned char Lee_Entrada( char entrada ) { char byte_out; char ampl = (entrada / 8) + 1; byte_out = Lee_Entradas( ampl, NULL ); // byte_out = ( byte_out & !( 0x01 << ( entrada - ampl*8 ) ) ) | ( estado << ( entrada - ampl*8 ) ); byte_out = ( ( byte_out & ( 0x01 << ( entrada - (ampl-1)*8 ) ) ) ) != 0; return byte_out; } /*ÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛ*/ /*ÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛFÛIÛNÛÛÛDÛEÛÛÛLÛAÛÛÛÛCÛAÛBÛEÛCÛEÛRÛAÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛ*/ /*ÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛ*/