[SOLVED] How to find first free address in eeprom memory

Status
Not open for further replies.

polona1010

Member level 1
Joined
Apr 17, 2013
Messages
40
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,524
How to find first free address in internal eeprom memory of 18f45k22 and write some data?

Need this in Mikro c
 

I think, it will be so difficult to get first free space of Internal EEPROM. You can do that but may not be correct result. If you know the EEPROM address and want to check if it is free or data contained, you can do that by using eeprom read function(ee_read function in Hi-Tech C). Instead, You can declare eeprom variable and check if it is free. If free write DATA into that space. This could be easy and effective. You can google "internal eeprom read, write + microC".
 

Can I read each address and compare it with FF and if true then this is first free address. I dont know how to do this in mikro c.
 

yes. sure. For example, read address and store the value into local variable and compare.
Code:
if (local_eeprom == 0xFF){
..
.
.
}

how to do this in mikro c.
Hope there could be a function to read EEPROM. Search at MicroC.
 

Can I ask one more thing about mentioned if

Is this correct:
if (true) {then do this} else {to this};
 

For true, I assume it is either a boolean (basic type), or it's just the result of the condition check. If so, yes the code is correct, but without the semicolon at the end.
Code:
 if (true){
    // do your things if true
} else {
    // do your things if not true
}
 
Polona I suggest You to use Help file in MikroC you can find all there.

Code:
if (polona == DontKnow)
      {
      True = Use_MikroC_Help;
      }
  else
      {
      False = Visit_EDABoard_com;
      }


This can be done easily. Just read all memory addresses and compare it with 0xFF value (0xFF means empty), first address which you find is your answer. PIC18F45K22 have 256 bytes eeprom memory size this can be done very fast. I will post some example later.

:wink:
 
Last edited:
Here is one example.

PIC18F45K22 (256 bytes of internal eeprom memory)
MikroC for PIC

Check connection between LCD and uC pins and adjust code.


EEPROM Screenshot:


LCD Screenshot:


Code:
// LCD module connections
sbit LCD_RS at LATB4_bit;
sbit LCD_EN at LATB5_bit;
sbit LCD_D4 at LATB0_bit;
sbit LCD_D5 at LATB1_bit;
sbit LCD_D6 at LATB2_bit;
sbit LCD_D7 at LATB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections

char bb, value, first_free_address;
char free_left, free_total, ser;
long int i, first;

void main() {

  Lcd_Init();                                    // Initialize LCD
  Lcd_Cmd(_LCD_CLEAR);                           // Clear LCD
  Lcd_Cmd(_LCD_CURSOR_OFF);                      // Turn cursor off

/*
  Find first free address in internal eeprom of PIC18F45K22 
  uC have 256 bytes (addresses) and last address is 0xFF
*/


//Check first eeprom free address and print address
//In first row under hex is hex address in eeprom
//In second row under ser is serial number in eeprom between 0 and 256

  for (i=0; i<=256; i++)
      {
      value = EEPROM_Read(i);   //Read eeprom address (i) and put data into "value"
      bb = value;
      if (bb == 0xFF)
            {
            first = i;
            ByteToHex(first, first_free_address);
            Lcd_Out(1,1,"hex: ");
            Lcd_Out_Cp(first_free_address);

            ByteToStr(first, ser);
            Lcd_Out(2,1,"ser:");
            Lcd_Out_Cp(ser);
            
            break;
            }
      }

  do {
  
      }
   while (1);
}




:wink:
 
Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…