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

FLEX I2C Memory Driver

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



Joined: 29 Aug 2010
Posts: 11

View user's profile Send private message Send e-mail Yahoo Messenger

FLEX I2C Memory Driver
PostPosted: Fri Sep 06, 2013 9:41 am     Reply with quote

This is CCS C 2464 Driver that i just modify. Modifications allow you to use many memory in the same I2C bus. Each memory should have it hardware address. For example if we want to use 8 Memory, We have to use this combination of bits.
E2 E1 E0
0 0 0 Memory0 (dev adr=00)
.
.
.
1 1 1 Memory7 (dev adr=7)

Code:

///////////////////////////////////////////////////////////////////////////
////   Library for a 24LC64 serial EEPROM                              ////
////                                                                   ////
////   init_ext_eeprom();    Call before the other functions are used  ////
////                                                                   ////
////   write_ext_eeprom(a, d);  Write the byte d to the address a      ////
////                                                                   ////
////   d = read_ext_eeprom(a);   Read the byte d from the address a    ////
////                                                                   ////
////   The main program may define eeprom_sda                          ////
////   and eeprom_scl to override the defaults below.                  ////
////                                                                   ////
///////////////////////////////////////////////////////////////////////////
////        (C) Copyright 1996,2003 Custom Computer Services           ////
//// This source code may only be used by licensed users of the CCS C  ////
//// compiler.  This source code may only be distributed to other      ////
//// licensed users of the CCS C compiler.  No other use, reproduction ////
//// or distribution is permitted without written permission.          ////
//// Derivative programs created using this software in object code    ////
//// form are not restricted in any way.                               ////
///////////////////////////////////////////////////////////////////////////

#ifndef EEPROM_SDA

#define EEPROM_SDA  PIN_C1
#define EEPROM_SCL  PIN_C0

#endif

#use i2c(master, sda=EEPROM_SDA, scl=EEPROM_SCL)

#define EEPROM_ADDRESS long int
#define EEPROM_SIZE   8192

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

void write_ext_eeprom(long int address, BYTE data,int dev_adr)
{
   short int status;
   i2c_start();           
   i2c_write(0xa0|dev_adr<<1);
   i2c_write(((address>>8)&0x1f)|dev_adr<<5);
   i2c_write(address);
   i2c_write(data);
   i2c_stop();
   i2c_start();
   status=i2c_write(0xa0|dev_adr<<1);
   while(status==1)
   {
      i2c_start();
      status=i2c_write(0xa0|dev_adr<<1);
   }
   i2c_stop();
}

BYTE read_ext_eeprom(long int address,int dev_adr) {
   BYTE data;
   i2c_start();     
   i2c_write(0xa0|dev_adr<<1);
   i2c_write(((address>>8)&0x1f)|dev_adr<<5);
   i2c_write(address);
   i2c_start();
   i2c_write(0xa1|dev_adr<<1);
   data=i2c_read(0);
   i2c_stop();
   return(data);
}


Now how to use it?
To write/read at memory with dev_adr=00, this is the code
Code:

dev_adr=0;
 write_ext_eeprom(adress, data,dev_adr);// adress: where you will write Byte data,dev_adr is the Hardware adress


Enjoy.
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