Author Topic: 24LC64 - CCS C compiler header (trying to understand it)  (Read 5775 times)

0 Members and 1 Guest are viewing this topic.

Offline h1386343Topic starter

  • Regular Contributor
  • *
  • Posts: 55
24LC64 - CCS C compiler header (trying to understand it)
« on: August 28, 2014, 12:41:24 am »
Hello dears :)

This is the entire code from the CCS C compiler "2464.C" header, and I am struggling with something which may be obvious, but it ain't to me:

Code: [Select]
///////////////////////////////////////////////////////////////////////////
////   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_C5
#define EEPROM_SCL  PIN_C4

#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)
{
   short int status;
   i2c_start();
   i2c_write(0xa0);
   i2c_write((address>>8)&0x1f); <<---------------------- What is this for?
   i2c_write(address);
   i2c_write(data);
   i2c_stop();
   i2c_start();
   status=i2c_write(0xa0);
   while(status==1)
   {
      i2c_start();
      status=i2c_write(0xa0);
   }
   i2c_stop();
}

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

Why does the code shift that byte to the right, by 8 places? That just means the byte ends up exactly the same as it was BEFORE it was shifted. I am gradually learning I2C, and I must confess that this has stumped me. Could someone explain for me, please? Thank you :)
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: 24LC64 - CCS C compiler header (trying to understand it)
« Reply #1 on: August 28, 2014, 12:48:27 am »
Read 24LC64's datasheet, and pay particular attention to the length of the address bytes.
================================
https://dannyelectronics.wordpress.com/
 

Offline miguelvp

  • Super Contributor
  • ***
  • Posts: 5550
  • Country: us
Re: 24LC64 - CCS C compiler header (trying to understand it)
« Reply #2 on: August 28, 2014, 12:48:52 am »
address is from 0 to 0x1fff

the shift is to get the higher 5 bits of the address out to the 8 bit I2C interface, the 2nd will truncate the upper byte and send only the lower 8 bits.
 

Offline h1386343Topic starter

  • Regular Contributor
  • *
  • Posts: 55
Re: 24LC64 - CCS C compiler header (trying to understand it)
« Reply #3 on: August 28, 2014, 12:56:32 am »
Read 24LC64's datasheet, and pay particular attention to the length of the address bytes.

i2c address is 7 bits of a byte, with the lsb being R/W indication, right?

Thanks everyone; I'm a step closer :)

Take care, much appreciated.
 

Offline miguelvp

  • Super Contributor
  • ***
  • Posts: 5550
  • Country: us
Re: 24LC64 - CCS C compiler header (trying to understand it)
« Reply #4 on: August 28, 2014, 01:05:43 am »
http://ww1.microchip.com/downloads/en/devicedoc/21189f.pdf

page 6 Figure 3-3

You write the first byte containing the 5 most significant bits, then the other 8 less significant bits on the 2nd call.



Edit: Full write (in your case the control byte is 0xa0 so it writes to bank 0), then MSB, then LSB then the data.


« Last Edit: August 28, 2014, 01:10:47 am by miguelvp »
 

Offline h1386343Topic starter

  • Regular Contributor
  • *
  • Posts: 55
Re: 24LC64 - CCS C compiler header (trying to understand it)
« Reply #5 on: August 28, 2014, 01:16:43 am »
Ah brill :)

I always imagine bits and bytes as "DIP switches" in my mind, which is how I rationalised learning PIC ASM, and each bit or byte flips the "DIP switches" to the appropriate configuration - (does that make sense to you?).
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: 24LC64 - CCS C compiler header (trying to understand it)
« Reply #6 on: August 28, 2014, 01:23:39 am »
Quote
Ah brill

It shows why it is critical to read the datasheet of the device you are attempting to code for.
================================
https://dannyelectronics.wordpress.com/
 

Offline h1386343Topic starter

  • Regular Contributor
  • *
  • Posts: 55
Re: 24LC64 - CCS C compiler header (trying to understand it)
« Reply #7 on: August 28, 2014, 01:33:04 am »
Quote
Ah brill

It shows why it is critical to read the datasheet of the device you are attempting to code for.

I'm actually not coding for that EEPROM, I'm just learning i2c generally, but this has now reminded me of how i2c packets are different per manufacturer/device, and not a standard. :)
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4199
  • Country: us
Re: 24LC64 - CCS C compiler header (trying to understand it)
« Reply #8 on: August 29, 2014, 07:57:34 am »
Code: [Select]
   i2c_write((address>>8)&0x1f); <<---------------------- What is this for?
   i2c_write(address);
Quote
this has now reminded me of how i2c packets are different per manufacturer/device

But in this case, the code that confused you doesn't have that much to do with the packet format. It's more of a standard technique for breaking up a larger piece of data (in this case, 13 bits of address) into individual bytes (i2c_write() presumably only writes a single byte at a time.)
If you had a really big I2C EEPROM, you might have seen:
Code: [Select]
   i2c_write((address>>24));  // Upper 8 of 32 bits.
   i2c_write((address>>16)&0xFF);  // second 8 bits of 32bit address.
   i2c_write((address>>8)&0xFF);  // third 8 bits of 32bit address.
   i2c_write(address&0xFF);  // lowest 8 bits of 32bit address.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf