5)C程序的准备
//发送FLASH程序文件到单片机中去
BOOL SendFileToFlash()
{
unsigned char ch;
FILE *fp;
long int flen,wi=0;
int i,j;
if ((fp = fopen("flashwl","rb"))==NULL)
{
return FALSE;
}
//初使化串口,且与单片机系统握手
InitialCommuClass();
//发送数据
for (wi=0,j=0;wi<flen;wi++)
{
if (feof(fp)!=0)//到达文件结束
break;
fread(&ch,sizeof(unsigned char),1,fp);
for (;;)
if (SendAByte(ch)==TRUE)
break;
Printf("%ld bytes is transmitted!
",wi); }
fclose(fp);
for (;;)
{
Printf("All bytes is transmitted!
");
}
}
//转换上载文件为可下载的HEX目标文件
BOOL ConvertFileToFlash()
{
unsigned char ch,ru;
FILE *fp,*fpw;
long int flen,wi=0;
int i,j;
if ((fp = fopen("SOURCEC","rb"))==NULL)
{
return FALSE;
}
if ((fpw = fopen("HEXOBJECT","wb"))==NULL)
{
return FALSE;
}
for (wi=0,j=0;;wi++)
{
if (feof(fp)!=0)
break;
ru=0;
fread(&ch,sizeof(unsigned char),1,fp);
if ((ch>=0x30) && (ch<=0x39))
ru+=(ch-0x30)*16;
else if ((ch>=0x61) && (ch<=0x66))
ru+=((ch-0x61)+0x0a)*16;
fread(&ch,sizeof(unsigned char),1,fp);
if ((ch>=0x30) && (ch<=0x39))
ru+=ch-0x30;
else if ((ch>=0x61) && (ch<=0x66))
ru+=(ch-0x61)+0x0a;
fwrite(&ru,sizeof(unsigned char),1,fpw);
fread(&ch,sizeof(unsigned char),1,fp);/*0x0d*/
fread(&ch,sizeof(unsigned char),1,fp);/*0x0d*/
fread(&ch,sizeof(unsigned char),1,fp);/*0x0a*/
j++;
if (j==255)
{
j=0;
fread(&ch,sizeof(unsigned char),1,fp);
fread(&ch,sizeof(unsigned char),1,fp);
fread(&ch,sizeof(unsigned char),1,fp);
fread(&ch,sizeof(unsigned char),1,fp);
fread(&ch,sizeof(unsigned char),1,fp);
continue;
}
}
fclose(fp);fclose(fpw);
}
结束语
以上所介绍的FLASH自动升级程序的PC部分在VC6.0及TC2.0中开发,并与805lFXXX系列的单片机联机成功,并最后用于在用户处的单片机FLASH程序升级。
作者: 李小路 时间: 2022-5-28 08:51
谢谢分享!