Published:2011/8/16 22:30:00 Author:Phyllis From:SeekIC
By Herman Bulle
Software switching
To drive the DTR and RTS lines we’ll have to write a bit of Delphi code.
To start with, we have to open the serial port. We have already covered this in Part 1 for the burglar alarm:
FHandle: = CreateFile (PChar(port), Generic_Read+
Generic+Write, 0, Nil, Open_Existing, 0, 0);
If FHandle = Invalid_Handle_Value Then
Begin
ShowMessage (’unable to open communication port.’);
End;
(As a reminder: Delphi programs are not ’case sensitive’, unlike C and several other programming languages. Because of this, it may happen that we don’t always use capitals and small letters the same way in different parts of the program.)
In the program mentioned above this line had an error-check added: if the port was already open it wouldn’t be opened again.
Windows keeps a list of various objects such as programs, ports, drivers etc. These objects are given a unique code (or handle). The function CreareFile, called above, returns the value of this code. If this value is equal to Invalid_Handle_Value, which is defined in Delphi, something went wrong during the initialisation of the port and the program is halted with a message and the exit command.
On successful completion of this phase we have to activate a few outputs. DTR has to be set to +12 V and RTS to -12V.
To help with programming the UART, a data structure (DCB, see inset) is defined in Windows that contains a large set of parameters. Once all parameters have been set, this data structure is sent to the UART. For our program the DCB.Flags field is the most important. In this field virtually all bits have a separate function. We have defined two constants in uDriver, which are required to turn DTR and RTS ’on’ and ’off’.
First the current state of the UART is read with the command GetCommState(FHandle, DCB). Next we set a bit in the FLAGS field to turn RTS on:
DCB.Flags := DCB.Flags OR RtsEnable
The RTS output then becomes -12 V. We do something similar for DTR, but in this case we have to set the bit to ’0’ while keeping all the other bits the same:
DCB.Flags := DCB.Flags AND (NOT DtrEnable)
We can now send the modified DCB back to the UART using the command SetCommState(FHandle, DCB). Since we need to use all possible combinations of DCD and RTS when selecting the columns, we have written the following procedure:
Procedure Key_0ut(RTS, DTR: Boolean);
Var DCB: TDCB;
Begin
IF FHandle > 0 Then Begin GetConu?,State(FHandle, DCB);
If DTR Then DCB.Flags : = DCB.Flags Or DtrEnable Else DCB.Flags := DCB.Flags And (NOT DtrEnable);
If RTS Then DCB.Flags := DCB.Flags Or RtsEnable Else DCB.Flags := DCB.Flags And (NOT RtsEnable);
SetCoET.State (FHandle, DCB)
End
End;
To turn on the supply voltage to the IC we call this function with the following parameters: Key_Out(False, True). This is done every time after scanning the keys, thereby keeping the capacitors fully charged.
Scanning the matrix
The next building block required for this project has to take care of the reading of the status of the four input lines. With the help of the procedure GetComm-ModemStatus(FHandle, MdmSts) these details are stored in the variable MdmSts. By applying a mask we can determine which bit, if any, is set to ’ 1’. A mask is a number which has just a single bit set to ’1’, for example binary 1000 (= hex 10), binary 10000 (= hex 0020) etc. Delphi has already defined some important values for us:
MS_CTS_ON = hex 0010 MS_DSR_0!i = hex 0020 MS_RING_ON = hex 0040 KS_RLSD_ON = hex 0080;
If we now apply the mask using the Boolean AND function we can determine if this bit is set. This therefore tells us if a key in this row is pressed:
If MdmSts And HS_RLSD_ON = HS RfcSD_OH Then
Result := 1
The same method is used to check if a key is pressed in the other rows.
The result will be 1,2,3 or 4, depending on the selected row.
These lines of code have been combined in the function Key_Inp, which returns the value of the selected row.
Reprinted Url Of This Article: http://www.seekic.com/blog/project_solutions/2011/08/16/DELPHI_FOR_ELECTRONIC_ENGINEERS__Part_3__An_external_calculator_(2).html
Print this Page | Comments | Reading(959)
Author:Ecco Reading(30178)
Author:Ecco Reading(3461)
Author:Ecco Reading(3181)
Author:Ecco Reading(3661)
Author:Ecco Reading(5231)
Author:Ecco Reading(3248)
Author:Ecco Reading(3390)
Author:Ecco Reading(3530)
Author:Ecco Reading(3935)
Author:Ecco Reading(3707)
Author:Ecco Reading(3677)
Author:Ecco Reading(3714)
Author:Ecco Reading(6028)
Author:Ecco Reading(3692)
Author:Ecco Reading(4478)