// sample1.c #include #include #include #include #define uint unsigned int #ifndef FALSE #define FALSE 0 #define TRUE !FALSE #endif char Title[] = {" PIOD24 SAMPLE ONE"}; char Info[] = {" This program initializes the PIOD24 control register at offset 8"}; char Note[] = {" note: the client driver for this device must be installed first"}; char Line[] = {"----------------------------------------------------------------------"}; #define TitleMessageX 1 #define TitleMessageY 2 #define MessageOneX 1 #define MessageOneY 4 #define NoteMessageX 1 #define NoteMessageY 6 #define LineX 1 #define LineY 8 #define PortADirX 10 #define PortADirY 10 #define PortBDirX 36 #define PortBDirY 10 #define PortCDirX 64 #define PortCDirY 10 #define PortATextX 1 #define PortATextY 12 #define PortBTextX 27 #define PortBTextY 12 #define PortCTextX 55 #define PortCTextY 12 #define PromptSpaceX 1 #define PromptSpaceY 15 #define uint unsigned int uint AskForCount( unsigned int); uint AskForBaseAddress( unsigned int); uint AskForValue( uint); void CtrMode( uint, char, char); void CtrLoad( uint, int, int); void IoMode( uint, char); void IoPortOut( uint, char, char); char IoPortIn( uint, char); void HexToBinDisp( char); void main() { int done = FALSE, x, y; char ch, counter, temp_ch[3], index; char port, mode = 0x9b, IoSetting, value = 1; uint count = 1, BaseAddr = 0x300; char ValuePortA, ValuePortB, ValuePortC; char IoSettingA, IoSettingB, IoSettingC; // display the title, info, notes, etc. clrscr(); gotoxy( TitleMessageX, TitleMessageY); clreol(); printf( "%s", Title); gotoxy( MessageOneX, MessageOneY); clreol(); printf( "%s", Info); gotoxy( NoteMessageX, NoteMessageY); clreol(); printf( "%s", Note); gotoxy( PortATextX, PortATextY); clreol(); printf( "Port A"); gotoxy( PortBTextX, PortBTextY); clreol(); printf( "Port B"); gotoxy( PortCTextX, PortCTextY); clreol(); printf( "Port C"); gotoxy( LineX, LineY); clreol(); printf( "%s", Line); do{ temp_ch[0] = 0; temp_ch[1] = 0; temp_ch[2] = 0; gotoxy( PromptSpaceX, PromptSpaceY); clreol(); puts( "press T for timer/counter or D for digital I/O or E to exit." ); ch = getch(); switch( ch) { case 't' : case 'T' : clreol(); puts( "timer/counter"); // display the user's choice // prompt the user for the counter to be programmed printf( "select a counter: press 0, 1, or 2." ); temp_ch[0] = getch(); if( (temp_ch[0] != '2') && // range check (temp_ch[0] != '1') && (temp_ch[0] != '0')) { // on error clear the data input section of the screen puts( "\ncounter selection error, press a key to continue"); getch(); gotoxy( 1, PromptSpaceY); for( index = PromptSpaceY; index < 25; index++) { clreol(); puts( ""); } break; } // display the user's input printf( " --- counter %s ---\n", temp_ch); counter = atoi( temp_ch); // prompt the user for the counter mode printf( "select the counter mode: press 0, 1, 2, 3, 4 or 5." ); temp_ch[0] = getch(); if( (temp_ch[0] != '5') && // range check (temp_ch[0] != '4') && (temp_ch[0] != '3') && (temp_ch[0] != '2') && (temp_ch[0] != '1') && (temp_ch[0] != '0')) { // on error clear the data input section of the screen puts( "\nmode selection error, press a key to continue"); getch(); gotoxy( 1, PromptSpaceY); for( index = PromptSpaceY; index < 25; index++) { clreol(); puts( ""); } break; } printf( " --- mode %s ---\n", temp_ch); // display the user's input mode = atoi( temp_ch); // prompt the user for the divisor puts( "type a divisor in hex: 0000 thru ffff" ); count = AskForCount( count); // prompt the user for the base I/O address puts(""); BaseAddr = AskForBaseAddress( BaseAddr); // program the counter's mode CtrMode( BaseAddr, counter, mode); // program the counter's divisor CtrLoad( BaseAddr, counter, count); // clear the data input section of the screen printf( "\npress a key to continue"); getch(); // hold the display // clear the data input section of the screen gotoxy( 1, PromptSpaceY); for( index = PromptSpaceY; index < 25; index++) { clreol(); puts( ""); } break; case 'd' : case 'D' : clreol(); puts( "digital I/O"); // display the user's choice // prompt the user for the base I/O address BaseAddr = AskForBaseAddress( BaseAddr); //prompt the user for a port printf( "\nselect a port: press A, B, or C." ); port = getch(); port &= 0xdf; // toupper if( port != 'A' && port != 'B' && port != 'C') // range check { // on error clear the data input section of the screen puts( "\nport selection error, press a key to continue"); getch(); gotoxy( 1, PromptSpaceY); // clear the input section of the display for( index = PromptSpaceY; index < 25; index++) { clreol(); puts( ""); } break; } printf( " --- %c ---\n", port);// user's input // prompt the user for input or output printf( "press I or O to set this port for input or output." ); IoSetting = getch(); IoSetting &= 0xdf; // toupper if( IoSetting != 'I' && IoSetting != 'O') { // on error clear the data input section of the screen puts( "\nI/O setting error, press a key to continue"); getch(); gotoxy( 1, PromptSpaceY); // clear the input section of the display for( index = PromptSpaceY; index < 25; index++) { clreol(); puts( ""); } break; } printf( " --- %c ---\n", IoSetting); // user's input // prompt the user for a value to output if( IoSetting == 'O') { puts( "type a hex number for output: 0 thru FF"); value = AskForValue( value); puts( " note: other ports will 'glitch'"); } x = wherex(); y = wherey(); if( port == 'A') { ValuePortA = value; IoSettingA = IoSetting; if( IoSetting == 'I') { mode |= 0x10; // set bit 4 IoMode( BaseAddr, mode); gotoxy( PortADirX, PortADirY); printf( "Input "); // must be 6 char gotoxy( PortATextX, PortATextY); printf( "Port A = "); HexToBinDisp( IoPortIn( BaseAddr, 0)); } else if( IoSetting == 'O') { mode &= 0xef; // clr bit 4 IoMode( BaseAddr, mode); IoPortOut( BaseAddr, 0, value); gotoxy( PortADirX, PortADirY); printf( "Output"); gotoxy( PortATextX, PortATextY); printf( "Port A = "); HexToBinDisp( value); } if( IoSettingB == 'O') IoPortOut( BaseAddr, 1, ValuePortB); if( IoSettingC == 'O') IoPortOut( BaseAddr, 2, ValuePortC); } else if( port == 'B') { ValuePortB = value; IoSettingB = IoSetting; if( IoSetting == 'I') { mode |= 0x02; // set bit 1 IoMode( BaseAddr, mode); gotoxy( PortBDirX, PortBDirY); printf( "Input "); // must be 6 char gotoxy( PortBTextX, PortBTextY); printf( "Port B = "); HexToBinDisp( IoPortIn( BaseAddr, 1)); } else if( IoSetting == 'O') { mode &= 0xfd; // clr bit 1 IoMode( BaseAddr, mode); IoPortOut( BaseAddr, 1, value); gotoxy( PortBDirX, PortBDirY); printf( "Output"); gotoxy( PortBTextX, PortBTextY); printf( "Port B = "); HexToBinDisp( value); } if( IoSettingA == 'O') IoPortOut( BaseAddr, 0, ValuePortA); if( IoSettingC == 'O') IoPortOut( BaseAddr, 2, ValuePortC); } if( port == 'C') { ValuePortC = value; IoSettingC = IoSetting; if( IoSetting == 'I') { mode |= 0x09; // set bits 3 and 0 IoMode( BaseAddr, mode); gotoxy( PortCDirX, PortCDirY); printf( "Input "); // must be 6 char gotoxy( PortCTextX, PortCTextY); printf( "Port C = "); HexToBinDisp( IoPortIn( BaseAddr, 2)); } else if( IoSetting == 'O') { mode &= 0xf6; // clr bits 3 and 0 IoMode( BaseAddr, mode); IoPortOut( BaseAddr, 2, value); gotoxy( PortCDirX, PortCDirY); printf( "Output"); gotoxy( PortCTextX, PortCTextY); printf( "Port C = "); HexToBinDisp( value); } if( IoSettingA == 'O') IoPortOut( BaseAddr, 0, ValuePortA); if( IoSettingB == 'O') IoPortOut( BaseAddr, 1, ValuePortB); } gotoxy( x, y); x= x; y=y; puts( "press a key to continue"); getch(); // clear the data input section of the screen gotoxy( 1, PromptSpaceY); for( index = PromptSpaceY; index < 25; index++) { clreol(); puts( ""); } break; case 'e' : case 'E' : done = TRUE; break; default: done = FALSE; } } while( !done); clrscr(); } void CtrMode(uint addr, char cntr, char mode) { int ctrl; addr += 4; ctrl = (cntr << 6) | 0x30 | (mode << 1); outportb(addr+3,ctrl); } void CtrLoad(uint addr ,int c,int val) { outportb(addr+4+c,val & 0x00FF); outportb(addr+4+c,(val>>8) & 0x00FF); } uint CtrRead(uint addr , int c) { outportb(addr+4+3,c<<6); return inportb (addr+4+c) + (inportb(addr+c) << 8); } unsigned AskForCount(unsigned int OldOne) { char msg[8]; int NewOne = 0, Success = 0, Dummy; int AddrInputPosX, AddrInputPosY; AddrInputPosX = wherex(); AddrInputPosY = wherey(); do { gotoxy(AddrInputPosX, AddrInputPosY); clreol(); msg[0] = 5; msg[1] = 0; cgets(msg); Success = sscanf(msg + 2, "%x", &NewOne); if( Success) { Dummy = NewOne; if (msg[1] == 0) { gotoxy(AddrInputPosX, AddrInputPosY); printf("%X", OldOne); Success = 1; Dummy = OldOne; } } } while(!Success); return (Dummy); } unsigned AskForBaseAddress(unsigned int OldOne) { char msg[6]; int NewOne = 0, Success = 0, Dummy; int AddrInputPosX, AddrInputPosY; puts("Please enter the Base Address (100-3F8) for your card (in hex)"); printf("or press ENTER for %X.\n>", OldOne); AddrInputPosX = wherex(); AddrInputPosY = wherey(); do { gotoxy(AddrInputPosX, AddrInputPosY); clreol(); msg[0] = 4; msg[1] = 0; cgets(msg); sscanf(msg + 2, "%x", &NewOne); if ((NewOne <= 0x3f8) && (NewOne >= 0x100)) { Success = 1; Dummy = NewOne; } else if (msg[1] == 0) { gotoxy(AddrInputPosX, AddrInputPosY); printf("%X", OldOne); Success = 1; Dummy = OldOne; } } while(!Success); return (Dummy); } /* end of AskForBaseAddress */ unsigned AskForValue( unsigned int OldOne) { char msg[6]; int NewOne = 0, Success = 0, Dummy; int AddrInputPosX, AddrInputPosY; AddrInputPosX = wherex(); AddrInputPosY = wherey(); do { gotoxy(AddrInputPosX, AddrInputPosY); clreol(); msg[0] = 3; msg[1] = 0; cgets(msg); Success = sscanf(msg + 2, "%x", &NewOne); if( Success) { Dummy = NewOne; if (msg[1] == 0) { gotoxy(AddrInputPosX, AddrInputPosY); printf("%X", OldOne); Success = 1; Dummy = OldOne; } } } while(!Success); return (Dummy); } void IoMode( uint BaseAddress, char mode) { outportb( BaseAddress + 3, mode); } void IoPortOut( uint BaseAddress, char PortNumber, char Value) { outportb( BaseAddress + PortNumber, Value); } char IoPortIn( uint BaseAddress, char PortNumber) { return inportb( BaseAddress + PortNumber); } void HexToBinDisp( char number) { printf( "%i ", (number >> 7) & 0x01); printf( "%i ", (number >> 6) & 0x01); printf( "%i ", (number >> 5) & 0x01); printf( "%i ", (number >> 4) & 0x01); printf( "%i ", (number >> 3) & 0x01); printf( "%i ", (number >> 2) & 0x01); printf( "%i ", (number >> 1) & 0x01); printf( "%i ", number & 0x01); }