Hi everyone,
I'm working on a function that erases every single byte on eeprom 24LC256 and displays the progression on an lcd screen. My code works fine, but I feel like I'm missing something. I use ISIS to simulate my circuit and the cpu load rises up to 85%. I have a Core i7 and it makes wonder if on the real microcontroller (pic 16f877) it won't be a lot.
I wanted to display a loading bar on the lcd screen so I was able to see the progression in percent. Before adding the loading bar, the process of erasing the eeprom took about 5 minutes, and now it takes 7. So I'd like your opinions on my code and let me know if there was something I could do better.
The function fc_i2c_write(); is just a function I made wich receives the high and low address with the byte to write and uses i2c to write it on the eeprom.
I've had problems with my loops, so I had to set my addrH variable to 0xFF so when it starts the loop and add 1 it will actually starts with the 0x00 address.
Also, I have to use my function twice in my loop, once in my first loop from 0 to 127 to include all the 0x00 low addresses of any high address and a second time in the second loop to include all the other addresses. Same thing here, I feel like I could simplify the code, but I'm not sure how.
For my loading bar, I use the progression variable and add 1 everytime a loop is done. Since I know that there are 32768 bytes, I use a simple division to get the percentage.
With 16 caracters on a single line on the lcd screen I divided 100/16 which gave me 6.25. I then compare my percentage to see if it has reached the treshold of 6.25, if it is the case then I add 6.25 to my new treshold and put a bar on the screen. I'm not sure if that how loading bars should be done though.
There's a lot of conditions and given the number of times the loop is done, it's high on the cpu load, I really would like to reduce my code if it is possible.
You'll find below my code and a screenshot of the result, which displays the address of the eeprom first, then the byte number and then the percentage. Below you can see the loading bar.
Thank you in advance for you help.
View attachment 64606
I'm working on a function that erases every single byte on eeprom 24LC256 and displays the progression on an lcd screen. My code works fine, but I feel like I'm missing something. I use ISIS to simulate my circuit and the cpu load rises up to 85%. I have a Core i7 and it makes wonder if on the real microcontroller (pic 16f877) it won't be a lot.
I wanted to display a loading bar on the lcd screen so I was able to see the progression in percent. Before adding the loading bar, the process of erasing the eeprom took about 5 minutes, and now it takes 7. So I'd like your opinions on my code and let me know if there was something I could do better.
The function fc_i2c_write(); is just a function I made wich receives the high and low address with the byte to write and uses i2c to write it on the eeprom.
I've had problems with my loops, so I had to set my addrH variable to 0xFF so when it starts the loop and add 1 it will actually starts with the 0x00 address.
Also, I have to use my function twice in my loop, once in my first loop from 0 to 127 to include all the 0x00 low addresses of any high address and a second time in the second loop to include all the other addresses. Same thing here, I feel like I could simplify the code, but I'm not sure how.
For my loading bar, I use the progression variable and add 1 everytime a loop is done. Since I know that there are 32768 bytes, I use a simple division to get the percentage.
With 16 caracters on a single line on the lcd screen I divided 100/16 which gave me 6.25. I then compare my percentage to see if it has reached the treshold of 6.25, if it is the case then I add 6.25 to my new treshold and put a bar on the screen. I'm not sure if that how loading bars should be done though.
There's a lot of conditions and given the number of times the loop is done, it's high on the cpu load, I really would like to reduce my code if it is possible.
You'll find below my code and a screenshot of the result, which displays the address of the eeprom first, then the byte number and then the percentage. Below you can see the loading bar.
Thank you in advance for you help.
View attachment 64606
Code:
short int fc_i2c_erase_all()
{
short int addrH, addrL, n;
float p;
float s;
unsigned int progression;
char affiche[10], affiche2[10], affiche3[10], affiche5[10], ii[10];
int i, j;
//char affiche[10];
//char affiche2[10];
char affiche4[] = "0";
Lcd_Cmd(_LCD_CURSOR_OFF);
addrH = 0xFF;
progression = 0;
s = 6.25;
for (i = 0; i <= 127; i++) {
addrH = addrH + 1;
//IntToStr(i, ii);
//Lcd_Out(2,1,ii);
addrL = 0x00;
ShortToHex(addrH, affiche);
Lcd_Out(1,1,affiche);
fc_i2c_write(addrH, addrL, 0x00);
ShortToHex(addrL, affiche2);
Lcd_Out(1,3,affiche2);
//Delay_ms(500);
progression = progression + 1;
for (j = 0; j < 255; j++) {
addrL = addrL + 1;
ShortToHex(addrL, affiche2);
Lcd_Out(1,3,affiche2);
fc_i2c_write(addrH, addrL, 0x00);
progression = progression + 1;
p = progression*100.0/32768.0;
//p = p/32768.0;
IntToStr(progression, affiche3);
Lcd_Out(1,5,affiche3);
IntToStr(p, affiche5);
Lcd_Out(1,10,affiche5);
}
//if (progression*100.0/32768.0 >= s)
if (p >= s)
{
s = s + 6.25;
for (n = 1; n < s/6.25; n++) {
//Lcd_Out(2,n,affiche4);
Lcd_Chr(2, n, 0xFF);
}
}
}
}
Last edited: