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

📄 main.pas

📁 单片机isp编程上位机源代码
💻 PAS
📖 第 1 页 / 共 2 页
字号:
    cbPort.Items.Add('LPT3');
  end
  else
  begin
    cbPort.Items.Clear;
    cbPort.Items.Add('USB');
  end;
  cbPort.ItemIndex := 0;
end;

procedure TMainForm.btnProgramClick(Sender: TObject);
begin
  if not FileExists(edFlash.Text) then
  begin
    MsgBox('Flash文件不存在,请重新输入正确的文件路径!');
    Exit;
  end;

  if (trim(edEeprom.Text) <> '') and not FileExists(edEeprom.Text) then
  begin
    MsgBox('EEPROM文件不存在,请重新输入正确的文件路径!');
    Exit;
  end;
  UpdateButtons;
  BuildCMDLine(0);
  JvCreateProcess1.CommandLine := sCMDLine;
  JvCreateProcess1.Run;
  AddLogMsg(sCMDLine, True);

end;

procedure TMainForm.btnReadClick(Sender: TObject);
begin
  JvCreateProcess1.Tag := 1;
  BuildCMDLine(1);

  JvCreateProcess1.CommandLine := sCMDLine;
  JvCreateProcess1.Run;
  UpdateButtons;
  AddLogMsg(sCMDLine, True);
end;

procedure TMainForm.sbnDefaultClick(Sender: TObject);
begin
  gbxFuse.Caption := '熔丝位设置';
  cbMCUChange(cbMCU);
end;

function TMainForm.GetFuseState(FuseName: string): Byte;
var
  i: Byte;
begin
  Result := 0;
  if FuseName <> 'ckbLock' then
  begin
    //m48 ,m88,m168   extbyte 0x63;
    if (FuseName = 'ckbExt') and (cbMCU.ItemIndex in [3, 5, 7]) then
    begin
      for i := 0 to 7 do
        if (TCheckBox(FindComponent(FuseName + IntToStr(i))).Tag = 1)
          or TCheckBox(FindComponent(FuseName + IntToStr(i))).Checked then
          ClrBit(i, Result)
        else
          SetBit(i, Result);
    end
    else
    begin
      for i := 0 to 7 do
        if TCheckBox(FindComponent(FuseName + IntToStr(i))).Checked then
          ClrBit(i, Result)
        else
          SetBit(i, Result);
    end;
  end
  else
  begin
    for i := 0 to 5 do
    begin
      if TCheckBox(FindComponent(FuseName + IntToStr(i))).Checked then
        ClrBit(i, Result)
      else
        SetBit(i, Result);
    end;
  end;
end;

procedure TMainForm.SetFuseState(FuseName: string; FuseValue: Byte);
var
  B: Byte;
  i, C: Byte;
begin
  B := FuseValue;
  for i := 0 to 7 do
  begin
    C := 1 shl i;
    if TCheckBox(FindComponent(FuseName + IntToStr(i))).Tag <> 1 then
    begin
      if ((B and C) > 0) then
        TCheckBox(FindComponent(FuseName + IntToStr(i))).Checked := False
      else
        TCheckBox(FindComponent(FuseName + IntToStr(i))).Checked := True;
    end
    else
    begin
      TCheckBox(FindComponent(FuseName + IntToStr(i))).Checked := False;
    end;
  end;
end;

procedure TMainForm.sbnSaveClick(Sender: TObject);
var
  F: string;
  S: string;
  List: TStringList;
begin
  List := TStringList.Create;
  if SaveDialog1.Execute then
  begin
    F := SaveDialog1.FileName;
    List.Add(SaveFuse);
    List.SaveToFile(F);
    S := ExtractFileName(F);
    S := copy(S, 1, pos('.', S) - 1);
    gbxFuse.Caption := '熔丝位设置 - ' + S;
  end;
  List.Free;
end;

procedure TMainForm.LoadFuse(S: string);
var
  XML: IXMLDOMDocument;
  Node: IXMLDOMNode;
begin
  XML := CoDOMDocument.Create;
  XML.load(S);
  Node := XML.documentElement.selectSingleNode('mcu');
  if Node <> nil then
  begin
    cbMCU.ItemIndex:=StrToIntDef(Node.text,0);
    cbMCUChange(cbMCU);
    Node := XML.documentElement.selectSingleNode('fuse/lowbyte');
    if Node <> nil then
    begin
      gbxLowByte.Tag := 0;
      SetFuseState('ckbLow', StrToInt(Node.text));
    end
    else
    begin
      gbxLowByte.Tag := 1;
      SetFuseState('ckbLow', $FF);
    end;

    Node := XML.documentElement.selectSingleNode('fuse/highbyte');
    if Node <> nil then
    begin
      gbxHighByte.Tag := 0;
      SetFuseState('ckbHigh', StrToInt(Node.text));
    end
    else
    begin
      gbxHighByte.Tag := 1;
      SetFuseState('ckbHigh', $FF);
    end;

    Node := XML.documentElement.selectSingleNode('fuse/extbyte');
    if Node <> nil then
    begin
      gbxExtByte.Tag := 0;
      SetFuseState('ckbExt', StrToInt(Node.text));
    end
    else
    begin
      gbxExtByte.Tag := 1;
      SetFuseState('ckbExt', $FF);
    end;

    Node := XML.documentElement.selectSingleNode('fuse/lockbyte');
    if Node <> nil then
    begin
      gbxLockByte.Tag := 0;
      SetFuseState('ckbLock', StrToInt(Node.text));
    end
    else
    begin
      gbxLockByte.Tag := 1;
      SetFuseState('ckbLock', $FF);
    end;
  end
  else
  begin
    MsgBox('提示', '文件格式不对,请重选择文件!', MB_OK + MB_ICONINFORMATION);
  end;
  XML := nil;

end;

function TMainForm.SaveFuse: string;
var
  S: string;
begin
  S := '<mcu>' + IntToStr(cbMCU.ItemIndex) + '</mcu>';
  S := S + '<fuse><lowbyte>' + IntToStr(GetFuseState('ckbLow')) + '</lowbyte>';
  S := S + '<highbyte>' + IntToStr(GetFuseState('ckbHigh')) + '</highbyte>';
  S := S + '<extbyte>' + IntToStr(GetFuseState('ckbExt')) + '</extbyte>';
  S := S + '<lockbyte>' + IntToStr(GetFuseState('ckbLock')) +
    '</lockbyte></fuse>';
  Result := '<meng>' + S + '</meng>';
end;

procedure TMainForm.AddLogMsg(const Text: string; IsClear: Boolean);
begin
  if IsClear then
    mmOutput.Lines.Clear;
  mmOutput.Lines.Add('' + Text);

end;

procedure TMainForm.ChangeMsg(const Text: string);
begin
  mmOutput.Lines[mmOutput.Lines.Count - 1] := '' + Text;
end;

procedure TMainForm.JvCreateProcess1Read(Sender: TObject; const S: string;
  const StartsOnNewLine: Boolean);
begin
  if StartsOnNewLine then
    AddLogMsg(S, False)
  else
    ChangeMsg(S);
end;

procedure TMainForm.UpdateButtons;
begin
  btnErase.Enabled := JvCreateProcess1.State = psReady;
  btnProgram.Enabled := JvCreateProcess1.State = psReady;
  btnVerify.Enabled := JvCreateProcess1.State = psReady;
  btnRead.Enabled := JvCreateProcess1.State = psReady;
  btnExit.Visible := JvCreateProcess1.State = psReady;
  btnStop.Visible := JvCreateProcess1.State <> psReady;

  sbnRead.Enabled := JvCreateProcess1.State = psReady;
  sbnWrite.Enabled := JvCreateProcess1.State = psReady;
  sbnDefault.Enabled := JvCreateProcess1.State = psReady;
  sbnLoad.Enabled := JvCreateProcess1.State = psReady;
  sbnSave.Enabled := JvCreateProcess1.State = psReady;

end;

procedure TMainForm.JvCreateProcess1Terminate(Sender: TObject;
  ExitCode: Cardinal);
var
  List: TStringList;
  B: Integer;
begin
  UpdateButtons;

  if JvCreateProcess1.Tag = 1 then
  begin
    List := TStringList.Create;

    // low byte
    if FileExists(GetCurrPath + 'bin\lowbyte.hex') then
    begin
      List.LoadFromFile(GetCurrPath + 'bin\lowbyte.hex');
      if List.Count > 0 then
        B := StrToIntDef(List[0], $FF)
      else
        B := $FF;
      if gbxLowByte.Tag = 0 then
        SetFuseState('ckbLow', B);
      DeleteFile(GetCurrPath + 'bin\lowbyte.hex')
    end;

    // high byte
    if FileExists(GetCurrPath + 'bin\highbyte.hex') then
    begin
      List.LoadFromFile(GetCurrPath + 'bin\highbyte.hex');
      if List.Count > 0 then
        B := StrToIntDef(List[0], $FF)
      else
        B := $FF;
      if gbxHighByte.Tag = 0 then
        SetFuseState('ckbHigh', B);
      DeleteFile(GetCurrPath + 'bin\highbyte.hex')
    end;

    // ext byte
    if FileExists(GetCurrPath + 'bin\extbyte.hex') then
    begin
      List.LoadFromFile(GetCurrPath + 'bin\extbyte.hex');
      if List.Count > 0 then
        B := StrToIntDef(List[0], $FF)
      else
        B := $FF;
      if gbxExtByte.Tag = 0 then
        SetFuseState('ckbExt', B);
      DeleteFile(GetCurrPath + 'bin\extbyte.hex')
    end;

    // lock byte
    if FileExists(GetCurrPath + 'bin\lockbyte.hex') then
    begin
      List.LoadFromFile(GetCurrPath + 'bin\lockbyte.hex');
      if List.Count > 0 then
        B := StrToIntDef(List[0], $FF)
      else
        B := $FF;
      if gbxLockByte.Tag = 0 then
        SetFuseState('ckbLock', B);
      DeleteFile(GetCurrPath + 'bin\lockbyte.hex')
    end;

    List.Free;

    JvCreateProcess1.Tag := 0;
  end;
end;

procedure TMainForm.btnStopClick(Sender: TObject);
begin
  JvCreateProcess1.Terminate;
end;

procedure TMainForm.btnVerifyClick(Sender: TObject);
begin
  if not FileExists(edFlash.Text) then
  begin
    MsgBox('Flash文件不存在,请重新输入正确的文件路径!');
    Exit;
  end;

  if (trim(edEeprom.Text) <> '') and not FileExists(edEeprom.Text) then
  begin
    MsgBox('EEPROM文件不存在,请重新输入正确的文件路径!');
    Exit;
  end;
  BuildCMDLine(3);
  JvCreateProcess1.CommandLine := sCMDLine;
  JvCreateProcess1.Run;
  UpdateButtons;
  AddLogMsg(sCMDLine, True);
end;

procedure TMainForm.LoadHistroy;
var
  ini: TIniFile;
begin
  ini := TIniFile.Create(GetCurrPath + 'config.ini');
  cbProgrammer.ItemIndex := ini.ReadInteger('System', 'programmer', 0);
  cbProgrammerChange(nil);
  cbPort.ItemIndex := ini.ReadInteger('System', 'port', 0);
  cbMCU.ItemIndex := ini.ReadInteger('System', 'mcu', 0);

  edFlash.FileName := ini.ReadString('System', 'flash', '');
  edEeprom.FileName := ini.ReadString('System', 'eeprom', '');
  ini.Free;
end;

procedure TMainForm.SaveHistroy;
var
  ini: TIniFile;
begin
  ini := TIniFile.Create(GetCurrPath + 'config.ini');
  ini.WriteInteger('System', 'programmer', cbProgrammer.ItemIndex);
  ini.WriteInteger('System', 'port', cbPort.ItemIndex);
  ini.WriteInteger('System', 'mcu', cbMCU.ItemIndex);

  ini.WriteString('System', 'flash', edFlash.FileName);
  ini.WriteString('System', 'eeprom', edEeprom.FileName);

  ini.Free;
end;

procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  SaveHistroy;
  OutList.Free;
end;

procedure TMainForm.CheckBox1Click(Sender: TObject);
begin
  if CheckBox1.Checked then
    FormStyle := fsStayOnTop
  else
    FormStyle := fsNormal;
end;

procedure TMainForm.InstallGiveIO;
begin
  OutList.Clear;
  JvCreateProcess2.Tag := 0;
  JvCreateProcess2.CommandLine := GetCurrPath
    + 'bin\install_giveio.bat';
  JvCreateProcess2.Run;
end;

procedure TMainForm.RemoveGiveIO;
begin
  OutList.Clear;
  JvCreateProcess2.Tag := 0;
  JvCreateProcess2.CommandLine := GetCurrPath
    + 'bin\remove_giveio.bat';
  JvCreateProcess2.Run;
end;

procedure TMainForm.StatusGiveIO;
begin
  OutList.Clear;
  JvCreateProcess2.Tag := 1;
  JvCreateProcess2.CommandLine := GetCurrPath
    + 'bin\status_giveio.bat';
  JvCreateProcess2.Run;
end;

procedure TMainForm.JvCreateProcess2Terminate(Sender: TObject;
  ExitCode: Cardinal);
begin
  if JvCreateProcess2.Tag = 1 then
    if not CheckStatusGiveIO('ok.') then
      InstallGiveIO;

end;

procedure TMainForm.JvCreateProcess2Read(Sender: TObject; const S: string;
  const StartsOnNewLine: Boolean);
begin
  if JvCreateProcess2.Tag = 1 then
    if StartsOnNewLine then
      OutList.Add(S)
    else
      OutList.Strings[OutList.Count - 1] := S;
end;

function TMainForm.CheckStatusGiveIO(S: string): Boolean;
var
  i: Integer;
begin
  Result := False;
  for i := OutList.Count - 1 downto 0 do
    if Pos(S, OutList[i]) > 0 then
    begin
      Result := True;
      Break;
    end;
end;

function TMainForm.GetCurrPath: string;
begin
  Result := ExtractFilePath(Application.ExeName);
end;

procedure TMainForm.ckbLowClick(Sender: TObject);
begin
  lbLow.Caption := '0x' + IntToHex(GetFuseState('ckbLow'), 2);
end;

procedure TMainForm.ckbExtClick(Sender: TObject);
begin
  lbExt.Caption := '0x' + IntToHex(GetFuseState('ckbExt'), 2);
end;

procedure TMainForm.ckbHighClick(Sender: TObject);
begin
  lbHigh.Caption := '0x' + IntToHex(GetFuseState('ckbHigh'), 2);
end;

procedure TMainForm.ckbLockClick(Sender: TObject);
begin
  lbLock.Caption := '0x' + IntToHex(GetFuseState('ckbLock'), 2);
end;

procedure TMainForm.InitCheckBox;
var
  i: Integer;
begin
  for i := 0 to 7 do
  begin
    TCheckBox(FindComponent('ckbLow' + IntToStr(i))).OnClick := ckbLowClick;
    TCheckBox(FindComponent('ckbHigh' + IntToStr(i))).OnClick := ckbHighClick;
    TCheckBox(FindComponent('ckbExt' + IntToStr(i))).OnClick := ckbExtClick;
    TCheckBox(FindComponent('ckbLock' + IntToStr(i))).OnClick := ckbLockClick;
  end;
end;

procedure TMainForm.sbnReadClick(Sender: TObject);
begin
  gbxFuse.Caption := '熔丝位设置';
  JvCreateProcess1.Tag := 1;
  BuildCMDLine(4);
  JvCreateProcess1.CommandLine := sCMDLine;
  JvCreateProcess1.Run;
  UpdateButtons;
  AddLogMsg(sCMDLine, True);

end;

procedure TMainForm.sbnWriteClick(Sender: TObject);
begin
  BuildCMDLine(5);
  JvCreateProcess1.CommandLine := sCMDLine;
  JvCreateProcess1.Run;
  UpdateButtons;
  AddLogMsg(sCMDLine, True);
end;
                                                              
procedure TMainForm.sbnLoadClick(Sender: TObject);
var
  F: string;
  S: string;
begin
  if OpenDialog1.Execute then
  begin
    F := OpenDialog1.FileName;
    S := ExtractFileName(F);
    S := copy(S, 1, Pos('.', S) - 1);
    gbxFuse.Caption := '熔丝位设置 - ' + S;
    LoadFuse(F);
  end;
end;

end.

⌨️ 快捷键说明

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