⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.pas

📁 单片机编程的一款好软件
💻 PAS
📖 第 1 页 / 共 2 页
字号:
{==============================================================================}
{--------------------- 2004年毕业设计: 单片机芯片编程器 ----------------------}
{---------------------------- 指导教师:肖永生 --------------------------------}
{-----------------------------作者    :郭俊杰 --------------------------------}
{==============================================================================}
unit Main;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,ImgList, 
  Dialogs, StdCtrls, ExtCtrls, Grids, Buttons, Menus, ComCtrls, ToolWin, Forms,
  SPComm,Inifiles;
type
  TF_Main = class(TForm)
    RadioGroup1: TRadioGroup; RadioGroup2: TRadioGroup;
    RadioButton1: TRadioButton; RadioButton2: TRadioButton;
    StatusBar1: TStatusBar; ProgressBar1: TProgressBar;
    Label1: TLabel; Label2: TLabel; Comm1: TComm; Edit1: TEdit; Bevel1: TBevel;
    OpenDialog1: TOpenDialog; SaveDialog1: TSaveDialog; StringGrid1: TStringGrid;
    MainMenu1: TMainMenu; E1: TMenuItem; A1: TMenuItem; H1: TMenuItem;
    N1: TMenuItem; N2: TMenuItem; N3: TMenuItem; N5: TMenuItem;  N6: TMenuItem;
    N7: TMenuItem; N8: TMenuItem; N9: TMenuItem; N11: TMenuItem; N4: TMenuItem;
    N12: TMenuItem; N13: TMenuItem; N14: TMenuItem;
    ImageList1: TImageList; ImageList2: TImageList;
    ControlBar1: TControlBar; ControlBar2: TControlBar;
    ToolBar1: TToolBar; ToolBar2: TToolBar;
    ToolButton1: TToolButton; ToolButton2: TToolButton; ToolButton3: TToolButton;
    ToolButton4: TToolButton; ToolButton5: TToolButton; ToolButton6: TToolButton;
    ToolButton7: TToolButton; ToolButton8: TToolButton; ToolButton9: TToolButton;
    ToolButton10: TToolButton; ToolButton11: TToolButton;
    ToolButton12: TToolButton; ToolButton13: TToolButton;
    ToolButton14: TToolButton; ToolButton15: TToolButton;
    ToolButton16: TToolButton; ToolButton17: TToolButton;
    procedure Rdic(Sender:TObject);
    procedure N2Click(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure E1Click(Sender: TObject);
    procedure H1Click(Sender: TObject);
    procedure N5Click(Sender: TObject);
    procedure N8Click(Sender: TObject);
    procedure N9Click(Sender: TObject);
    procedure N7Click(Sender: TObject);
    procedure A1Click(Sender: TObject);
    procedure N13Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure StringGrid1SelectCell(Sender: TObject;
              ACol, ARow: Integer; var CanSelect: Boolean);
    procedure Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
              BufferLength: Word);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private    { Private declarations }
  public     { Public declarations }
end;

var
  F_Main: TF_Main;
  s,sz:string;
  myini:TInifile;
  Read,Comp:Boolean;
  ia,ja,LL,w:Integer;
  file1,file2:TFileStream;
  buf:array[1..65536] of Char;
implementation
uses Help, About;

{$R *.dfm}
/////////////////////////////////// 程序开始 ///////////////////////////////////
{==============================================================================}

{------------------------------------------------------------------------------}
{-------------------------------- 公用事件 ------------------------------------}
procedure TF_Main.Rdic(Sender: TObject);
begin
  Read:=True; ja:=1; ia:=1;
  case RadioGroup1.ItemIndex of
    0: begin s:=Char($07); sz:='0800'; end;    //2051
    1: begin s:=Char($05); sz:='1000'; end;    //89c51
    2: begin s:=Char($06); sz:='2000'; end;    //89c52
    3: begin s:=Char($05); sz:='1000'; end;    //89s51
    4: begin s:=Char($06); sz:='2000'; end;    //89s52
    5: begin s:=Char($08); sz:='4000'; end;    //89s53
    6: begin s:=Char($01); sz:='2000'; end;    //2764
    7: begin s:=Char($01); sz:='2000'; end;    //2864
  end;
  LL:= StrToInt('$'+sz);  w:=LL;
  Comm1.WriteCommData(pchar(s),1);             //发出控制码
  Progressbar1.Min :=0;                        //最小值
  Progressbar1.Max :=LL;                       //最大值
  Progressbar1.Step:=256;                      //步进
end;
{------------------------------------------------------------------------------}
procedure TF_Main.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
          BufferLength: Word);
var
  i:Integer;
  s,d:String;
  cp:Boolean;
begin
  Setlength(s,bufferlength); cp:=True;
  Move(buffer^,pchar(s)^,bufferlength);
  if Read then
    begin
      for i:=1 to bufferlength do
        begin
          if Comp then
            begin
              if stringGrid1.Cells[ja,ia]<>IntToHex(Byte(s[i]),2) then
                begin cp:=False; stringGrid1.Cells[ja,ia]:=stringGrid1.Cells[ja,ia];   Showmessage('Comp Error!'); Break; end;
            end
          else StringGrid1.Cells[ja,ia]:=IntToHex(byte(s[i]),2);
          ja:=ja+1;
          if ja=17 then begin ja:=1; ia:=ia+1 end;
        end;
      Progressbar1.StepIt;
      Edit1.Text:='当前地址:'+IntToHex(ia-1,3)+IntToHex(ja-1,1);
      if cp then s:=Char($6A)
      else s:=Char($6B);
      Comm1.WriteCommData(pchar(s),1);
      w:=w-256;
      if w=0 then begin
        if Comp then Showmessage(' Comp OK !')  else Showmessage(' Readed OK ! ');
        Progressbar1.Position:=0 ;
      end;
    end
  else                                           
    case Byte(s[1]) of
      $5A: begin
             d:=Char(StrToInt('$'+sz[1]+sz[2]));
             Comm1.WriteCommData(pchar(d),1);
           end;
      $5B: begin
             d:=Char(StrToInt('$'+sz[3]+sz[4]));
             Comm1.WriteCommData(pchar(d),1);
           end;
      $5C: begin
             for i:=1 to 16 do
               begin
                 d:=Stringgrid1.Cells[ja,ia];
                 if d='' then d:=Char($FF) else d:=Char(StrToInt('$'+d));
                 Comm1.WriteCommData(pchar(d),1); ja:=ja+1;
                 if ja=17 then begin ja:=1; ia:=ia+1 end;
               end;
             Progressbar1.StepIt;
             Edit1.Text:='当前地址:'+IntToHex(ia-1,3)+IntToHex(ja-1,1);
           end;
      $5D: begin Showmessage('  OK !  '); Progressbar1.Position:=0 ; end;
      $5E: Showmessage('  Error ! ');
    end;
end;
{------------------------------------------------------------------------------}
procedure TF_Main.StringGrid1SelectCell(Sender: TObject;
          ACol, ARow: Integer; var CanSelect: Boolean);
Var PanStr:String;
begin
  PanStr:='    当前单元地址:'+IntToHex(ARow-1,3)+IntToHex(ACol-1,1);
  StatusBar1.Panels[1].Text:=PanStr;
  Progressbar1.Position:=0 ;
end;
{------------------------------------------------------------------------------}
{-----------------------------  程序启动事件 ----------------------------------}
procedure TF_Main.FormCreate(Sender: TObject);   // 程序启动事件
var
  adr:String;
  i,k:Integer;
begin
  Read:=False; Comp:=False;
  myini:=TInifile.Create('write.ini');
  Radiogroup1.ItemIndex:=myini.Readinteger('芯片选择','型号',1);
  Radiogroup2.ItemIndex:=myini.Readinteger('文件格式','类型',1);
  Comm1.IgnoreNullChar:=False;
  Comm1.Inx_XonXoffFlow:=False;
  Comm1.Outx_XonXoffFlow:=False;
  Comm1.CommName:='com1';
  Comm1.TxContinueOnXoff:=True;
  Comm1.StartComm;
  s:='0000';
  StringGrid1.ColWidths[0]:=45;
  Stringgrid1.RowCount:=$201;
  Progressbar1.Visible:=True;
  Progressbar1.Step:=1;
  for i:=0 to 15 do                               // 循环填充行头0~F(列行)
    begin
      if i<10 then k:=i+$30 else k:=i+$37;
      StringGrid1.Cells[i+1,0]:=Char(k);
    end;
  for i:=0 to Stringgrid1.RowCount do             // 循环填充地址
    begin
      adr:=format('%x',[i*$10]);
      StringGrid1.Cells[0,i+1]:=copy(s,1,4-length(adr))+adr;
    end;
  StringGrid1.Cells[0,0]:='地址';
end;
{------------------------------------------------------------------------------}
{-----------------------------  程序关闭事件 ----------------------------------}
procedure TF_Main.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  COMM1.StopComm;
  myini.Writeinteger('芯片选择','型号',Radiogroup1.ItemIndex);
  myini.Writeinteger('文件格式','类型',Radiogroup2.ItemIndex);
  Application.Terminate;
end;
{--------------------------------- 文件菜单  ----------------------------------}
procedure TF_Main.N2Click(Sender: TObject);      //<打开>菜单处理事件
var
  i,j,m,n,nk,x,y,yi,xa:Integer;
begin
  N13.Enabled:=True; ToolButton6.Enabled:=True;
  for i:=1 to StringGrid1.RowCount do
    for j:=1 to 17 do
      if Radiobutton1.Checked then StringGrid1.Cells[j,i]:='FF'
      else StringGrid1.Cells[j,i]:='00';
  OpenDialog1.FilterIndex:=RadioGroup2.ItemIndex+1;
  if OpenDialog1.Execute then
    begin
      LL:=0; y:=0;
      file1:=TFilestream.Create(Opendialog1.FileName,fmopenread);
      file1.Read(buf,file1.size);    n:=1; nk:=0; x:=0;
      F_Main.Caption :='当前读入文件是:'+Opendialog1.FileName;
      case RadioGroup2.ItemIndex of
        1: begin
             while n<file1.Size do
               begin
                 if buf[n]=':' then
                   if buf[n+1]='1' then nk:=16
                   else nk:=StrToInt('$'+(buf[n+2]));
                 x:=StrToInt('$'+buf[n+3]+buf[n+4]+buf[n+5]);
                 y:=StrToInt('$'+(buf[n+6]));

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -