(**************************************************************************** * 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: * * -- Reference voltage for the DACs should be -10V. * * * ****************************************************************************) program sample1; uses crt; var key_entered:char; 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: 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(Address:word); var volt_value_expected,volt_value_entered :real; counts_entered,temp,dac_number :word; lo_byte,hi_byte :byte; ch :char; code: integer; begin (* prompt for the DAC number and desired voltage. *) WriteLn;WriteLn; repeat GotoXY (1,3); ClrEol; Write('Enter the DAC number (0 or 1 only): '); ch:= ReadKey; WriteLn(ch); val(ch,dac_number,code); until (dac_number=0) or (dac_number=1); WriteLn;WriteLn;WriteLn; repeat GotoXY (1,7); ClrEol; Write('Enter a voltage between 0.000v to 9.997v: '); ReadLn(volt_value_entered); until (volt_value_entered>=0) and (volt_value_entered<=9.997); WriteLn;WriteLn; volt_value_entered := volt_value_entered / 0.002441; (* 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.002441; 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 *) temp := counts_entered; counts_entered := counts_entered div 256; (* extract hi byte *) hi_byte := counts_entered; (* convert to byte *) temp := temp mod 256; (* extract low byte *) lo_byte := temp; (* convert to byte *) port[Address+8+(2 * dac_number)] := lo_byte; (* Write low byte *) port[Address+8+(2*dac_number+1)] := hi_byte; (* Write high byte *) end; (* write_DAC *) (**************************************************************************** * * * FUNCTION: main program - global routine * * * * PURPOSE: Controls overall program execution and determines exit. * * * * INPUT: None. * * * * CALLS: None. * * * * OUTPUT: None. * * * ****************************************************************************) var Address:word; 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(' Board Configuration: '); WriteLn; WriteLn(' -- The Base Address should be set to the address which will'); WriteLn(' be entered during program execution.'); WriteLn(' -- DACs should be in the 10V unipolar mode.'); WriteLn(' -- The 2''s complement jumper should be removed.'); WriteLn(' -- All remaining jumper settings are irrelevant'); WriteLn; WriteLn; WriteLn; Address := AskForBaseAddress('340'); Port[Address+8] := 0; { set DAC0 to 0 volts } Port[Address+9] := 0; Port[Address+$0A] := 0; { set DAC1 to 0 volts } Port[Address+$0B] := 0; WriteLn; repeat ClrScr; write_DAC(Address); WriteLn;WriteLn; Write('Would you like to output another value (Y or N)? '); ReadLn(key_entered); until ((key_entered = 'N') or (key_entered = 'n')); ClrScr; end. (* main *)