Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

fram FM24C16B adressing issue

Status
Not open for further replies.

raman00084

Full Member level 6
Joined
Nov 29, 2010
Messages
362
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,298
Activity points
4,006
fram FM24C16B interfacing with pic

i am using i2c fram FM24C16B with pic 18f4620 micro but i could not able to get accurate results. can any one send me a sample code for fram read and write i am using ccs complier



regards
kalyan
 

Re: fram FM24C16B interfacing with pic

FM24C16B is compatible with 2416 EEPROM, did you check the CCS 2416 driver?
 

Re: fram FM24C16B interfacing with pic

i could not able to write in addr 2049, only 0-2048 addr is aces sable in ccs driver
 

Re: fram FM24C16B interfacing with pic

Code:
u8 I2C_24C_EEPROM_Write_Page (u8 I2c_Address, u16 Mem_Address, char * pBuffer)
{
  u8 err=0;
  err=I2c_Start(EEPROM_I2Cx);
  if (err) return err;
  err=I2c_WriteAddress(EEPROM_I2Cx, I2c_Address);
  if (err) return err;
  I2c_WriteByte(EEPROM_I2Cx, (0xFF00&Mem_Address)>>8);
  I2c_WriteByte(EEPROM_I2Cx, 0xFF&Mem_Address);
  while (* pBuffer) I2c_WriteByte(EEPROM_I2Cx, * pBuffer++);
  I2c_Stop(EEPROM_I2Cx);
	delay_ms(10);
  return I2C_OK;
}

u8 I2C_24C_EEPROM_Write_Page_ByLen (u8 I2c_Address, u16 Mem_Address, char * pBuffer, u16 Len)
{
  u8 err;
	u8 PagesWritten = 0;
	u8 BytesToWrite;
	while (Len)
	{	
		if (Len<MaxPageSizeBytes) 
		{
			BytesToWrite = Len;
			Len = 0;
		}
		else 
		{
			BytesToWrite = MaxPageSizeBytes;
			PagesWritten++;
			Len-= MaxPageSizeBytes;
		}
		
		err=I2c_Start(EEPROM_I2Cx);
		if (err) return err;
		err=I2c_WriteAddress(EEPROM_I2Cx, I2c_Address);
		if (err) return err;
		I2c_WriteByte(EEPROM_I2Cx, (0xFF00&Mem_Address)>>8);
		I2c_WriteByte(EEPROM_I2Cx, 0x00FF&Mem_Address);
		while (BytesToWrite--) I2c_WriteByte(EEPROM_I2Cx, * pBuffer++);
		I2c_Stop(EEPROM_I2Cx);
		delay_ms(I2c_EEPROM_WriteTime_ms);
		
		Mem_Address+= MaxPageSizeBytes;
	}
  return I2C_OK;
}

u8 I2C_24C_EEPROM_Read_Byte (u8 I2c_Address, u16 Mem_Address, char * pBuffer)
{
  u8 err;
  err=I2c_Start(EEPROM_I2Cx);
  if (err) return err;
  err=I2c_WriteAddress(EEPROM_I2Cx, I2c_Address);
  if (err) return err;
  I2c_WriteByte(EEPROM_I2Cx, (0xFF00&Mem_Address)>>8);
  I2c_WriteByte(EEPROM_I2Cx, 0xFF&Mem_Address);
  I2c_ReStart(EEPROM_I2Cx);
  I2c_NACK(EEPROM_I2Cx);
  I2c_WriteAddress(EEPROM_I2Cx, I2c_Address|1);
  * pBuffer = I2c_ReadByte(EEPROM_I2Cx);
  I2c_Stop(EEPROM_I2Cx);
  I2c_ACK(EEPROM_I2Cx);
  return I2C_OK;
}

u8 I2C_24C_EEPROM_Read_Page (u8 I2c_Address, u16 Mem_Address, char * pBuffer, u8 pBufferSize)
{
  u8 err=0;
	I2c_ACK(EEPROM_I2Cx);
  err=I2c_Start(EEPROM_I2Cx);
  if (err) return err;
  err=I2c_WriteAddress(EEPROM_I2Cx, I2c_Address);
  if (err) return err;
  I2c_WriteByte(EEPROM_I2Cx, (0xFF00&Mem_Address)>>8);
  I2c_WriteByte(EEPROM_I2Cx, 0xFF&Mem_Address);
  I2c_ReStart(EEPROM_I2Cx);
  I2c_WriteAddress(EEPROM_I2Cx, I2c_Address|1);
  I2c_ACK(EEPROM_I2Cx);
  while (pBufferSize) 
  {
    * pBuffer++ = I2c_ReadByte(EEPROM_I2Cx);
    pBufferSize--;
    if (!pBufferSize) I2c_NACK(EEPROM_I2Cx);
  }
  I2c_Stop(EEPROM_I2Cx);
  
  return I2C_OK;  
}
 
Re: fram FM24C16B interfacing with pic

i could not able to write in addr 2049, only 0-2048 addr is aces sable in ccs driver
Good joke. How can a 2kx8 memory have addresses 2048 or 2049? Neither Standard EE24C16 nor FM24C16B has.
 

in the below code if i change the adress to 2049 i cannot store the value, below 0- 2048 it works goods. kindly help
how to choose the bank it has 2kx8 banks.




Code:
#include <main.h>
#include <math.h>
#include <stdlib.h>
#include <ctype.h>
#include <float.h>

#include <Flex_LCD4x20.c>  

#define EEPROM_SDA  PIN_c4
#define EEPROM_SCL  PIN_c3


#use i2c(Master,Fast, sda=EEPROM_SDA, scl=EEPROM_SCL)

int32 count_1 = 0 ;
int32 count_2 = 0 ;
int32 count = 0 ;

float fcount = 0; 

#define EEPROM_ADDRESS unsigned int16
//#define EEPROM_SIZE    1024

void init_ext_eeprom()
{
   output_float(EEPROM_SCL);
   output_float(EEPROM_SDA);
}

int1 ext_eeprom_ready()
{
   int1 ack;
   i2c_start();            // If the write command is acknowledged,
   ack = i2c_write(0xa0);  // then the device is ready.
   i2c_stop();
   return !ack;
}

void write_ext_eeprom(EEPROM_ADDRESS address, BYTE data) 
{
   while(!ext_eeprom_ready());
   i2c_start();
   i2c_write((0xa0|(BYTE)(address>>7))&0xfe);
   i2c_write(address);
   i2c_write(data);
   i2c_stop();
}


BYTE read_ext_eeprom(EEPROM_ADDRESS address) 
{
   BYTE data;

   while(!ext_eeprom_ready());
   i2c_start();
   i2c_write((0xa0|(BYTE)(address>>7))&0xfe);
   i2c_write(address);
   i2c_start();
   i2c_write((0xa0|(BYTE)(address>>7))|1);
   data=i2c_read(0);
   i2c_stop();
   return(data);
}



void write_eeprom_32( unsigned int16 addr, int32 val)
{
  unsigned int8 *val_ptr = (unsigned int8 *)&val;
  for(int i = 0; i < 4; i++)
  {
    write_ext_eeprom(addr + i, *(val_ptr + i));
  }
}

int32 read_eeprom_32(unsigned int16 addr)
{
  int32 val;
  unsigned int8 *val_ptr = (unsigned int8 *)&val;
  for(int i = 0; i < 4; i++) 
  {
    *(val_ptr + i) = read_ext_eeprom(addr + i);
  }
  return val;
}

void write_eeprom_float32( unsigned int16 addr , float val)
{
   unsigned int8 *val_ptr = (unsigned int8 *)&val;
   for(int i = 0; i < sizeof(float); i++)
   {
      write_ext_eeprom(addr + i, *(val_ptr + i));
   }
}

float read_eeprom_float32(unsigned int16 addr)
{
   float val;
   unsigned int8 *val_ptr = (unsigned int8 *)&val;
   for(int i = 0; i < sizeof(float); i++)
   {
      *(val_ptr + i) = read_ext_eeprom(addr + i);
   }
   return val;
}

void main()
{
 init_ext_eeprom();
 lcd_init();
 lcd_gotoxy(1, 1); 
 printf(lcd_putc, "testing lcd");
 printf("testing FRAM");

 lcd_clear_all;
 output_high(pin_b6);
 delay_ms(1000);
 output_low(pin_b6);
 count_1 = read_eeprom_32(0); 
 count_2 = read_eeprom_32(4); 
 count = read_eeprom_32(2040); 
 fcount =  read_eeprom_float32(2000) ; 
 while(TRUE)
 {
count++;
count_1++;
count_2++;
fcount += 0.01;

//write_ext_eeprom(100,count);

 write_eeprom_32(2040,count);
 write_eeprom_float32(2000,fcount);
  write_eeprom_32(4,count_2);
 
 write_eeprom_32(0,count_1);

     lcd_gotoxy(1, 1); 
     printf(lcd_putc, "float: %f", read_eeprom_float32(2000));
     
     lcd_gotoxy(1, 2); 
     printf(lcd_putc, "%ld", read_eeprom_32(0));

      lcd_gotoxy(1, 3);
      printf(lcd_putc, "%04LD", read_eeprom_32(4)); 

      lcd_gotoxy(1, 4);
      printf(lcd_putc, "%04LD",read_eeprom_32(2040));
      delay_ms(200);

 }

}
 

Hi,

in your thread:
the answer is given already.

You only have 2048 memory cells. Why do you think you can use 2049 or even more?

Klaus
 

i want to access all the 16k memory how to do this kindly help thank you i tried many methods all failed
 

Can it be that you confused 16 kbit (= 2 kByte) with 16 kByte?

A flaw of your code is that the upper address bits overflow into the I2C address if you erroneously enter an address > 2047
Code:
i2c_write((0xa0|(BYTE)(address>>7))&0xfe);

I suggest to rewrite it as follows to get at least wrap around to lower addresses. There are only 3 memory address bits in the first command byte:
Code:
i2c_write(0xa0 | (BYTE)(address>>7) & 0xe);
 

problem solved
it is the confusion withe 16Kbit as 16k byte.,

thanks for all your support
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top