/*------------------------------------------------------------------- * DA12-16 C language sample 3. Program illustrates access to * simultaneous and automatic modes. *------------------------------------------------------------------*/ #include #include #include #include /* Provide global access to card's base address */ unsigned base; /*------------------------------------------------------------------- * OutputValue * Program the output value of a specified DA channel *------------------------------------------------------------------*/ void OutputValue(char CH, unsigned VALUE) { outport(base + (CH * 2), VALUE); } /* end OutputValue */ /*------------------------------------------------------------------- * FUNCTION: AskForBaseAddress * PURPOSE: Prompt user to enter base address for their I/O card. -------------------------------------------------------------------*/ unsigned AskForBaseAddress(unsigned int OldOne) { char msg[7]; int NewOne = 0, Success = 0, Dummy; int AddrInputPosX, AddrInputPosY; puts("\nPlease enter the Base Address for your card (in hex)"); printf("or press ENTER for %X.\n>", OldOne); AddrInputPosX = wherex(); AddrInputPosY = wherey(); do { gotoxy(AddrInputPosX, AddrInputPosY); clreol(); msg[0] = 5; msg[1] = 0; cgets(msg); sscanf(msg + 2, "%x", &NewOne); Success = 1; Dummy = NewOne; if (msg[1] == 0) { gotoxy(AddrInputPosX, AddrInputPosY); printf("%X", OldOne); Success = 1; Dummy = OldOne; } } while(!Success); return (Dummy); } /* end of AskForBaseAddress */ /*------------------------------------------------------------------------- * FUNCTION: intro * PURPOSE: Present user with introductory screen. Get base address. -------------------------------------------------------------------------*/ void intro(void) { clrscr(); puts("\n"); puts(" C Sample #3 : SAMPLE3.C"); puts("\n"); puts("This sample program demonstrates both automatic and simultaneous"); puts("update modes of the DA12-16."); puts("\n"); puts("Note: This sample assumes a ñ5 Volt range on all DACs.\n"); puts("\n"); puts("\n"); puts("Press any key to set base address ... "); getch(); clrscr(); base = AskForBaseAddress(0x300); clrscr(); } /* end intro */ /*------------------------------------------------------------------- * Main *------------------------------------------------------------------*/ void main(void) { unsigned Channel = 0, Value = 0; char key = 0; intro(); puts("Should I perform a hardware-reset clear?"); puts("Answer Y if the card was just powered-up,"); puts("or N if you've already done this since the last reset."); do { key = toupper(getch()); } while ((key != 'Y') && (key != 'N')); puts(""); if (key == 'Y') { for (Channel = 0; Channel < 16; Channel++) OutputValue(Channel, 0x800); /* Note: 0x000 if Unipolar Mode (per channel) Be aware that the OutputValue routine does not necessarily update the outputs. In this particular case, it does not. The card powers up in simultaneous Update mode, therefore, the output command is not reflected in the voltages until an update command is issued -- the next line: */ inportb(base + 10); /* This updates the outputs, and exits simultaneous mode. We could have remained in simultaneous mode and still updated the outputs by using base + 8 instead of +10. */ inportb(base + 15); /* Release zero latch */ puts("All of the outputs have been updated with Bipolar zero (0x800)"); puts("Assuming power-on condition, nothing should have changed."); puts("(Incidentally, any channels that are in the Unipolar range"); puts("should have simultaneously changed to +2«V outputs.)"); puts("\n"); } /* if 'Y' */ puts("Press any key to output 2«V on all channels, non-simultaneously"); getch(); puts("\n"); inportb(base + 2); // remove from simultaneous, offset // 10 would also work. for (Channel = 0; Channel < 16; Channel++) OutputValue(Channel,0xC00); // these each take effect immediately. inportb(base + 0); // place in simultaneous mode for (Channel = 0; Channel < 16; Channel++) OutputValue(Channel,0xFFF); // these don't do a thing to // the outputs yet. puts("All channels have been written to with 0xFFF, but the outputs don't"); puts("change. Press any key to initiate simultaneous update of all channels."); puts("Outputs will all change at the same time to 4.9998V DC."); getch(); puts("\n"); inportb(base + 8); // still in simultaneous mode, updated outputs. puts("All channels should have changed to full-scale.\n"); puts("\n"); puts("Press any key to continue ... "); getch(); puts("\n"); // note that we don't issue a Simultaneous Update Mode read command -- we're // still in simultaneous mode from above. for (Channel = 1; Channel <= 7; Channel += 2) OutputValue(Channel, 0x000); // these don't do a thing to // the outputs yet. puts("Channels 1, 3, 5, and 7 have been written to with 0x000, but"); puts("the outputs haven't changed."); puts("Press any key to change channels 1 through 7 odd to -5V, simultaneously."); getch(); puts("\n"); inportb(base + 10); // update and remove from simultaneous mode. puts("The channels should have changed now."); puts("\n"); puts("Press any key to continue with program."); puts("\n"); do { clrscr(); puts("The program will now allow you to type in any count value desired"); puts("for output on any channel desired.\n"); puts("Enter ChannelNumber Space HexCounts Ex: 15 A00 [enter]\n"); gotoxy(2, 6); cputs("> "); // just clear out the last value. do { if (Channel >= 16) puts("Channel must be less than 16."); gotoxy(4, 6); scanf("%i %x", &Channel, &Value); } while (Channel >= 16); OutputValue(Channel, Value); // this happens immediately, not // in simultaneous mode puts("Press [ENTER] to Exit Program, or any other key to continue."); } while(getch() != 13); // Program Complete. Outputs remain the same. } /* end main */