{**********************************************************}
{                                                          }
{  八位七段数字LED演示程序                                 }
{                                                          }
{  Email: pnzwzw@cdle.net or pnzwzw@163.com                }
{  URL: http://www.cdle.net                                }
{                                       明浩   2003.9.14   }
{**********************************************************}
unit main;

interface

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

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    Label1: TLabel;
    Edit1: TEdit;
    Button1: TButton;
    Image1: TImage;
    Button2: TButton;
    Timer1: TTimer;
    Button3: TButton;
    procedure Button3Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  private

  public
    procedure InitDstr();
  end;

var
  Form1: TForm1;
  DStr: array[1..8] of byte; //保存8个数码LED的转换值
  DST: Integer;
  DisTime: Boolean; //用于标识是否在显示时间
const
  DSN: array[1..8] of byte = (1, 2, 4, 8, 16, 32, 64, 128); //用于控制第几个LED阳极通

implementation

Uses
  IOFun;
{$R *.dfm}


procedure TForm1.InitDstr();   //初始化LED转换值，用共阳极FFH(255)为不显示
var
  TempCyc: integer;
begin
  for TempCyc := 1 to 8 do
    DStr[TempCyc] := 255;
  DST := 1;
end;

procedure TForm1.Button3Click(Sender: TObject);
var
  TempCyc,TempCycB: integer;
  TempD: string;
begin
  DisTime := False;
  TempCycB := 0;
  InitDstr(); //初始化转换值
  try        //将输入的字符转换成数字格式
    Edit1.Text := Format('%8.2f',[StrToFloat(Edit1.Text)]);
  except
    Application.MessageBox('请输入数字', '错误', MB_OK);
    Edit1.Text := '000000.00';
  end;
  Edit1.Text := RightStr(' '+Edit1.Text,9);
  for TempCyc := 1 to 9 do
    begin
      if TempCyc <> 7 then TempCycB := TempCycB+1; //略过“.“字符
      TempD := RightStr(LeftStr(Edit1.Text,TempCyc),1);
      Case ord(TempD[1]) of  //从左开始转换
         48: DStr[TempCycB] := 192; //0
         49: DStr[TempCycB] := 249; //1
         50: DStr[TempCycB] := 164; //2
         51: DStr[TempCycB] := 176; //3
         52: DStr[TempCycB] := 153; //4
         53: DStr[TempCycB] := 146; //5
         54: DStr[TempCycB] := 130; //6
         55: DStr[TempCycB] := 248; //7
         56: DStr[TempCycB] := 128; //8
         57: DStr[TempCycB] := 144; //9
         46: DStr[6] := DStr[6]-128; //.
      end;
    end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  IO_Fun.LPTCheck(); //检测LPT基址
  if LPTBA[1] = 0 then //从检测回来的变量，取第一个有效的基址
    begin
      Application.MessageBox('没有有效的基址,程序关闭','错误');
      Application.Terminate;
    end
  else
    begin
      GLPTBA := LPTBA[1]; //取第一有效的LPT口
      ShowMessage('当前有效基址是'+IntToHex(LPTBA[1],3)+'H');
      InitDstr();  //初始化
    end;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
var
  TempCyc,TempCycB: Integer;
begin
  if DisTime then
    Button1Click(self);
  for TempCyc := 1 to 8 do
    begin
      for TempCycB := 1 to 60000 do; //不同速度的CPU可以改变不同的值，以保证显示不会闪烁
      IO_Fun.PortOut(GLPTBA+2,12);  //置控制口控制IC1（LPT16脚为高，控制阳极），同时IC2锁存
      IO_Fun.PortOut(GLPTBA,DSN[TempCyc]);  //控制数据送IC1(如DSN为1时控制LED1点亮)
      IO_Fun.PortOut(GLPTBA+2,0);  //置控制口控制IC1（LPT16脚为低，IC1锁存）同关IC2开启
      IO_Fun.PortOut(GLPTBA,DStr[TempCyc]);  //显示数据送IC2(显示的笔划)
      for TempCycB := 1 to 60000 do;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  TempCyc,TempCycB: integer;
  TempD,TempTime: string;
begin
  DisTime := True;
  TempCycB := 0;
  InitDstr(); //初始化转换值
  TimeSeparator := '-';
  ShortTimeFormat	 := 'hh:mm:ss';
  TempTime := TimeToStr(Now()); //取日期并转为字符
  for TempCyc := 1 to 8 do
    begin
      TempCycB := TempCycB+1;
      TempD := RightStr(LeftStr(TempTime,TempCyc),1);
      Case ord(TempD[1]) of  //从左开始转换
         48: DStr[TempCycB] := 192; //0
         49: DStr[TempCycB] := 249; //1
         50: DStr[TempCycB] := 164; //2
         51: DStr[TempCycB] := 176; //3
         52: DStr[TempCycB] := 153; //4
         53: DStr[TempCycB] := 146; //5
         54: DStr[TempCycB] := 130; //6
         55: DStr[TempCycB] := 248; //7
         56: DStr[TempCycB] := 128; //8
         57: DStr[TempCycB] := 144; //9
      else
        DStr[TempCycB] := 191; //-
      end;
    end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  TempCyc,TempCycB: integer;
  TempD,TempDate: string;
begin
  DisTime := False;
  TempCycB := 0;
  InitDstr(); //初始化转换值
  DateSeparator := '-';
  ShortDateFormat := 'yy/mm/dd';
  TempDate := DateToStr(Now()); //取日期并转为字符
  for TempCyc := 1 to 8 do
    begin
      TempCycB := TempCycB+1;
      TempD := RightStr(LeftStr(TempDate,TempCyc),1);
      Case ord(TempD[1]) of  //从左开始转换
         48: DStr[TempCycB] := 192; //0
         49: DStr[TempCycB] := 249; //1
         50: DStr[TempCycB] := 164; //2
         51: DStr[TempCycB] := 176; //3
         52: DStr[TempCycB] := 153; //4
         53: DStr[TempCycB] := 146; //5
         54: DStr[TempCycB] := 130; //6
         55: DStr[TempCycB] := 248; //7
         56: DStr[TempCycB] := 128; //8
         57: DStr[TempCycB] := 144; //9
      else
        DStr[TempCycB] := 191; //-
      end;
    end;
end;

procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  DisTime := False;
  InitDstr();
  Timer1Timer(self);
end;

end.

