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

📄 unit2.cpp

📁 ISP 并口下载工具
💻 CPP
字号:
//---------------------------------------------------------------------------


#pragma hdrstop
#include "Unit5.h"
#include "Unit2.h"
#include "Unit1.h"
   //Memo1的每一行读取出来,根据*.hex文件的格式(: ll aaaa tt dd...dd cc)
   //进行判断文件是否正确 :
   //a、在该行中,第一个字符是不是":" ,如果不是,则该文件出错。
   //b、第2和第3个字符是该行程序代码的代码长度
   //c、第4和第5组成一个字节(高位)、第6和第7组成一个字节(低位);高低这两位
   //   组成程序代码的第一个数据的地址。
   //d、第8和第9个字符是记录的类型,00表示有效数据,01表示文件结束
   //e、第10个字符以上至倒数第3个字符是代码数据(每2个组成一个字节)
   //f、倒数第2和第1个字符组成一个校验和。如果ll+aa+aa+dd+...+dd+cc=0 ,则没有错误。
   //将dd...dd(dd经过string to int的转换后)依次放到以aaaa为地址(索引号)的
   //rom_code中
//---------------------------------------------------------------------------
 unsigned char chartobyte(char ch)
 {
 if(ch>0x60)
   ch=ch-0x57;
  else if(ch>0x39)
   ch=ch-0x37;
  else
   ch=ch-0x30;
  return ch;  
 }
 //===============================================
 unsigned int getmaxadress(void)
 {
   unsigned int maxadress,mark;
   unsigned char tempA,tempB,tempC,tempD;
   unsigned int adress;
   mark=0; //记录最大地址的行号
   maxadress=0;
   adress=0;  //中间临时地址
   tempA=tempB=tempC=tempD=0;//两个八位的地址组合为十六位地址
   AnsiString sch;
   for(int line=0;line<Form1->Memo1->Lines->Count;line++)
    {
         sch=Form1->Memo1->Lines->Strings[line];
         tempA=chartobyte(sch[4]);
         tempB=chartobyte(sch[5]);
         tempC=chartobyte(sch[6]);
         tempD=chartobyte(sch[7]);

         adress=tempA*0x1000+tempB*0x100+tempC*0x10+tempD;
         if(adress>maxadress)
          {
           maxadress=adress;
           mark=line;
          }
     }  //end for
    unsigned char i,j,dataleng;;
    AnsiString marckline;
    marckline=Form1->Memo1->Lines->Strings[mark];
    i=chartobyte(marckline[2]);
    j=chartobyte(marckline[3]);
    dataleng=i*0x10+j;
    maxadress=maxadress+dataleng;
    return maxadress;
 }
 //====================================================
unsigned char * hextobin(unsigned int & fileleng)
  {
   unsigned int maxromsize;
   unsigned char *temppA;
   unsigned char *temppB;
   maxromsize=getmaxadress();
   temppA= new unsigned char[maxromsize];
   temppB=temppA;
   for(unsigned int i=0;i<=maxromsize;i++)
    {
     *temppA=0xff;
     temppA++;
    }
   temppA=temppB;
   AnsiString sch;
   unsigned char ten,one,dataleng,sum;
   unsigned char tempa,tempb,tempc,tempd;
   unsigned int beginadress;
   for(int line=0;line<Form1->Memo1->Lines->Count;line++)   //begin for1
    {
      sch=Form1->Memo1->Lines->Strings[line];
      if(sch[1]==':')   //begin if1
       {
          ten=chartobyte(sch[2]);
          one=chartobyte(sch[3]);
          dataleng=ten*0x10+one;
          tempa=chartobyte(sch[4]);
          tempb=chartobyte(sch[5]);
          tempc=chartobyte(sch[6]);
          tempd=chartobyte(sch[7]);
          beginadress=tempa*0x1000+tempb*0x100+tempc*0x10+tempd;
          sum=dataleng+(tempa*0x10+tempb)+(tempc*0x10+tempd);

          ten=chartobyte(sch[8]);
          one=chartobyte(sch[9]);
          tempa=ten*0x10+one;
          if(tempa==0) //是数据    //begin if2
           {
            for(unsigned char i=0;i<dataleng;i++)
             {
               ten=chartobyte(sch[2*i+10]);
               one=chartobyte(sch[2*i+11]);
               tempa=ten*0x10+one;
               temppA[beginadress+i]=tempa;
               sum=sum+tempa;
             }
            ten=chartobyte(sch[2*dataleng+10]);
            one=chartobyte(sch[2*dataleng+11]);
            tempa=ten*0x10+one;
            sum=sum+tempa;
            if(sum!=0) //校验和不为零,说明文件错误       //begin if3
             {
               Form1->Memo2->Lines->Add("文件中第"+IntToStr(line+1)+"行的校验和不为零,文件错误");
               delete [] temppB;
               fileleng=0;
               return NULL;
             }      //end if3
           }        //end if2
          else if(tempa==1)         //是文件结束
           {
             sum=sum+tempa;
             ten=chartobyte(sch[10]);
             one=chartobyte(sch[11]);
             tempa=ten*0x10+one;
             sum=sum+tempa;
             if(sum!=0)
              {
               Form1->Memo2->Lines->Add("文件最后一行校验和不为零,文件错误");
               delete [] temppB;
               return NULL;
              }
             else
               {
                 Form1->Memo2->Lines->Add("文件成功转换为二进制数据!");
                 Form1->Memo2->Lines->Add("文件占用FLASH ROM:"+IntToStr(maxromsize)+" BYTE");
                 Form1->Memo2->Lines->Add("该单片机程序在Flash Rom中的最大地址是:"+IntToStr(maxromsize-1)+" 单元");
                 fileleng=maxromsize;  //设置ROM数据长度
                 return temppB;
                }

           }
       }     //end if1
      else  // 错误的文件
       {
         Form1->Memo2->Lines->Add("文件每行不是':'开始,不是HEX文件!");
         delete [] temppB ;
         fileleng=0;
         return NULL;
       }
    }  //end for1
 }
//------------------------------------------------------------------------------
void bintohex(unsigned char *buff)
{
 int i,j,k;
 int counter;
 unsigned char checksum;
 unsigned char addresschecksuml;
 unsigned char addresschecksumh;
 unsigned char temp;
 AnsiString lineformat;
 AnsiString address;
 i=getromsize();
 for(counter=(i-1);counter>0;counter--)
  {
    if(buff[counter]!=0xff)
     break;
  }
 counter++;  //如果counter加一,表示数据的数量
 if(counter%16!=0)
   j=counter/16+1; //每16个数据分一行
 else
   j=counter/16;
 Form1->Memo1->Clear();
 for(i=0;i<j;i++)
  {
    checksum=0;
    lineformat=":10";
    address=IntToHex((int)0x10*i,4);
    lineformat=lineformat+address+"00";
    k=0x10*i;
    addresschecksuml=k;
    addresschecksumh=k >> 8;
    checksum=0x10+addresschecksuml+addresschecksumh;
    for(k=0;k<16;k++)
     {
       lineformat=lineformat+IntToHex((int)buff[0x10*i+k],2);
       checksum=checksum+buff[0x10*i+k];
     }
    temp=0-checksum;
    lineformat=lineformat+IntToHex((int)temp,2);
    Form1->Memo1->Lines->Add(lineformat);
  }
 lineformat=":00000001FF";
 Form1->Memo1->Lines->Add(lineformat);
}
//==============================================================================
#pragma package(smart_init)


⌨️ 快捷键说明

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