Published:2011/8/16 22:51:00 Author:Phyllis From:SeekIC
By Herman Bulle
Combining the routines
In principle that is all there is to do: we know how to select a column and how to read on which row a key is pressed. However, these functions still have to be ’glued together’ and the result has to be shown on the display. This takes place as follows: a timer is used to call the function GetKey at regular intervals. This calls the procedure Key_Out to select a column, for example Key_out(False, False). Next we use Key_Inp to check if a key in that column has been pressed. A small section of this procedure is shown below: if no key is pressed in the first column, the variable Row becomes 0. If a key is pressed, Key_Inp returns the row number.
If row=0 then
Begin
col:=2;
Key Oat(False, True);
rov:=Key_Inp;
End;
If row=Q then
col:=3
In this way the entire keypad matrix is scanned. Once the row and column are known, the correct key number is looked up using the conversion matrix Map [row, Col], as follows. We do have to make sure that we don’t read 2 key presses if a key is held down a bit longer. This is where the variable KeyLast comes to the rescue. If we insist on at least one scan without a key press between two successive key presses, we can use that to distinguish two real key presses from a single one, which is held down too long.
The previous key press should therefore always be an empty scan (with key number 0) for the current scan to be valid.
Every old key press is stored in the variable KeyLast. When we’re scanning for a new key press the result will only be valid if the old scan (stored in KeyLast) was 0. Adding the following line to the end of the GetKey procedure stops the generation of false key presses:
If (KeyLast = 0) Then Result : = Key
else result:=0;
KeyLast : = Key;
We then store the value of the scanned key in KeyLast. This nearly completes this section. We just have to make sure that the supply voltages are restored at the end of a scan. This is done by calling Key_Oul(False, True) once more, which sets DTR to +12 V and RTS to -12 V.
Apart from the procedures mentioned above for driving and reading the RS232 port, there is another procedure (KeyGet), which is used during the simulation of the program. The intention is that only one of the two functions is active. The other is removed using curly brackets (in Delphi any text between curly brackets is interpreted as a comment).
In the simulation the tag of the key is read, which we described in the previous article. This value is put into the variable KeyPressed. In the simulation mode this value is passed as the result of the function GetKey and subsequently processed. From this point onwards the program is the same whether in simulation mode or when using an external keypad.
We now know how to read the keys. From the main program (in the unit uConsole) we start a timer that calls a procedure every 50 ms to read the key matrix. The procedure ControiExe (in uControl) separates the received IDs (1 to 16) into two groups. When ID < 10 a digit was pressed and a number is being entered. When ID > 10 an action is requested. In that case we subtract 10 from the ID and execute the appropriate action from the list defined in Tmath. As an example: ’subtraction’ has on ID or tag of 12. Process 2 is mtSub (counting from 0) and this generates a subtraction. When the process has finished, a Screenrefresh is executed, which shows the result in the display of the calculator.
Functional design
Observant readers will have noticed that this program consists of three functional blocks, which are the three units. The basis for this division is that complicated problems are best described as a block diagram, which is also common practice in electronics. If you have a clear division between these blocks you can describe and test the working of the individual blocks belter, and the chance of introducing design faults becomes smaller. In this project we have split the software into three layers (in the software world we talk about layers in this context).
We have a presentation layer (uConsole), which receives the results from the simulated keys, a control layer (uControl), which runs the processes in the procedure ControiExe and a driver layer (uDriver), which takes care of the communications with the outside world via the RS232 port.
In this installment we have shown you how to implement 4 command lines and 4 status lines under Windows for use external to the computer, with only minimal hardware and a serial port. Since most desktops have two serial ports as standard, a total of 8 command lines and 8 status lines are available for external use.
Reprinted Url Of This Article: http://www.seekic.com/blog/project_solutions/2011/08/16/DELPHI_FOR_ELECTRONIC_ENGINEERS__Part_3__An_external_calculator_(3).html
Print this Page | Comments | Reading(650)
Author:Ecco Reading(30186)
Author:Ecco Reading(3461)
Author:Ecco Reading(3181)
Author:Ecco Reading(3662)
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)