Tuesday 3 August 2010

Function and Procedure

There are two kind of subroutine in Pascal : Function and Procedure. The difference between these subroutines is in return value. Function gives a return value, but procedure doesn’t. The structure of function and procedure are the same :

Here is the example of procedure and function :

program FunctionAndProcedure;
{$APPTYPE CONSOLE}
uses
   SysUtils;

procedure Add1(x, y : integer);
 var
  z : integer;
begin
  z := x + y;
  writeln(x,' + ',y,' = ',z,' (using procedure)');
  readln;
end;

function Add2(x, y : integer) : integer;
begin
  Add2 := x + y;
end;

begin
  Add1(25,10);
  writeln('25 + 40 = ',Add2(25,40),' (using function)');
  readln;
end.

You can download the complete code here.

Tuesday 27 July 2010

The Complete Electronic Circuit

(Dancing Lamp #3)

In my previous tutorial, I’ve given you the example program and the LED circuit. But what you’ve got is only a small electronic circuit on your table. How about if you want to use bigger light bulbs instead of using LED. Here is the complete electronic circuit : 

You have to connect parallel port ground pin out to the circuit ground. Just be careful while connecting this circuit to your computer, or it will damage your computer !!! Make sure that everything is in the place.

Using this circuit, if you send a “high” to the data pin out (D0 .. D7), it will trigger the transistor. It will be saturated, and let current flows through the relay and the light bulb will be connected to the electricity and light on. Use this circuit for each data pin out of your parallel port.


Previous tutorials about dancing lamp :

Accessing your PC’s parallel port using Inpout32.dll in Delphi

LED Circuit


Monday 26 July 2010

LED Circuit

(Dancing Lamp # 2)

To test the program I’ve created in my previous tutorial, I use this circuit (see picture). But be careful while you connect this circuit to your parallel port. Make sure that every component is in the right place or connection, otherwise it will damage your PC !!!

I use 1 KOhm resistors. If you send an integer data = 15 to the parallel port, D0…D3 LED will be light on. And D4…D7 LED will be off.

You can arrange which LED is on or off by writing some codes in Delphi. And you can also set the delay of each step you turn on or off the LED using OnTimer event of Timer component in Delphi. I’ve created an example program for you.

Download the example program here.

Go to previous tutorial (Accessing your PC’s parallel port using Inpout32.dll in Delphi)

Go to next tutorial (The Complete Electronic Circuit)

Monday 19 July 2010

Accessing your PC’s parallel port using Inpout32.dll in Delphi

(Dancing Lamp # 1)

Before we create a dancing lamp using our PC’s parallel port, we have to know the parallel port female pin-out (see the picture below).

D0 is the LSB (Least Significant Bit) and D7 is the Most Significant Bit (MSB). You can send an integer type data to the D0..D7 pin-outs. And the output will be the binary form of the integer we send. For example : If we send an integer data 15 the output will be :

D7D6D5D4D3D2D1D0
LOWLOWLOWLOWHIGHHIGHHIGHHIGH

15 (byte) = 00001111 (binary)

How To Send Data To The Parallel Port ?

To send data to the parallel port I use a DLL file (Inpout32.dll) that can be downloaded at logix4u.net, and it’s free for non-commercial manner. Place this file in C:\WINDOWS\system or in the same folder with you project you’re going to create.

To use this file you can see my previous tutorial about using a DLL file. This DLL file contains a function called Out32(). The syntax of this function :

                                                    Out32(portaddress,data);

Usually, the port address is $378 (you can check the BIOS setting of your PC) 

Example program :

unit Unit1;

interface

uses
   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
   StdCtrls;

type
   TForm1 = class(TForm)
   Edit1: TEdit;
   Label1: TLabel;
   Button1: TButton;
   procedure FormActivate(Sender: TObject);
   procedure Button1Click(Sender: TObject);
private
  { Private declarations }
public
  { Public declarations }
end;

var
   Form1: TForm1;

implementation

{$R *.DFM}
function Out32(wAddr:word;bOut:byte):byte; stdcall; external 'inpout32.dll';

procedure TForm1.FormActivate(Sender: TObject);
begin
  Edit1.Text := '';
  Edit1.SetFocus; 
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Out32($378,StrToInt(Edit1.Text));
end;

end.

You can download the complete code here.

Go to next tutorial (LED Circuit)

Wednesday 7 July 2010

Pointer

Pointer is a variable that points toward a memory address. So a pointer is a data address. The data is somewhere else. Below picture will show you the relationship between a pointer and a data pointed by a pointer.

To declare a pointer, use the below statement :

Type

   PointerType : ^Type;

In this case, type can be an integer, a real, an array or record. And you can see the example code of using a pointer :


program Project1;
   {$APPTYPE CONSOLE}
uses
   SysUtils;

type
   PointerType = ^integer;

var
   MyPointer : PointerType;
   Data : integer;

begin
  Data := 100;
  MyPointer := @Data;
  writeln('Data pointed toward by the pointer is ', MyPointer^);
  Data := 123;
  writeln('Data pointed toward by the pointer is ', MyPointer^);
  readln;
end.


Disposing pointer allocation

To free the memory that is used by a pointer, you can use dispose statement.

  Dispose(pointer);

Example :

program Project1;
   {$APPTYPE CONSOLE}
uses
   SysUtils;

var
   MyPointer : ^integer;

begin
  New(MyPointer); // allocate memory
  MyPointer^ := 100;
  writeln('Vallue pointed toward by MyPointer is ',MyPointer^);
  Dispose(MyPointer); // free memory allocation
  readln;
end.


You can download the complete code here.

Sunday 20 June 2010

Record’s variant

A record can have one or more variant elements or fields. This variant allows data to accessed using one or more different fields. To declare a record contain variant use these statements :

Type

   RecordTypeName = record

      Field_1 : type_1;

      ..

      ..

      Field_n : type_n;

      Case Tag : OrdinalType of

         Constant_1 : ( variant_1);

         Constant_2 : ( variant_2);

      End;

Example record contain variant :

Type

   TCategory = (FullTimer, PartTimer);

   TEmployeeRecord : record

     Name : String(30);

     Case Category : TCategory of

       Full Time : ( Salary : currency;

                              Bonus : currency);

       Part Time : (  Salary : currency);

     End;


Example program :

program Project1;
{$APPTYPE CONSOLE}
uses
   SysUtils;

type
   TCategory = ( FullTimer, PartTimer );
   TEmployeeRecord = record
       Name : String[30];
       Case Category : TCategory of
          FullTimer : ( Salary : currency;
                                  Bonus : currency);
          PartTimer : ( Payment : currency);
   end;

var
   Employee : TEmployeeRecord;

begin
  with Employee do
    begin
     // assigning fields' values
     Name := 'Albert Einstein';
     Category := FullTimer;
     Salary := 1000;
     Bonus := 100;
     // writing fields' values
     writeln('Name : ',Name);
     writeln('Salary : ',Salary:10:2);
     writeln('Bonus : ',Bonus:10:2);
     // assigning fields' values
     Name := 'Albert Einstein';
     Category := PartTimer;
     Payment := 500;
     // writing fields' values
     writeln('Name : ',Name);
     writeln('Payment : ',Payment:10:2);
    end;
  readln;
end.

You can download the complete code here.

Friday 11 June 2010

Accessing Record’s Elements

After we declare a record ( previous tutorial ), now we are going to access the elements or fields of the record. The fields in a record can be accessed using this notation :

    RecordName.RecordField

Between the field name and the record name separated by a dot.

Example :

program Project1;
{$APPTYPE CONSOLE}
uses
   SysUtils;

type
   TRecordItem = record
     Item : string;
     Quantity : integer;
     Price : currency;
   end;
var
   ItemStock : TRecordItem;

begin
  // assigning values to record's fields
  ItemStock.Item := 'USB Flash Disk';
  ItemStock.Quantity := 20;
  ItemStock.Price := 10;
  // writing teh fields' values.
  writeln('Item : ',ItemStock.Item);
  writeln('Quantity : ',ItemStock.Quantity,' pc/s');
  writeln('Price : $ ',ItemStock.Price:10:2 );
  readln;
end.

Using with statement

With statement allows us to access a record field without mentioning the record name. The syntax of with statement is :

 with Record do
   begin
     statements
   end;

Example :

program Project1;
{$APPTYPE CONSOLE}
uses
   SysUtils;

type
   TRecordItem = record
     Item : string;
     Quantity : integer;
     Price : currency;
   end;
var
ItemStock : TRecordItem;

begin
  with ItemStock do
    begin
      // assigning values to record's fields
      Item := 'USB Flash Disk';
      Quantity := 20;
      Price := 10;
      // writing teh fields' values.
      writeln('Item : ',Item);
      writeln('Quantity : ',Quantity,' pc/s');
      writeln('Price : $ ',Price:10:2 );
    end;
  readln;
end.

You can download the complete code here.

These links are part of a pay per click advertising program called Infolinks. Infolinks is an In Text advertising service; they take my text and create links within it. If you hover your mouse over these double underlined links, you will see a small dialog box containing an advertisement related to the text. You can choose to move the mouse away and go on with your browsing, or to click on the box and visit the relevant ad. Click here to learn more about Infolinks Double Underline Link Ads.