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

I2C 24LC16B Read/Write Question

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
edbfmi1



Joined: 18 Jul 2006
Posts: 98

View user's profile Send private message

I2C 24LC16B Read/Write Question
PostPosted: Thu Oct 05, 2006 10:29 am     Reply with quote

I am using the PCM compiler with a PIC16C73A micro.
I had to upgrade to the PIC16C73A from my original PIC because I ran out of memory. I know the PIC16C73A has a USART but I am mot able to take advantage of this due to backward compatibility. I am using the following pins for communication with the 24LC16B Eeprom:
SDA = Pin_B7
SCL = Pin_B6

I have written a simple subroutine to initialize the Eeprom with the following data:

Code:

/*
Ram Location      Value
11,12         11
13,14         21
15,16         31
17,18         41

21,22         12
23,24         22
25,26         32
27,28         42

31,32         13
33,34         23
35,36         33
37,38         43

.         .
.         .
.         .
.         .

91,92         19
93,94         29
95,96         39
97,98         49


*/


Below is the subroutines I use to accomplish this, (I have only included the subroutines in question and the pre-processor directives)

Code:

#include <16C73A.h>
#device *=8

#fuses HS,NOWDT,NOPROTECT
#use delay(clock=40000000)
#use I2C(master, SDA=PIN_B7,  SCL=PIN_B6, fast=1000000)
#define EEPROM_SDA  PIN_B7
#define EEPROM_SCL  PIN_B6

void WriteEeprom(long int data, int loc)
{
   output_float(EEPROM_SCL);
   output_float(EEPROM_SDA);
   {
      I2C_START();
      I2C_WRITE(160);
         I2C_WRITE(loc);
            I2C_WRITE(data);    // Low Byte of data
      I2C_WRITE(data>>8);   // High byte of data
      I2C_STOP();
      delay_ms(20);
   }
}
void ReadEeprom(long int data, int loc)
{
   I2C_START();
   I2C_WRITE(160);
   I2C_WRITE(loc);
   I2C_START();
   I2C_WRITE(161);
   data1 = I2C_READ();
   data2 = I2C_READ(0);
   I2C_STOP();
   data2 = data2<<8;
   preset1 = data2+data1;
}
void PresetEeprom()
{
   ProgNumber = 1;
   while (ProgNumber < 10)
   {
      delay_cycles(1);
      location = ProgNumber*10;

      location = location+1;
      preset1 = ProgNumber + 10;
      WriteEeprom((preset1),(location));   // WriteEepprom (data, location)
      location = location+2;

      preset1 = ProgNumber + 20;
      delay_cycles(1);
      WriteEeprom((preset1),(location));
      location = location+2;

      preset1 = ProgNumber + 30;
      delay_cycles(1);
      WriteEeprom((preset1),(location));
      location = location+2;

      preset1 = ProgNumber + 40;
      delay_cycles(1);
      WriteEeprom((preset1),(location));
      ProgNumber++;
   }
}


Everything works except it stores decimal 12327 instead of 39 at locations 95,96.

This is binary 0011000000100111 instead of 0000000000100111.

I can not figure out why it has 00110000 in the high byte instead of 00000000.

BTW, I do have external 10K pull ups on the SDA and SCL lines and I have tried a new Eeprom but it gives the same results.

Any helpful insight will be appreciated.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Oct 05, 2006 11:56 am     Reply with quote

Quote:
#include <16C73A.h>
#device *=8 // Don't need this line.
#fuses HS,NOWDT,NOPROTECT
#use delay(clock=40000000) // 40 MHz is not correct
#use I2C(master, SDA=PIN_B7, SCL=PIN_B6, fast=1000000)
#define EEPROM_SDA PIN_B7
#define EEPROM_SCL PIN_B6

Your program has a large number of errors.

1. 8-bit pointers is the default for the 16C73A. You don't need that line.

2. You have the clock set for 40 MHz. Do you really have a 4 MHz
crystal ? If so, delete one of the 0's.

3. The 24LC16B SCL signal can run at a maximum speed of 400 KHz.
You have the speed set for 1 MHz. This will not work. The max
speed information is at the top of the first page of the 24LC16B data
sheet. You need to download the data sheet and read it.
http://ww1.microchip.com/downloads/en/DeviceDoc/21703F.pdf


Quote:
void ReadEeprom(long int data, int loc)
{
I2C_START();
I2C_WRITE(160);
I2C_WRITE(loc);
I2C_START();
I2C_WRITE(161);
data1 = I2C_READ();
data2 = I2C_READ(0);

CCS already has a driver for the 24LC16B. It's in this folder:
c:\program files\picc\drivers\2416.c

If you look at that driver and the eeprom data sheet, you will see
that the address is 16-bits ('long int') and the data is 8-bits.
In your driver, you have it the opposite way. That won't work.

CCS has a driver. Use it. That way, you prevent this kind of bug
from happening.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion 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