I wrote a download program for Atmel's S5X before, which supports HEX format files, so I posted this program. The program means to convert the input HEX file into a BIN format file and store it in the file. Note that it does not support 64K extension mode.
int CFlashP51App::HexToBin(CString hexfile, CString binfile)
{
CFile fex, fbin;
CString pBuffer;
BYTE len, len1, len2, len_at_max=0;
int start_addr, start_addr_max=0;
int nEnd, n, bin_length=0;
if(!(fhex.Open(hexfile, CFile::modeRead, NULL))) //// Open the file
return -1;
if(!(fbin.Open(binfile, CFile::modeCreate|CFile::modeWrite, NULL)))
return -1;
char *buffer = new char [fhex.GetLength()]; //// Allocate memory
fhex.Read(buffer, fhex.GetLength());
pBuffer = buffer;
nEnd = pBuffer.Find(":00000001"); //// Use this to determine if the file format is HEX
if(nEnd < 0) //First determine the end of the file
{
// AfxMessageBox("*.Hex file conversion failed (\":00000001\" record not found)!");
return -2;
}
n = 0;
while(n < nEnd) // First get the length of the bin file
{
if(buffer[n] == ':')
{
CharToByte(&buffer[n+1], &len);
CharToByte ( & buffer [ n + 3 ] , & len1 ) ;
CharToByte(&buffer[n+5], &len2);
start_addr = (len1<<8) + len2; // starting address
/*
* Some HEX files have "holes" in the middle, and only the size of the previous row of data is calculated.
* Errors may occur, so get the maximum address value and the row with the maximum address value
* The size of the BIN file can be obtained by using the data size. In fact, this method should be used accurately.
* The method of the surface is not removed, it can be removed
*
*/
if(start_addr >= start_addr_max)
{
start_addr_max = start_addr;
len_at_max = len;
}
bin_length += len;
n += len*2+11; // No carriage return symbol
}
else
n++;
}
start_addr_max += len_at_max;
if(start_addr_max >= bin_length)
bin_length = start_addr_max;
BYTE *bbuf = new BYTE [bin_length]; //// Allocate memory
for(int i=0; i bbuf[i] = 0xff; n = 0; while(n < nEnd) // Write data to the BIN file buffer { if(buffer[n] == ':') { CharToByte(&buffer[n+1], &len); CharToByte ( & buffer [ n + 3 ] , & len1 ) ; CharToByte(&buffer[n+5], &len2); start_addr = (len1<<8) + len2; // starting address for(unsigned int i=0; i { CharToByte(&buffer[n+9+i*2], &len1); bbuf[start_addr+i] = len1; } n += len*2+11; // No carriage return symbol } else n++; } fbin.Write(bbuf, bin_length); fhex.Close(); fbin.Close(); delete [] buffer; delete [] bbuf; return bin_length; } BOOL CFlashP51App::CharToByte(char *pChar, BYTE *pByte) { char h,l; h = pChar[0]; //high 4 bits l = pChar[1]; //lower 4 bits if(l>='0' && l<='9') l = l - '0'; else if(l>='a' && l<='f') l = l - 'a' + 0xa; else if(l>='A' && l<='F') l = l - 'A' + 0xa; else return FALSE; if(h>='0' && h<='9') h = h - '0'; else if(h>='a' && h<='f') h = h - 'a' + 0xa; else if(h>='A' &&h <='F') h = h - 'A' + 0xa; else return FALSE; *pByte = (BYTE)h*16 + l; return TRUE; }
Previous article:Homemade MCU Part 4… LCD1602 Driver
Next article:The MCU program emulator cannot run normally after being correctly burned
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Brief Analysis of Automotive Ethernet Test Content and Test Methods
- How haptic technology can enhance driving safety
- Study Notes: GPIO Operation of TMS320C6748
- The sensor is connected via Bluetooth and then reads the data from the mpu
- EEWORLD University Hall----Project Production: Intelligent Access Control System
- Ask for advice
- Which domestic half-bridge driver product is the best?
- Study on TMS570LS3137
- AT32F425-Evaluation Report-Upper Computer Development 04
- The quickest way to set the plug-in through-hole pin pad of a multi-layer board to appear on only one side in Altium Designer
- Can 'ice' be used as a semiconductor material?
- 【NUCLEO-L552ZE Review】- 8 : TrustZone