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

📄 main.pas

📁 单片机isp编程上位机源代码
💻 PAS
📖 第 1 页 / 共 2 页
字号:
unit Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Mask, Buttons, ComCtrls, ExtCtrls, msxml, Inifiles,
  JvExMask, JvToolEdit, JvComponentBase, JvCreateProcess;

type
  TMainForm = class(TForm)
    GroupBox1: TGroupBox;
    edFlash: TJvFilenameEdit;
    GroupBox2: TGroupBox;
    edEeprom: TJvFilenameEdit;
    GroupBox3: TGroupBox;
    cbProgrammer: TComboBox;
    cbPort: TComboBox;
    cbMCU: TComboBox;
    gbxFuse: TGroupBox;
    gbxLowByte: TGroupBox;
    gbxHighByte: TGroupBox;
    gbxExtByte: TGroupBox;
    gbxLockByte: TGroupBox;
    ckbLock6: TCheckBox;
    ckbLock5: TCheckBox;
    ckbLock4: TCheckBox;
    ckbLock3: TCheckBox;
    ckbLock2: TCheckBox;
    ckbLock1: TCheckBox;
    ckbLock0: TCheckBox;
    ckbLock7: TCheckBox;
    ckbExt7: TCheckBox;
    ckbExt6: TCheckBox;
    ckbExt5: TCheckBox;
    ckbExt4: TCheckBox;
    ckbExt3: TCheckBox;
    ckbExt2: TCheckBox;
    ckbExt1: TCheckBox;
    ckbExt0: TCheckBox;
    ckbHigh7: TCheckBox;
    ckbHigh6: TCheckBox;
    ckbHigh5: TCheckBox;
    ckbHigh4: TCheckBox;
    ckbHigh3: TCheckBox;
    ckbHigh2: TCheckBox;
    ckbHigh1: TCheckBox;
    ckbHigh0: TCheckBox;
    ckbLow7: TCheckBox;
    ckbLow6: TCheckBox;
    ckbLow5: TCheckBox;
    ckbLow4: TCheckBox;
    ckbLow3: TCheckBox;
    ckbLow2: TCheckBox;
    ckbLow1: TCheckBox;
    ckbLow0: TCheckBox;
    plFuse: TPanel;
    GroupBox9: TGroupBox;
    ckbProgress: TCheckBox;
    ckbErase: TCheckBox;
    ckbVerify: TCheckBox;
    ckbFlash: TCheckBox;
    ckbEerpom: TCheckBox;
    ckbFuse: TCheckBox;
    ckbLock: TCheckBox;
    btnErase: TButton;
    btnProgram: TButton;
    btnVerify: TButton;
    btnRead: TButton;
    btnExit: TButton;
    sbnSave: TSpeedButton;
    sbnLoad: TSpeedButton;
    sbnDefault: TSpeedButton;
    ckbDelay: TCheckBox;
    GroupBox5: TGroupBox;
    ckbReset: TCheckBox;
    ckbNoReset: TCheckBox;
    ckbNoVCC: TCheckBox;
    ckbVCC: TCheckBox;
    OpenDialog1: TOpenDialog;
    SaveDialog1: TSaveDialog;
    Label1: TLabel;
    lbLow: TLabel;
    Label3: TLabel;
    lbHigh: TLabel;
    Label5: TLabel;
    lbExt: TLabel;
    Label7: TLabel;
    lbLock: TLabel;
    mmOutput: TMemo;
    JvCreateProcess1: TJvCreateProcess;
    btnStop: TButton;
    CheckBox1: TCheckBox;
    JvCreateProcess2: TJvCreateProcess;
    sbnWrite: TSpeedButton;
    sbnRead: TSpeedButton;
    procedure FormCreate(Sender: TObject);
    procedure btnExitClick(Sender: TObject);
    procedure cbMCUChange(Sender: TObject);
    procedure btnEraseClick(Sender: TObject);
    procedure cbProgrammerChange(Sender: TObject);
    procedure btnProgramClick(Sender: TObject);
    procedure btnReadClick(Sender: TObject);
    procedure sbnDefaultClick(Sender: TObject);
    procedure sbnSaveClick(Sender: TObject);
    procedure JvCreateProcess1Read(Sender: TObject; const S: string;
      const StartsOnNewLine: Boolean);
    procedure JvCreateProcess1Terminate(Sender: TObject;
      ExitCode: Cardinal);
    procedure btnStopClick(Sender: TObject);
    procedure btnVerifyClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure CheckBox1Click(Sender: TObject);
    procedure JvCreateProcess2Terminate(Sender: TObject;
      ExitCode: Cardinal);
    procedure JvCreateProcess2Read(Sender: TObject; const S: string;
      const StartsOnNewLine: Boolean);
    procedure sbnReadClick(Sender: TObject);
    procedure sbnWriteClick(Sender: TObject);
    procedure sbnLoadClick(Sender: TObject);
  private
    sCMDLine: string;
    OutList: TStringList;
    procedure SetCheckboxStatus(ACheckBox: TCheckBox; ASearchString: string;
      XML: IXMLDOMNode);
    procedure BuildCMDLine(Kind: Integer);

    function GetFuseState(FuseName: string): Byte;
    procedure SetFuseState(FuseName: string; FuseValue: Byte);

    function SaveFuse: string;
    procedure LoadFuse(S: string);
    procedure AddLogMsg(const Text: string; IsClear: Boolean);
    procedure ChangeMsg(const Text: string);
    procedure UpdateButtons;

    procedure SaveHistroy;
    procedure LoadHistroy;

    procedure StatusGiveIO;
    procedure InstallGiveIO;
    procedure RemoveGiveIO;
    function CheckStatusGiveIO(S: string): Boolean;
    function GetCurrPath: string;

    procedure InitCheckBox;
    procedure ckbLowClick(Sender: TObject);
    procedure ckbHighClick(Sender: TObject);
    procedure ckbExtClick(Sender: TObject);
    procedure ckbLockClick(Sender: TObject);

    { Private declarations }
  public
    { Public declarations }
  end;

var
  MainForm: TMainForm;

implementation

{$R *.dfm}

function BinToInt(const S: string): Byte;
var
  i, Len: Integer;
begin
  Result := 0;
  Len := Length(S);
  for i := Len downto 1 do
    if S[i] = '1' then
      Result := Result + (1 shl (Len - i));
end;

function MsgBox(AText: string; ACaption: string = '提示'; AFlags: Longint = MB_OK
  + MB_ICONINFORMATION): Integer;
begin
  Result := Application.MessageBox(Pchar(AText), Pchar(ACaption), AFlags);
end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
  InitCheckBox;
  OutList := TStringList.Create;
  StatusGiveIO;
  MainForm.Font.Name := 'Tahoma';
  MainForm.Font.Size := 8;
  LoadHistroy;
  cbMCUChange(cbMCU);
end;

procedure TMainForm.btnExitClick(Sender: TObject);
begin
  Close;
end;

procedure TMainForm.cbMCUChange(Sender: TObject);
var
  S: string;
  F: string;
  XML: IXMLDOMDocument;
  Node: IXMLDOMNode;
begin
  S := cbMCU.Text;
  F := ExtractFilePath(Application.ExeName) + 'part\' + S + 'sl.xml';
  if FileExists(F) then
  begin
    XML := CoDOMDocument.Create;
    XML.load(F);
    Node := XML.documentElement.selectSingleNode('FUSE/LOW');
    if Node <> nil then
      gbxLowByte.Tag := 0
    else
      gbxLowByte.Tag := 1;
    Node := XML.documentElement.selectSingleNode('FUSE/HIGH');
    if Node <> nil then
      gbxHighByte.Tag := 0
    else
      gbxHighByte.Tag := 1;
    Node := XML.documentElement.selectSingleNode('FUSE/EXTENDED');
    if Node <> nil then
      gbxExtByte.Tag := 0
    else
      gbxExtByte.Tag := 1;
    Node := XML.documentElement.selectSingleNode('LOCKBIT');
    if Node <> nil then
      gbxLockByte.Tag := 0
    else
      gbxLockByte.Tag := 1;

    SetCheckboxStatus(ckbLow0, 'FUSE/LOW/FUSE0', XML.documentElement);
    SetCheckboxStatus(ckbLow1, 'FUSE/LOW/FUSE1', XML.documentElement);
    SetCheckboxStatus(ckbLow2, 'FUSE/LOW/FUSE2', XML.documentElement);
    SetCheckboxStatus(ckbLow3, 'FUSE/LOW/FUSE3', XML.documentElement);
    SetCheckboxStatus(ckbLow4, 'FUSE/LOW/FUSE4', XML.documentElement);
    SetCheckboxStatus(ckbLow5, 'FUSE/LOW/FUSE5', XML.documentElement);
    SetCheckboxStatus(ckbLow6, 'FUSE/LOW/FUSE6', XML.documentElement);
    SetCheckboxStatus(ckbLow7, 'FUSE/LOW/FUSE7', XML.documentElement);

    SetCheckboxStatus(ckbHigh0, 'FUSE/HIGH/FUSE0', XML.documentElement);
    SetCheckboxStatus(ckbHigh1, 'FUSE/HIGH/FUSE1', XML.documentElement);
    SetCheckboxStatus(ckbHigh2, 'FUSE/HIGH/FUSE2', XML.documentElement);
    SetCheckboxStatus(ckbHigh3, 'FUSE/HIGH/FUSE3', XML.documentElement);
    SetCheckboxStatus(ckbHigh4, 'FUSE/HIGH/FUSE4', XML.documentElement);
    SetCheckboxStatus(ckbHigh5, 'FUSE/HIGH/FUSE5', XML.documentElement);
    SetCheckboxStatus(ckbHigh6, 'FUSE/HIGH/FUSE6', XML.documentElement);
    SetCheckboxStatus(ckbHigh7, 'FUSE/HIGH/FUSE7', XML.documentElement);

    SetCheckboxStatus(ckbExt0, 'FUSE/EXTENDED/FUSE0', XML.documentElement);
    SetCheckboxStatus(ckbExt1, 'FUSE/EXTENDED/FUSE1', XML.documentElement);
    SetCheckboxStatus(ckbExt2, 'FUSE/EXTENDED/FUSE2', XML.documentElement);
    SetCheckboxStatus(ckbExt3, 'FUSE/EXTENDED/FUSE3', XML.documentElement);
    SetCheckboxStatus(ckbExt4, 'FUSE/EXTENDED/FUSE4', XML.documentElement);
    SetCheckboxStatus(ckbExt5, 'FUSE/EXTENDED/FUSE5', XML.documentElement);
    SetCheckboxStatus(ckbExt6, 'FUSE/EXTENDED/FUSE6', XML.documentElement);
    SetCheckboxStatus(ckbExt7, 'FUSE/EXTENDED/FUSE7', XML.documentElement);

    SetCheckboxStatus(ckbLock0, 'LOCKBIT/LOCKBIT0', XML.documentElement);
    SetCheckboxStatus(ckbLock1, 'LOCKBIT/LOCKBIT1', XML.documentElement);
    SetCheckboxStatus(ckbLock2, 'LOCKBIT/LOCKBIT2', XML.documentElement);
    SetCheckboxStatus(ckbLock3, 'LOCKBIT/LOCKBIT3', XML.documentElement);
    SetCheckboxStatus(ckbLock4, 'LOCKBIT/LOCKBIT4', XML.documentElement);
    SetCheckboxStatus(ckbLock5, 'LOCKBIT/LOCKBIT5', XML.documentElement);
    SetCheckboxStatus(ckbLock6, 'LOCKBIT/LOCKBIT6', XML.documentElement);
    SetCheckboxStatus(ckbLock7, 'LOCKBIT/LOCKBIT7', XML.documentElement);
  end;

  lbLow.Caption := '0x' + IntToHex(GetFuseState('ckbLow'), 2);
  lbHigh.Caption := '0x' + IntToHex(GetFuseState('ckbHigh'), 2);
  lbExt.Caption := '0x' + IntToHex(GetFuseState('ckbExt'), 2);
  lbLock.Caption := '0x' + IntToHex(GetFuseState('ckbLock'), 2);

  XML := nil;
end;

procedure TMainForm.SetCheckboxStatus(ACheckBox: TCheckBox; ASearchString:
  string; XML: IXMLDOMNode);
var
  Node: IXMLDOMNode;
begin
  Node := XML.selectSingleNode(ASearchString);
  if (Node <> nil) then
  begin
    ACheckBox.Caption := Node.childNodes[0].text;
    if (Node.childNodes.length > 2) then
    begin
      if (Node.childNodes[2].text = '0') then
        ACheckBox.Checked := True
      else
        ACheckBox.Checked := False;
    end
    else
    begin
      ACheckBox.Checked := False;
    end;

    if (ACheckBox.Caption <> 'SPIEN') and (ACheckBox.Caption <> 'RSTDISBL') then
    begin
      ACheckBox.Enabled := True;
    end
    else
    begin
      ACheckBox.Enabled := False;
    end;
    ACheckBox.Tag := 0;
  end
  else
  begin
    ACheckBox.Checked := False;
    ACheckBox.Enabled := False;
    ACheckBox.Caption := '';
    ACheckBox.Tag := 1;
  end;
end;

procedure TMainForm.BuildCMDLine(Kind: Integer);
var
  mcu, ExitMode: string;
begin
  sCMDLine := GetCurrPath + 'bin\avrdude.exe';

  //MCU ID
  case cbMCU.ItemIndex of
    0: mcu := 'm8';
    1: mcu := 'm16';
    2: mcu := 'm32';
    3: mcu := 'm48';
    4: mcu := 'm64';
    5: mcu := 'm88';
    6: mcu := 'm128';
  else
    mcu := 'm168';
  end;
  sCMDLine := sCMDLine + ' -p ' + mcu;

  //program style
  if cbProgrammer.ItemIndex = 0 then
    sCMDLine := sCMDLine + ' -c pony-stk200'
  else
    sCMDLine := sCMDLine + ' -c usbasp';

  //program port
  if cbProgrammer.ItemIndex = 0 then
    sCMDLine := sCMDLine + ' -P ' + LowerCase(cbPort.Text);

  // verify signature
  if not ckbVerify.Checked then
    sCMDLine := sCMDLine + ' -V';

  //disable auto erase
  if not ckbErase.Checked then
    sCMDLine := sCMDLine + ' -D';

  //show progress bar
  if not ckbProgress.Checked then
    sCMDLine := sCMDLine + ' -q';

  // show verbose
  if ckbDelay.Checked then
    sCMDLine := sCMDLine + ' -v';

  //write
  if Kind = 0 then
  begin
    //flash eeprom
    if ckbFlash.Checked then
      sCMDLine := sCMDLine + ' -U flash:w:"' + edFlash.Text + '":a';

    if ckbEerpom.Checked then
      if trim(edEeprom.Text) <> '' then
        sCMDLine := sCMDLine + ' -U eeprom:w:"' + edEeprom.Text + '":a';

    if ckbFuse.Checked then
    begin
      if gbxLowByte.Tag = 0 then
        sCMDLine := sCMDLine + ' -U lfuse:w:0x' +
          IntToHex(GetFuseState('ckbLow'), 2) + ':m';
      if gbxHighByte.Tag = 0 then
        sCMDLine := sCMDLine + ' -U hfuse:w:0x' +
          IntToHex(GetFuseState('ckbHigh'), 2) + ':m';
      if gbxExtByte.Tag = 0 then
        sCMDLine := sCMDLine + ' -U efuse:w:0x' +
          IntToHex(GetFuseState('ckbExt'), 2) + ':m';
    end;

    if ckbLock.Checked then
    begin
      if gbxLockByte.Tag = 0 then
        sCMDLine := sCMDLine + ' -U lock:w:0x' +
          IntToHex(GetFuseState('ckbLock'), 2) + ':m';
    end;
  end
  else if Kind = 1 then //read
  begin
    //flash eeprom
    if ckbFlash.Checked then
      sCMDLine := sCMDLine + ' -U flash:r:"' + GetCurrPath + 'bin\flash.hex":i';

    if ckbEerpom.Checked then
      if trim(edEeprom.Text) <> '' then
        sCMDLine := sCMDLine + ' -U eeprom:r:"' + GetCurrPath +
          'bin\eeprom.hex":i';

    if ckbFuse.Checked then
    begin
      if gbxLowByte.Tag = 0 then
        sCMDLine := sCMDLine + ' -U lfuse:r:"' + GetCurrPath +
          'bin\lowbyte.hex":d';
      if gbxHighByte.Tag = 0 then
        sCMDLine := sCMDLine + ' -U hfuse:r:"' + GetCurrPath +
          'bin\highbyte.hex":d';
      if gbxExtByte.Tag = 0 then
        sCMDLine := sCMDLine + ' -U efuse:r:"' + GetCurrPath +
          'bin\extbyte.hex":d';
    end;

    if ckbLock.Checked then
    begin
      if gbxLockByte.Tag = 0 then
        sCMDLine := sCMDLine + ' -U lock:r:"' + GetCurrPath +
          'bin\lockbyte.hex":d';
    end;
  end
  else if Kind = 2 then // erase chip
  begin
    sCMDLine := sCMDLine + ' -e';
  end
  else if Kind = 3 then //verify chip
  begin
    //flash eeprom
    if ckbFlash.Checked then
      sCMDLine := sCMDLine + ' -U flash:v:"' + edFlash.Text + '":i';

    if ckbEerpom.Checked then
      if trim(edEeprom.Text) <> '' then
        sCMDLine := sCMDLine + ' -U eeprom:v:"' + edEeprom.Text + '":i';
  end
  else if Kind = 4 then //read fuse
  begin
    if gbxLowByte.Tag = 0 then
      sCMDLine := sCMDLine + ' -U lfuse:r:"' + GetCurrPath +
        'bin\lowbyte.hex":d';
    if gbxHighByte.Tag = 0 then
      sCMDLine := sCMDLine + ' -U hfuse:r:"' + GetCurrPath +
        'bin\highbyte.hex":d';
    if gbxExtByte.Tag = 0 then
      sCMDLine := sCMDLine + ' -U efuse:r:"' + GetCurrPath +
        'bin\extbyte.hex":d';
    if gbxLockByte.Tag = 0 then
      sCMDLine := sCMDLine + ' -U lock:r:"' + GetCurrPath +
        'bin\lockbyte.hex":d';
  end
  else if Kind = 5 then //write fuse
  begin
    sCMDLine := sCMDLine + ' -u';
    if gbxLowByte.Tag = 0 then
      sCMDLine := sCMDLine + ' -U lfuse:w:0x' + IntToHex(GetFuseState('ckbLow'),
        2) + ':m';
    if gbxHighByte.Tag = 0 then
      sCMDLine := sCMDLine + ' -U hfuse:w:0x' +
        IntToHex(GetFuseState('ckbHigh'), 2) + ':m';
    if gbxExtByte.Tag = 0 then
      sCMDLine := sCMDLine + ' -U efuse:w:0x' + IntToHex(GetFuseState('ckbExt'),
        2) + ':m';
    if gbxLockByte.Tag = 0 then
      sCMDLine := sCMDLine + ' -U lock:w:0x' + IntToHex(GetFuseState('ckbLock'),
        2) + ':m';
  end;

  ExitMode := '';
  if ckbReset.Checked then
    ExitMode := 'reset';

  if ckbNoReset.Checked then
    if ExitMode <> '' then
      ExitMode := ExitMode + ',noreset'
    else
      ExitMode := 'noreset';

  if ckbVCC.Checked then
    if ExitMode <> '' then
      ExitMode := ExitMode + ',vcc'
    else
      ExitMode := 'vcc';

  if ckbNoVCC.Checked then
    if ExitMode <> '' then
      ExitMode := ExitMode + ',novcc'
    else
      ExitMode := 'novcc';

  if ExitMode <> '' then
    sCMDLine := sCMDLine + ' -E ' + ExitMode;
end;

procedure SetBit(N: Byte; var B: Byte);
begin
  B := B or (1 shl N);
end;

procedure ClrBit(N: Byte; var B: Byte);
begin
  B := B and (not (1 shl N));
end;

procedure TMainForm.btnEraseClick(Sender: TObject);
begin
  BuildCMDLine(2);
  JvCreateProcess1.CommandLine := sCMDLine;
  JvCreateProcess1.Run;

  AddLogMsg(sCMDLine, True);
  UpdateButtons;

end;

procedure TMainForm.cbProgrammerChange(Sender: TObject);
begin
  if cbProgrammer.ItemIndex = 0 then
  begin
    cbPort.Items.Clear;
    cbPort.Items.Add('LPT1');
    cbPort.Items.Add('LPT2');

⌨️ 快捷键说明

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