(**************************************************************************** * Pascal SAMPLE #1: SAMPLE1.PAS * * * * This sample will prompt the user for a voltage between 0 and 10 volts, * * then calculate the actual voltage based on the resolution of the DAC * * and output the voltage to the desired DAC channel. * * * * The following setup of the board is expected: * * * * þ All DAC voltage ranges should be set to 0v-10v unipolar. * * * * LAST MODIFICATION: 2/5/98 SMG * * * ****************************************************************************) Program Sample1; Uses Crt; Var Base, EBase : Word; KeyEntered : Char; (**************************************************************************** * * * FUNCTION: Cal * * * * PURPOSE: Calibrate * * * * INPUT: Value and channel * * * * OUTPUT: Calibrated value * * * ****************************************************************************) Function Cal(InValue: Word; Ch: Word) : Word; var a, b: byte; OutValue: Word; begin a:=Port[EBase+((ch*2)+(Port[EBase+240+ch]*32))+1]; b:=Port[EBase+((ch*2)+(Port[EBase+240+ch]*32))]; OutValue:=Round(((4095.0-a-b)/4095.0)*InValue+b); Cal:=OutValue; end; (**************************************************************************** * * * FUNCTION: write_DAC - local routine * * * * PURPOSE: Prompts the user for DAC number and voltage, then calculates * * the actual output voltage based on resolution, displays it * * and writes the value to the DAC. * * * * INPUT: None. * * * * CALLS: None. * * * * OUTPUT: None. * * * ****************************************************************************) Procedure write_DAC; var volt_value_entered,volt_value_expected : Real; counts_entered,dac_number : Word; begin (* prompt for the DAC number and desired voltage. *) Write('Enter the DAC number (0 through 15 only): '); Readln(dac_number); Writeln; Writeln; Write('Enter a voltage between 0.000v to 9.997v: '); Readln(volt_value_entered); Writeln; Writeln; volt_value_entered := volt_value_entered / 0.002442; (* compute the digital output needed for this value and the actual voltage that will be expected, and display the voltage. *) counts_entered:= trunc(volt_value_entered); volt_value_expected := counts_entered * 0.002442; Writeln('Due to the 12-bit resolution of the DAC, you should expect'); Writeln('to see a voltage of ',volt_value_expected:4:3,'.'); (* prepare the control word and write to the DAC *) portw[Base+(2 * dac_number)] := Cal(counts_entered, dac_number); (* write low byte *) end; (* write_DAC *) (**************************************************************************** * * * FUNCTION: Deci * * * * PURPOSE: Convert a decimal string into a hex word. * * * * INPUT: A decimal string. * * * * CALLS: None. * * * * OUTPUT: Hexidecimal word. * * * ****************************************************************************) Function Deci(DS : String) : Word; var BS : String; Er : Integer; DI : Word; begin BS := '$' + DS; Val(BS, DI, Er); Deci := DI; end; (**************************************************************************** * * * FUNCTION: Hex * * * * PURPOSE: Convert a hex word into a hex string. * * * * INPUT: Hex word. * * * * CALLS: None. * * * * OUTPUT: Hex string. * * * ****************************************************************************) Function Hex(BB : Word) : String; var AA, CC : Byte; DD : String; HexTable : String; begin HexTable := '0123456789ABCDEF'; DD := '000'; DD[3] := HexTable[(BB AND $00F) + 1]; DD[2] := HexTable[((BB AND $0F0) SHR 4) + 1]; DD[1] := HexTable[((BB AND $F00) SHR 8) + 1]; Hex := DD; end; (**************************************************************************** * * * FUNCTION: AskForBaseAddress * * * * PURPOSE: Prompt user for card base address. * * * * INPUT: None. * * * * CALLS: Deci * * Hex * * * * OUTPUT: None. * * * ****************************************************************************) function AskForBaseAddress(OldOne : String) : Word; const Msg : string[4] = '0'; var NewOne, Success, Dummy, Error : Word; AddrInputPosX, AddrInputPosY : Word; begin if (OldOne = 'OLD') then OldOne := Msg; WriteLn('Please enter the Base Address (0000-FFFF) for your card (in hex)'); WriteLn('or press ENTER for ', OldOne, '. '); Write('>'); AddrInputPosX := WhereX; AddrInputPosY := WhereY; repeat GotoXY(AddrInputPosX, AddrInputPosY); ClrEol; Readln(Msg); Val('$' + Msg, NewOne, Error); if (error=0) then begin Success := 1; Dummy := NewOne; end else if (Msg = '') then begin GotoXY(AddrInputPosX, AddrInputPosY); WriteLn(OldOne); Msg := OldOne; Success := 1; Val('$' + Msg, Dummy, Error); end; until (Success = 1); AskForBaseAddress := Dummy; end; { end of AskForBaseAddress } function AskForCalBaseAddress(OldOne : String) : Word; const Msg : string[4] = '0'; var NewOne, Success, Dummy, Error : Word; AddrInputPosX, AddrInputPosY : Word; begin if (OldOne = 'OLD') then OldOne := Msg; WriteLn('Please enter the Calibration Base Address (0000-FFFF) for your card (in hex)'); Write('>'); AddrInputPosX := WhereX; AddrInputPosY := WhereY; repeat GotoXY(AddrInputPosX, AddrInputPosY); ClrEol; Readln(Msg); Val('$' + Msg, NewOne, Error); if (error=0) then begin Success := 1; Dummy := NewOne; end else if (Msg = '') then begin GotoXY(AddrInputPosX, AddrInputPosY); WriteLn(OldOne); Msg := OldOne; Success := 1; Val('$' + Msg, Dummy, Error); end; until (Success = 1); AskForCalBaseAddress := Dummy; end; { end of AskForBaseAddress } (**************************************************************************** * * * FUNCTION: Intro * * * * PURPOSE: Display introductory information and prompt for base address. * * * * INPUT: None. * * * * CALLS: AskForBaseAddress * * * * OUTPUT: None. * * * ****************************************************************************) Procedure Intro; begin ClrScr; Writeln(' Pascal SAMPLE #1: SAMPLE1.PAS '); Writeln(' '); Writeln(' This sample will prompt the user for a voltage between 0 and 10 volts, '); Writeln(' then calculate the actual voltage based on the resolution of the DAC and'); Writeln(' output the voltage to the desired DAC channel. '); Writeln; Writeln(' The following setup of the board is expected: '); Writeln; Writeln(' þ All DAC voltage ranges should be set to 0v-10v unipolar. '); Writeln; Writeln; Writeln('Press ENTER to set base addresses.'); Readln; ClrScr; Base := AskForBaseAddress( '$340' ); Writeln; EBase := AskForCalBaseAddress('$340'); ClrScr; end; (* of Intro *) (**************************************************************************** * * * FUNCTION: main program - global routine * * * * PURPOSE: Controls overall program execution and determines exit. * * * * INPUT: None. * * * * CALLS: None. * * * * OUTPUT: None. * * * ****************************************************************************) Var Channel : Word; PortVal : Byte; Begin Intro; (* Set all sixteen buffers to 0 *) for Channel := 0 to 16 Do PortW[Base + (Channel * 2)] := Cal(0, Channel); PortVal := Port[Base+10]; { place in automatic update mode } PortVal := Port[Base+15]; { release zero latch } repeat ClrScr; write_DAC; Writeln; Writeln; Write('Would you like to output another value (Y or N)? '); Read(KeyEntered); ClrScr; until ((KeyEntered = 'N') or (KeyEntered = 'n')); { Set all sixteen buffers to 0 } for Channel := 0 to 16 Do PortW[Base + (Channel * 2)] := Cal(0, Channel); Writeln; Writeln('DA12-16 sample #1 complete.'); End. (* main *)