Skip to content

Commit 48472ce

Browse files
committed
Initial code commit
1 parent 814ed32 commit 48472ce

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed

eepromIHEX.c

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#include <stdio.h>
2+
3+
unsigned char data[23] = {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,0x10,0x11,0x12,0x13,0x14,0x15,0x16};
4+
5+
int main(void){
6+
unsigned long data_remainig = sizeof(data);
7+
unsigned short counter = 0;
8+
unsigned short address = 0;
9+
10+
// Create the file
11+
FILE *eeprom_file;
12+
eeprom_file = fopen("b.eeprom","w");
13+
14+
while(data_remainig != 0){
15+
16+
unsigned int checksum = 0;
17+
18+
// Byte count y address
19+
if(data_remainig >= 16){
20+
// Start code
21+
fputc(':',eeprom_file);
22+
23+
// Byte count
24+
fputs("10",eeprom_file);
25+
checksum += 0x10;
26+
27+
data_remainig -= 0x10;
28+
address += 0x10;
29+
checksum += (address & 0x00FF) + ((address & 0xFF00)>>8);
30+
31+
// Address
32+
if(address <= 0xF){
33+
fprintf(eeprom_file,"000%X",address);
34+
} else if(address <= 0xFF){
35+
fprintf(eeprom_file,"00%X",address);
36+
} else if(address <= 0xFFF){
37+
fprintf(eeprom_file,"0%X",address);
38+
} else {
39+
fprintf(eeprom_file,"%X",address);
40+
}
41+
42+
// Record type
43+
fputs("00",eeprom_file);
44+
45+
// Data
46+
for(long i = 0; i < 16; i++){
47+
char string[2];
48+
if(data[16 * counter + i]==0){
49+
fputs("00",eeprom_file);
50+
} else if(data[16 * counter + i] <= 0x0F) {
51+
fprintf(eeprom_file,"0%X", data[16 * counter + i]);
52+
checksum += data[16*counter + i];
53+
} else {
54+
fprintf(eeprom_file,"%X", data[16 * counter + i]);
55+
checksum += data[16*counter + i];
56+
}
57+
}
58+
counter++;
59+
} else {
60+
// Start code
61+
fputc(':', eeprom_file);
62+
63+
// Byte count
64+
fprintf(eeprom_file,"0%X", (unsigned int)data_remainig);
65+
checksum += (unsigned int) data_remainig;
66+
67+
// Address
68+
address += data_remainig;
69+
checksum += (address & 0x00FF) + ((address & 0xFF00)>>8);
70+
71+
if(address <= 0xF){
72+
fprintf(eeprom_file,"000%X",address);
73+
} else if(address <= 0xFF){
74+
fprintf(eeprom_file,"00%X",address);
75+
} else if(address <= 0xFFF){
76+
fprintf(eeprom_file,"0%X",address);
77+
} else {
78+
fprintf(eeprom_file,"%X",address);
79+
}
80+
81+
// Record type
82+
fputs("00",eeprom_file);
83+
84+
// Data
85+
for(int i = 0; i < (unsigned int)data_remainig; i++){
86+
char string[2];
87+
if(data[16 * counter + i]==0){
88+
fputs("00",eeprom_file);
89+
} else if(data[16 * counter + i] <= 0x0F) {
90+
fprintf(eeprom_file,"0%X", data[16 * counter + i]);
91+
checksum += data[16*counter + i];
92+
} else {
93+
fprintf(eeprom_file,"%X", data[16 * counter + i]);
94+
checksum += data[16*counter + i];
95+
}
96+
97+
}
98+
data_remainig = 0;
99+
}
100+
// Checksum
101+
// Check checksum printf("%X\t%X\n", checksum, ((~checksum)+1) & 0xFF);
102+
fprintf(eeprom_file,"%X\n", ((~checksum)+1) & 0xFF);
103+
}
104+
105+
// EOF
106+
fputs(":00000001FF", eeprom_file);
107+
fclose(eeprom_file);
108+
}

0 commit comments

Comments
 (0)