CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug Reports on this forum. Send them to support@ccsinfo.com

4 x Serial EEPROM 24AA1025 512 Kbytes direct access driver.

 
Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message
Redpic



Joined: 18 Dec 2005
Posts: 5
Location: Spain

View user's profile Send private message Visit poster's website

4 x Serial EEPROM 24AA1025 512 Kbytes direct access driver.
PostPosted: Sun Oct 15, 2006 10:32 am     Reply with quote

This work is based in two previous works:

The EX_EXTEE.C from CCS C examples directory and
a post in this same Code Library section Driver Microchip 24AA512 512k I2C EEPROM by UFAnders

This driver is capable for addresing from 0x0000 to 0x7FFFF, 0x8000 positions = 512 Kbytes.

Driver:

Code:

///////////////////////////////////////////////////////////////////////////////
//
// 4 x EEPROM 24AA1025 Driver with absolute memory addressing
//
///////////////////////////////////////////////////////////////////////////////

#define EEDEBUG

// device hardware address

int8 const deviceAddress1 = 0b00000011; // #3
int8 const deviceAddress2 = 0b00000010; // #2
int8 const deviceAddress3 = 0b00000001; // #1
int8 const deviceAddress4 = 0b00000000; // #0

// baseAddress = 8 bits which mean ...
//
// Fixed (4 bits)            -> 1010
// EE internal Block (1 bit) -> 0 for 0000/FFFF, 1 for 10000/1FFFF
// Dev Hard Address (2 bits) -> 00 or 01 or 10 or 11
// R/W (1 bit)               -> 0 -> Write / 1 -> Read

int8 const baseAddress24AA1025 = 0b10100000;

// Aux Functions //////////////////////////////////////////////////////////////

int1  menRelative2BankSelect(int32 memRelativeAddress){

   int1 returnBit=0;

   if(memRelativeAddress>(int32) 0xffff) returnBit = 1;

   return returnBit;
}

int32 memAbsoluteAddress2menRelativeAddress(int8 deviceOrder, int32 memAddress){

   int32 returnInt32 = 0;

   switch(deviceOrder){
      case 1: returnInt32 = memAddress;
              break;
      case 2: returnInt32 = memAddress - 0x20000;
              break;
      case 3: returnInt32 = memAddress - 0x40000;
              break;
      case 4: returnInt32 = memAddress - 0x60000;
              break;
   }
   return returnInt32;
}

int8 memAbsoluteAddress2deviceOrder(int32 memAddress){

   int8  returnByte=0;

   if((memAddress>0x05ffff) && (memAddress < 0x080000)) returnByte=4;
   if((memAddress>0x03ffff) && (memAddress < 0x060000)) returnByte=3;
   if((memAddress>0x01ffff) && (memAddress < 0x040000)) returnByte=2;
   if((memAddress< 0x020000)) returnByte=1;

   return returnByte;
}

int8 memAbsoluteAddress2deviceAddress(int32 memAddress){

   int8  deviceOrder=0, returnByte=0;

   deviceOrder = memAbsoluteAddress2deviceOrder(memAddress);

   switch(deviceOrder){
      case 0: returnByte = 0xff;
              break;
      case 1: returnByte = deviceAddress1;
              break;
      case 2: returnByte = deviceAddress2;
              break;
      case 3: returnByte = deviceAddress3;
              break;
      case 4: returnByte = deviceAddress4;
              break;
   }
   return returnByte;
}

// Main Functions /////////////////////////////////////////////////////////////

void writeByte24AA1025(int32 memAddress, int8 data){

   int8  baseAddress;
   int8  deviceAddress;
   int8  deviceOrder=0;
   int32 memRelativeAddress;
   int8  bank=0;

   deviceAddress      = memAbsoluteAddress2deviceAddress(memAddress);
   deviceOrder        = memAbsoluteAddress2deviceOrder(memAddress);
   memRelativeAddress = memAbsoluteAddress2menRelativeAddress(deviceOrder, memAddress);
   bank               = menRelative2BankSelect(memRelativeAddress);
   baseAddress        = baseAddress24AA1025 + (bank<<3)  + (deviceAddress<<1);
   
#ifdef EEDEBUG
   printf("\r\n\nAbsolute Address: %LX Relative Address: %Lx Device: %u Bank: %u\r\n",memAddress,memRelativeAddress,deviceOrder,bank);
#endif

   i2c_start();
   i2c_write(baseAddress);
   i2c_write(memRelativeAddress>>8);
   i2c_write(memRelativeAddress);
   i2c_write(data);
   i2c_stop();
   delay_ms(5);
}

int8 readByte24AA1025(int32 memAddress){

   int8  returnByte=0;
   int8  baseAddress;
   int8  deviceAddress;
   int8  deviceOrder=0;
   int32 memRelativeAddress;
   int8  bank=0;

   deviceAddress      = memAbsoluteAddress2deviceAddress(memAddress);
   deviceOrder        = memAbsoluteAddress2deviceOrder(memAddress);
   memRelativeAddress = memAbsoluteAddress2menRelativeAddress(deviceOrder, memAddress);
   bank               = menRelative2BankSelect(memRelativeAddress);
   baseAddress        = baseAddress24AA1025 + (bank<<3)  + (deviceAddress<<1);

#ifdef EEDEBUG
   printf("\r\n\nAbsolute Address: %LX Relative Address: %Lx Device: %u Bank: %u\r\n",memAddress,memRelativeAddress,deviceOrder,bank);
#endif

   i2c_start();
   i2c_write(baseAddress);
   i2c_write(memRelativeAddress>>8);
   i2c_write(memRelativeAddress);
   i2c_start();
   i2c_write(baseAddress|1);
   returnByte = i2c_read(0);
   i2c_stop();
   return returnByte;
}
///////////////////////////////////////////////////////////////////////////////


Adapted from example code EX_EXTEE.C, my own EEPROM_24AA1025x4.C:

Code:

///////////////////////////////////////////////////////////////////////////////
//
// EEPROM_24AA1025x4.C
//
// based on EX_EXTEE.C example by CCS
// and "Microchip 24AA512 512k I2C EEPROM driver" by UFAnders
// in http://www.ccsinfo.com/forum/
//
// by RedPic from http://picmania.garcia-cuervo.com
//
// October 2006
//
///////////////////////////////////////////////////////////////////////////////

#include <18F4550.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#use i2c(master, sda=PIN_B0, scl=PIN_B1)

#include <input.c>
#include <24AA1025x4.c>

const char Version[]="1.0.F\0";

///////////////////////////////////////////////////////////////////////////////
//
// Main
//
///////////////////////////////////////////////////////////////////////////////

void main() {

   BYTE value, cmd;
   int32 address;
   int8  device;

   delay_ms(300);
   printf("\r\n\n");
   printf("[RRBOARD2] EEPROM Commander %s\r\n",version);
   printf("based on 24AA1025 Microchip Hardware\r\n");
   printf("\xa9 10.2006 by RedPic\r\n\n");

   do {
      do {
         printf("\r\nDo you Read or Write? : ");
         cmd=getc();
         cmd=toupper(cmd);
         putc(cmd);
      } while ( (cmd!='R') && (cmd!='W') );

      printf("\n\rGive me EEPROM internal absolute Address (24 bits) in hex : ");

      address = gethex();
      address = (address<<8)+gethex();
      address = (address<<8)+gethex();

      device  = memAbsoluteAddress2deviceAddress(address);

      if(cmd=='R')
         printf("\r\nReturn Value (8 bits) in hex is : %X\r\n",readByte24AA1025(address));

      if(cmd=='W') {
         printf("\r\nEnter new value (8 bits) in hex : ");
         value = gethex();
         printf("\n\r");
         writeByte24AA1025(address, value );
      }
   } while (TRUE);
}


At last a executing view:



More details (in spanish) in AUX Quad-EEPROM I2C 512 Kbytes de EEPROM con cuatro 24AA1025
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group