// sample2.c ...for the PIOD24, compile with BC v3.1 #include #include #include #include #define uint unsigned int #ifndef FALSE #define FALSE 0 #define TRUE !FALSE #endif char Title[] = {" PIOD24 SAMPLE TWO"}; 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"}; #define TitleMessageX 1 #define TitleMessageY 2 #define MessageOneX 1 #define MessageOneY 4 #define NoteMessageX 1 #define NoteMessageY 6 #define PromptSpaceX 1 #define PromptSpaceY 8 #define IntPromptX 1 #define IntPromptY 14 #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); char Register8; void main() { int done = FALSE, x, y; char ch, counter, temp_ch[3], index; char port, IoSetting; uint BaseAddr = 0x300; Register8 = 0; // 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); // prompt the user for the base I/O address gotoxy( PromptSpaceX, PromptSpaceY); clreol(); BaseAddr = AskForBaseAddress( BaseAddr); do{ temp_ch[0] = 0; temp_ch[1] = 0; temp_ch[2] = 0; for( index = PromptSpaceY; index < 25; index++) { gotoxy( 1, index); clreol(); } gotoxy( PromptSpaceX, PromptSpaceY); puts( "press I for interrupt selection\n" "press C for clock source selection\n" "press L for counter link selection\n" "press E to exit." ); ch = getch(); switch( ch) { case 'i' : case 'I' : // display the user's choice puts( "\n\nInterrupt Selection (press a number key):\n"); // prompt the user for the interrupt source puts( "0 No interrupt"); puts( "1 External"); puts( "2 8255 PC0 (digital I/O)"); puts( "3 Counter 0"); puts( "4 Counter 1"); puts( "5 Counter 2"); puts( "6 8255 PC3 (digital I/O)"); temp_ch[0] = getch(); if((temp_ch[0] < 0x30) || (temp_ch[0] > 0x36)) // range check { // on error clear the data input section of the screen puts( "\ninterrupt selection error, press a key to continue"); getch(); gotoxy( 1, PromptSpaceY); for( index = PromptSpaceY; index < 25; index++) { clreol(); puts(""); } break; } Register8 &= 0x1f; Register8 = (temp_ch[0] - 0x30) << 5; outportb( BaseAddr + 8, Register8); // clear the data input section of the screen printf( "\npress a key to continue"); getch(); // hold the display break; case 'c' : case 'C' : // display the user's choice puts( "\nClock Source Selection (press a number key):\n"); // prompt the user for the counter/clock source puts( "0 Counter 0 source = 10MHz"); puts( "1 Counter 0 source = external clock 0"); puts( "2 Counter 1 source = 10MHz"); puts( "3 Counter 1 source = 1MHz"); puts( "4 Counter 2 source = 1MHz"); puts( "5 Counter 2 source = external clock 1"); temp_ch[0] = getch(); if((temp_ch[0] < 0x30) || (temp_ch[0] > 0x35)) // range check { // on error clear the data input section of the screen puts( "\nClock source selection error, press a key to continue"); getch(); gotoxy( 1, PromptSpaceY); for( index = PromptSpaceY; index < 25; index++) { clreol(); puts(""); } break; } switch( temp_ch[0]) { case '0': Register8 &= 0xfb; break; // clear bit 2 case '1': Register8 |= 0x04; break; // set bit 2 case '2': Register8 &= 0xf7; break; // clear bit 3 case '3': Register8 |= 0x08; break; // set bit 3 case '4': Register8 &= 0xef; break; // clear bit 4 case '5': Register8 |= 0x10; break; // set bit 4 } outportb( BaseAddr + 8, Register8); // clear the data input section of the screen printf( "\npress a key to continue"); getch(); // hold the display break; case 'l' : case 'L' : // display the user's choice puts( "\nCounter Link Selection (press a number key):\n"); // prompt the user for the counter to be programmed puts( "0 no links"); puts( "1 Counters 1 and 2 linked "); puts( "2 Counters 0 and 1 and 2 linked"); temp_ch[0] = getch(); if((temp_ch[0] < 0x30) || (temp_ch[0] > 0x32)) // range check { // on error clear the data input section of the screen puts( "\nCounter link selection error, press a key to continue"); getch(); gotoxy( 1, PromptSpaceY); for( index = PromptSpaceY; index < 25; index++) { clreol(); puts(""); } break; } switch( temp_ch[0]) { case '0': Register8 &= 0xfc; // clear bits 0 and 1 break; case '1': Register8 &= 0xfc; // clear bit 1, set bit 0 Register8++; break; case '2': Register8 &= 0xfc; // clear bit 0, set bit 1 Register8 += 2; break; } outportb( BaseAddr + 8, Register8); // clear the data input section of the screen printf( "\npress a key to continue"); getch(); // hold the display break; case 'e' : case 'E' : done = TRUE; break; default: done = FALSE; } } while( !done); clrscr(); } 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 */