理论上来说用不用外部RAM不会产生额外的代码块。所以我对你说的“开了外部RAM后不好用”有所怀疑。请LZ把开外部RAM之前和之后的hex文件贴上来,大家分析分析。
另外,hex的格式其实不复杂,可以认为是二进制代码的一种格式化文本表述。
贴个TurboC 2.0下的转换源程序。此程序不太规范、适用范围也很狭窄,不过可以参考一下。
/**********************************************************************
文件名:HEX2BIN
功能: 将Intel HEX格式的文件转换为二进制文件
不计算校验和
***********************************************************************/
#include "stdio.h"
#include "fcntl.h"
#include "io.h"
#define ROWSIZE 80
int SourceP; /*源文件指针*/
int TargetP; /*目标文件指针*/
unsigned char source[ROWSIZE]; /*声明数组,用以存放行文本*/
unsigned char target[32]; /*存放目标代码*/
unsigned char ErrFlag = 0;
unsigned char EndFlag = 0;
unsigned char Text2Bin(unsigned char);
void main()
{
char In_FileName[15], Out_FileName[15];
unsigned char i;
long SrcOff = 0;
long TgtOff = 0;
struct
{
unsigned char ByteNum;
unsigned char TotalNum;
}Row;
printf("Input HEX filename:\n");
scanf("%s",In_FileName);
strcpy(Out_FileName, In_FileName);
strcat(In_FileName, ".hex");
strcat(Out_FileName, ".bin");
printf("Source HEX file:%s\n",In_FileName);
printf("Target BIN file:%s\n",Out_FileName);
/*以无缓冲方式打开源文件*/
SourceP = open(In_FileName, O_BINARY | O_RDONLY);
if(SourceP==-1)
{
printf("Source not exist! Enter to return\n");
getch();
exit(1);
}
/*以无缓冲方式打开目标文件*/
TargetP = open(Out_FileName, O_BINARY | O_RDWR);
if(TargetP==-1)
{
TargetP = _creat(Out_FileName, 1);
if(TargetP==-1)
{
close(SourceP);
printf("Can not open target! Enter to return.\n");
getch();
exit(1);
}
}
/*读入源文件文本,转换为二进制写入目标文件*/
while(EndFlag==0)
{
lseek(SourceP, SrcOff, SEEK_SET);
read(SourceP, source, ROWSIZE);
if(source[0]!=':')
{
EndFlag = 1;
break;
}
for(i = 0; i < ROWSIZE; i++) /*找到下一个有效行*/
{
if(source[i]==':')
{
if((source[i-2]==0x0D)&&(source[i-1]==0x0A))
{
break;
}
}
}
if(i==ROWSIZE) /*找不到,则结束转换过程*/
{
EndFlag = 1;
break;
}
SrcOff = SrcOff + i; /*源文件偏移量指向下一行*/
Row.ByteNum = Text2Bin(1); /*代码字节数*/
if(Row.ByteNum==0) /*行字节数为0,是标准的结束方式*/
{
EndFlag = 1;
break;
}
Row.TotalNum = i; /*文本行字节数*/
TgtOff = Text2Bin(3);
TgtOff = TgtOff * 0x100;
TgtOff = TgtOff + Text2Bin(5);
if((TgtOff<0)||(TgtOff>0xFFFF)) /*校验偏移量*/
{
close(SourceP);
close(TargetP);
printf("Out of range:%d Enter to return.\n",TgtOff);
getch();
exit(1);
}
for(i = 0; i < Row.ByteNum; i++) /*将文本转换为二进制存入数组*/
{
target[i] = Text2Bin(9 + 2 * i);
}
lseek(TargetP, TgtOff, SEEK_SET);
write(TargetP, target, Row.ByteNum);
}
close(SourceP);
close(TargetP);
printf("Conversion complete.\n");
}
unsigned char Text2Bin(unsigned char start)
{
unsigned char m, n;
m = source[start];
if((m >= '0')&&(m <= '9'))
{
m -= '0';
}
else if((m >= 'A')&&(m <= 'F'))
{
m = m - 'A' + 10;
}
else if((m >= 'a')&&(m <= 'f'))
{
m = m - 'a' + 10;
}
else
{
ErrFlag = 1;
}
n = m << 4;
m = source[start + 1];
if((m >= '0')&&(m <= '9'))
{
m -= '0';
}
else if((m >= 'A')&&(m <= 'F'))
{
m = m - 'A' + 10;
}
else if((m >= 'a')&&(m <= 'f'))
{
m = m - 'a' + 10;
}
else
{
ErrFlag = 1;
}
n = n + (m & 0x0F);
return n;
}
|