{ sample1.pas } Uses Strings, crt; Const True = 1; False = 0; TitleMessageX = 1; TitleMessageY = 2; MessageOneX = 1; MessageOneY = 4; NoteMessageX = 1; NoteMessageY = 5; Var Title: string[79]; Info: string[79]; Note: string[79]; Line: string[79]; procedure CtrMode( addr: integer; cntr: byte; mode: byte); Var ctrl: integer; begin addr := addr + 7; ctrl := ctrl shl 6; ctrl := ctrl or $30; mode := mode shl 1; ctrl := ctrl or mode; port[ addr] := ctrl; end; procedure CtrLoad( addr: integer; cntr: integer; value: integer); begin port[ addr + 4 + cntr] := value and $00ff; value := value shr 8; port[ addr + 4 + cntr] := value and $00ff; end; function CtrRead( addr: integer; cntr: integer): integer; Var temp1: integer; temp2: integer; begin temp1 := cntr shl 6; port[ addr + 7] := temp1; temp1 := port[ addr + cntr + 4] shl 8; temp2 := port[ addr + cntr + 4]; CtrRead := temp1 + temp2; end; function AskForBaseAddress(OldOne : String) : Word; const Msg : string[3] = '0'; var NewOne, Success, Dummy, Error : Word; AddrInputPosX, AddrInputPosY : Word; begin if (OldOne = 'OLD') then OldOne := Msg; AddrInputPosX := WhereX; AddrInputPosY := WhereY; Success := 0; repeat GotoXY(AddrInputPosX, AddrInputPosY); ClrEol; Readln(Msg); Val('$' + Msg, NewOne, Error); if ((NewOne <= $3F8) AND (NewOne >= $100)) 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; function AskForCount(OldOne : String) : integer; const Msg : string[3] = '0'; var NewOne, Success, Dummy, Error : Word; CountInputPosX, CountInputPosY : Word; begin if (OldOne = 'OLD') then OldOne := Msg; CountInputPosX := WhereX; CountInputPosY := WhereY; Success := 0; repeat GotoXY(CountInputPosX, CountInputPosY); ClrEol; Readln(Msg); Val('$' + Msg, NewOne, Error); if ((NewOne <= $3F8) AND (NewOne >= $100)) then begin Success := 1; Dummy := NewOne; end else if (Msg = '') then begin GotoXY(CountInputPosX, CountInputPosY); WriteLn(OldOne); Msg := OldOne; Success := 1; Val('$' + Msg, Dummy, Error); end; until (Success = 1); AskForCount := Dummy; end; function AskForValue(OldOne : String) : byte; const Msg : string[3] = '0'; var NewOne, Success, Dummy, Error : Word; ValueInputPosX, ValueInputPosY : Word; begin if (OldOne = 'OLD') then OldOne := Msg; ValueInputPosX := WhereX; ValueInputPosY := WhereY; Success := 0; repeat GotoXY(ValueInputPosX, ValueInputPosY); ClrEol; Readln(Msg); Val('$' + Msg, NewOne, Error); if ((NewOne < $100)) then begin Success := 1; Dummy := NewOne; end else if (Msg = '') then begin GotoXY(ValueInputPosX, ValueInputPosY); WriteLn(OldOne); Msg := OldOne; Success := 1; Val('$' + Msg, Dummy, Error); end; until (Success = 1); AskForValue := Dummy; end; procedure IoMode( BaseAddress: integer; mode: byte); begin port[ BaseAddress + 3] := mode; end; procedure IoPortOut( BaseAddress: integer; PortNumber: byte; Value: byte); begin port[ BaseAddress + PortNumber] := Value; end; function IoPortIn( BaseAddress: integer; PortNumber: byte): byte; begin IoPortIn := port[ BaseAddress + PortNumber]; end; procedure HexToBinDisp( number: byte); var temp: byte; begin temp := number; temp := temp shr 7; temp := temp and $01; write( temp); temp := number; temp := temp shr 6; temp := temp and $01; write( temp); temp := number; temp := temp shr 5; temp := temp and $01; write( temp); temp := number; temp := temp shr 4; temp := temp and $01; write( temp); temp := number; temp := temp shr 3; temp := temp and $01; write( temp); temp := number; temp := temp shr 2; temp := temp and $01; write( temp); temp := number; temp := temp shr 1; temp := temp and $01; write( temp); temp := number; temp := temp shr 0; temp := temp and $01; write( temp); end; Var done, x, y, count, BaseAddr: integer; value, counter, mode, index: byte; ch, IoPort, IoSetting: char; temp_ch: array[0..3] of char; PortADirX, PortADirY: byte; PortBDirX, PortBDirY: byte; PortCDirX, PortCDirY: byte; TempX, TempY: byte; PortAValueX, PortAValueY: byte; PortBValueX, PortBValueY: byte; PortCValueX, PortCValueY: byte; PromptSpaceX, PromptSpaceY: byte; DirPortA, DirPortB, DirPortC: char; ValuePortA, ValuePortB, ValuePortC: byte; begin done := FALSE; mode := $9b; value := 1; count := 1; BaseAddr := $300; ClrScr; Writeln( ' PIOD24 SAMPLE ONE'); Writeln( ''); Writeln( ' This program controls the PIOD24 I/O and Timer Functions'); Writeln( ' note: the client driver for this device must be installed first'); Writeln( ''); Writeln( ' --------------------------------------------------------------'); Writeln( ''); { port direction info goes here } PortADirY := WhereY; PortBDirY := WhereY; PortCDirY := WhereY; Writeln( ''); Writeln( ''); PortAValueY := WhereY; PortBValueY := WhereY; PortCValueY := WhereY; Write( 'Port A '); PortAValueX := WhereX; { Used to place } Write( ' Port B '); PortBValueX := WhereX; { I/O direction } Write( ' Port C '); PortCValueX := WhereX; { information. } Writeln( ''); Writeln( ''); PromptSpaceX := WhereX; PromptSpaceY := WhereY; { prompts start here } repeat index := PromptSpaceY; repeat GotoXY( 1, index); clreol; index := index + 1; until index = 25; GotoXY( PromptSpaceX, PromptSpaceY); Writeln( 'press T for timer/counter or D for digital I/O or E to exit.' ); ch := ReadKey; case ch of 't', 'T': begin WriteLn( 'timer/counter'); { display the user's choice } { prompt the user for the counter to be programmed } Write( 'select a counter: press 0, 1, or 2. '); ch := ReadKey; if (ch <> '0') and (ch <> '1') and (ch <> '2') then { range check } begin Writeln(''); Write( 'counter selection error, press a key to continue'); ReadKey; end else begin WriteLn( ' --- counter '+ ch + ' ---'); { display input } Counter := byte(ch) - $30; { ascii to hex } { prompt the user for the counter mode } Write( 'select the counter mode: press 0, 1, 2, 3, 4 or 5.'); ch := ReadKey; if (ch < '0') or (ch > '5') then { range check } begin { on error clear the data input section of the screen } Writeln(''); WriteLn( 'mode selection error, press a key to continue'); ReadKey; end else begin WriteLn( ' --- mode ' + ch + ' ---'); mode := byte(ch) - $30; { ascii to hex } end; { prompt the user for the divisor } WriteLn( 'type a divisor in hex: 0 thru ffff'); count := AskForCount( '1'); { prompt the user for the base I/O address } BaseAddr := AskForBaseAddress( '300'); { program the counter's mode } CtrMode( BaseAddr, counter, mode); { program the counter's divisor } CtrLoad( BaseAddr, counter, count); { ask the user for permission to continue } Write( 'press a key to continue'); end; end; 'd', 'D': begin { display the user's choice } WriteLn( 'digital I/O'); { prompt the user for the base I/O address } WriteLn( 'type the board''s base I/O address:'); BaseAddr := AskForBaseAddress( '300'); { prompt the user for a port } WriteLn( 'select a port: press A, B, or C.'); IoPort := ReadKey; byte(IoPort) := byte(IoPort) and $df; { to upper case } if (IoPort < 'A') or (IoPort > 'C') then { range check } begin Write( 'port selection error, press a key to continue'); ReadKey; end else begin { prompt the user for input or output } WriteLn( 'press I or O to set this port for input or output.'); IoSetting := ReadKey; byte(IoSetting) := byte(IoSetting) and $df; { to upper case } if (IoSetting <> 'I') and (IoSetting <> 'O') then { range check } begin WriteLn( 'I/O setting error, press a key to continue'); ReadKey; end else begin if IoSetting = 'O' then { output } begin WriteLn( 'type a hex number for output: 0 thru FF'); value := AskForValue( '0'); TempX := WhereX; TempY := WhereY; if IoPort = 'A' then begin DirPortA := IoSetting; ValuePortA := value; port[ BaseAddr + byte( IoPort) - $41] := value; { $41 = 'A' } mode := mode and $ef; { clear bit 4 } IoMode( BaseAddr, mode); IoPortOut( BaseAddr, 0, value); gotoxy( PortADirX, PortADirY); Write( 'Output'); gotoxy( PortAValueX, PortAValueY); HexToBinDisp( value); if DirPortB = 'O' then IoPortOut( BaseAddr, 1, ValuePortB); if DirPortC = 'O' then IoPortOut( BaseAddr, 2, ValuePortC); end; if IoPort = 'B' then begin DirPortB := IoSetting; ValuePortB := value; port[ BaseAddr + byte( IoPort) - $41] := value; { $41 = 'A' } mode := mode and $fd; { clear bit 1 } IoMode( BaseAddr, mode); IoPortOut( BaseAddr, 1, value); gotoxy( PortBDirX, PortBDirY); Write( 'Output'); gotoxy( PortBValueX, PortBValueY); HexToBinDisp( value); if DirPortA = 'O' then IoPortOut( BaseAddr, 0, ValuePortA); if DirPortC = 'O' then IoPortOut( BaseAddr, 2, ValuePortC); end; if IoPort = 'C' then begin DirPortC := IoSetting; ValuePortC := value; port[ BaseAddr + byte( IoPort) - $41] := value; { $41 = 'A' } mode := mode and $f6; { clear 0 & 3 } IoMode( BaseAddr, mode); IoPortOut( BaseAddr, 2, value); gotoxy( PortCDirX, PortCDirY); Write( 'Output'); gotoxy( PortCValueX, PortCValueY); HexToBinDisp( value); if DirPortA = 'O' then IoPortOut( BaseAddr, 0, ValuePortA); if DirPortB = 'O' then IoPortOut( BaseAddr, 1, ValuePortB); end; end else { IoSetting = 'I' input } begin if IoPort = 'A' then begin DirPortA := IoSetting; TempX := WhereX; TempY := WhereY; mode := mode or $10; { set bit 4 } IoMode( BaseAddr, mode); GotoXY( PortADirX, PortADirY); Write( 'Input'); GotoXY( PortAValueX, PortAValueY); HexToBinDisp( IoPortIn( BaseAddr, 0)); end; if IoPort = 'B' then begin DirPortB := IoSetting; mode := mode or 2; { set bit 1 } IoMode( BaseAddr, mode); GotoXY( PortBDirX, PortBDirY); Write( 'Input'); GotoXY( PortBValueX, PortBValueY); HexToBinDisp( IoPortIn( BaseAddr + 1, 0)); end; if IoPort = 'C' then begin DirPortC := IoSetting; mode := mode or 9; { set bits 0 and 3 } IoMode( BaseAddr, mode); GotoXY( PortCDirX, PortCDirY); Write( 'Input'); GotoXY( PortCValueX, PortCValueY); HexToBinDisp( IoPortIn( BaseAddr + 2, 0)); end; end; end; end; GotoXY( TempX, TempY); Write( 'press a key to continue'); end; 'e', 'E': done := TRUE; end; until done = TRUE; end.