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 communication with external eeprom

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



Joined: 16 Oct 2006
Posts: 8

View user's profile Send private message

i2c communication with external eeprom
PostPosted: Mon Oct 16, 2006 4:54 pm     Reply with quote

Hi,
At first, I am sorry about my language. I wrote the below codes, but did not worked. Yes I know, now you will tell me the read other releated forum massagess. I red and I worked very very much combinations of the i2c codes but did not worked. I red the other sources (like Embaded C Programming and the Microchip PIC / Barnett, Cox & O'Cull Book).
I am using CCS C compiler ver. 3.189 I forgot, Iwant to communicate with microchip 24c65
Could some one help me?

Code:

#define speed 4000000
#include <16F876.h>
#Fuses  xt,nowdt,protect,noput,nobrownout,nolvp,nocpd
#use  delay(clock=speed)
#byte status=3         
#byte porta=5 
#byte portb=6 
#byte portc=7 
#define eeprom_sda  pin_c4
#define eeprom_scl  pin_c3
#use rs232(baud=9600, xmit=pin_b7)    // This is for my LCD
#use i2c(master,sda=eeprom_sda,scl=eeprom_scl,restart_wdt,force_hw)

int8 data,receive;

void init_ext_eeprom()
   {
   output_float(pin_c4);
   output_float(pin_c3);
   }

boolean is_ext_eeprom_ready()
   {
   int1 ack;
   i2c_start();
   ack=i2c_write(160);
   i2c_stop();
   return !ack;
   }

void write_ext_eeprom(int16 address, int8 data)
   {
   while(!is_ext_eeprom_ready());
   i2c_start();
   i2c_write(160);
   i2c_write(address>>8);
   i2c_write(address);
   i2c_write(data);
   i2c_stop();
   }

void read_ext_eeprom(int16 adres)
   {
   while(!is_ext_eeprom_ready());
   i2c_start();
   i2c_write(160);
   i2c_write(address>>8);
   i2c_write(address);
   i2c_start();
   i2c_write(161);
   receive=i2c_read(0);
   i2c_stop();
   }

void main()                                     
   {
   setup_counters(rtcc_internal,wdt_1152ms);
   init_ext_eeprom();
   clr_screen();
   while(true)
      {
      if(!bit_test(porta,0))
         {
         delay_ms(500);
         restart_wdt();
         write_ext_eeprom(16,113);
         delay_ms(50);
         line_1();
         printf("  MEMORY INSERTED   ");
         }
      if(!bit_test(porta,1))
         {
         delay_ms(500);
         restart_wdt();
         read_ext_eeprom(16);
         delay_ms(50);
         line_1();
         printf("  IN MEMORY  ");
         line_2();
         printf("      %u        ",receive);
         }
      }
   }
Neckruin



Joined: 17 Jan 2006
Posts: 66

View user's profile Send private message

PostPosted: Tue Oct 17, 2006 3:38 am     Reply with quote

Well, I'm not an I2C expert... in fact I found your post while looking for some help related to I2C, but I may give you some extra point of view:
In your code, you read the PORTA onve every 500ms, maybe this is to slow to detect the pulse.
I guess your are using an LCD display, aren't you? Well... I like guessing, there is no problem with that :P
The driver you are using asummes a fixed address for the memory, maybe is not the one you are using... no idea. Just "brain storming"

Good Luck,

Juanma
onurdo



Joined: 16 Oct 2006
Posts: 8

View user's profile Send private message

PostPosted: Tue Oct 17, 2006 4:22 am     Reply with quote

I wanted write a simple test program. When I push the (porta,0) button, it sends a test numbers (address 16 and data 113) to 24c65. And when I push the (porta,1) button, I am asking the 24c65 what number is in your address 16 memory.
bsodmike



Joined: 05 Aug 2006
Posts: 52

View user's profile Send private message Visit poster's website AIM Address MSN Messenger

PostPosted: Tue Oct 17, 2006 7:05 am     Reply with quote

Small tip, include the recieve pin in the use rs232 def, to enable UART,
Code:
//For 16F877A, use correct pins; check datasheet!
#use rs232(baud=4800, parity=N, bits=8, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Tue Oct 17, 2006 8:18 am     Reply with quote

Next time please tell us what is working and what is not working, which output you expect and the output you see.

You copied the whole of the 2465.c driver supplied by CCS. Don't do this, just place a #include line in your code. The code has been around for several years so I don't expect the error to be in the I2C command sequence.

In copying the code you changed read_ext_eeprom to return no data anymore. Why? How do you expect to get access to the read data now?

You never assign a value to the 'receive' variable, so whatever value is printed will be random and not related to the eeprom.
Neckruin



Joined: 17 Jan 2006
Posts: 66

View user's profile Send private message

PostPosted: Tue Oct 17, 2006 8:22 am     Reply with quote

ckielstra wrote:
You never assign a value to the 'receive' variable, so whatever value is printed will be random and not related to the eeprom.


If you look at the code you will see that "receive" is (or is intended to be) a global variable and is written within the read_ext_eeprom() routine.

Regards.
onurdo



Joined: 16 Oct 2006
Posts: 8

View user's profile Send private message

PostPosted: Tue Oct 17, 2006 12:02 pm     Reply with quote

I am sorry ckielstra,
yes, I copied 2465.c from compiler driver file.
but I changed the receive=read_ext_eeprom() I am not stupid.
I hoped you will though of this simple thing.

I asked This codes doesn't return 113 to lcd
best regards
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Oct 17, 2006 12:19 pm     Reply with quote

I have two suggestions:

1. Use the CCS driver, 2465.c.

2. See this thread, which explains an important feature of the 2465 eeprom:
http://www.ccsinfo.com/forum/viewtopic.php?t=18236
onurdo



Joined: 16 Oct 2006
Posts: 8

View user's profile Send private message

PostPosted: Tue Oct 17, 2006 1:55 pm     Reply with quote

Thank you PCM programmer,
I will look your suggestions.
onurdo



Joined: 16 Oct 2006
Posts: 8

View user's profile Send private message

PostPosted: Tue Oct 17, 2006 2:53 pm     Reply with quote

Dear PCM Programmer,

At the beginning I was tried #include 2465.c. But I didn't get right result
Now I am trying new one same #include 2465.c and I am getting from
LCD 201-199-199
3 results. Because I Wrote

a=read_ext_eeprom();
b=read_ext_eeprom();
c=read_ext_eeprom();
So, I get a 3 rasults like 201-199-199
however, I write an address 16 113 I only wanted to see 113
thanks for your intresting.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Oct 17, 2006 3:16 pm     Reply with quote

I need to study the security options on this eeprom some more,
and then I will post a simple test program for it.
onurdo



Joined: 16 Oct 2006
Posts: 8

View user's profile Send private message

PostPosted: Tue Oct 17, 2006 5:33 pm     Reply with quote

Okey, and thank you PCM Programmer, I will wait your massage
best regards,
newguy



Joined: 24 Jun 2004
Posts: 1903

View user's profile Send private message

PostPosted: Tue Oct 17, 2006 7:03 pm     Reply with quote

onurdo wrote:
Okey, and thank you PCM Programmer, I will wait your massage
best regards,


Just a guess, but you'll probably be waiting a loooooong time for that massage. Laughing
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Oct 20, 2006 3:57 pm     Reply with quote

Here is a program that will read the Security Block information
from the 24LC65 eeprom. I don't have an eeprom chip to test,
but I followed the i2c diagram in the data sheet in the section on
"Security Read", when I wrote the security_read_24LC65()
function.

If you run this program, it will tell you if the eeprom has any
blocks that are write-protected. You were having problems
with reading the correct data from your eeprom. If part of
the eeprom is write-protected, then this could explain your
problem.

Note: This program is only for the 24LC65 eeprom, which has
a special Security block feature. This program is not for normal
eeproms such as the 24LC64, etc.

Code:

#include <16F877.h>
#fuses XT, NOWDT, NOPROTECT, PUT, BROWNOUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

// Use the default pins of sda=PIN_C4 and scl=PIN_C3.
#include <2465.c>

//-------------------------------------
int16 security_read_24LC65(void)
{
int8 starting_block;
int8 number_of_blocks;
int16 retval;

while(!ext_eeprom_ready());

i2c_start();
i2c_write(0xA0);  // Slave address
i2c_write(0x80);  // Access security information
i2c_write(0x00);  // Don't care
i2c_write(0xC0);  // Configuration byte
starting_block = i2c_read() & 0x0F;   
number_of_blocks = i2c_read(0) & 0x0F;
i2c_stop();

// Combine the two status bytes into a 16-bit word.
retval = make16(starting_block, number_of_blocks);

return(retval);
}

//=============================
void main()
{
int8 result;

init_ext_eeprom();

result = security_read_24LC65();

printf("Display security block settings in 24LC65:\n\r");
printf("Starting block = %X \n\r", (int8)(result >> 8));
printf("Number of blocks = %X \n\r", (int8)result);

while(1);
}
onurdo



Joined: 16 Oct 2006
Posts: 8

View user's profile Send private message

PostPosted: Fri Oct 27, 2006 5:28 am     Reply with quote

Dear PCMprogrammer,
Thank you very much for your reply. I solved my problem. As you said, I think the reason that was the security or block problem. Because I had got 24c65 from my friend. And I bought a new ATMEL 24c64 it is worked.
Thank you for your interest.
With my best regards.
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