View previous topic :: View next topic |
Author |
Message |
onurdo
Joined: 16 Oct 2006 Posts: 8
|
i2c communication with external eeprom |
Posted: Mon Oct 16, 2006 4:54 pm |
|
|
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
|
|
Posted: Tue Oct 17, 2006 3:38 am |
|
|
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
|
|
Posted: Tue Oct 17, 2006 4:22 am |
|
|
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
|
|
Posted: Tue Oct 17, 2006 7:05 am |
|
|
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
|
|
Posted: Tue Oct 17, 2006 8:18 am |
|
|
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
|
|
Posted: Tue Oct 17, 2006 8:22 am |
|
|
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
|
|
Posted: Tue Oct 17, 2006 12:02 pm |
|
|
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
|
|
|
onurdo
Joined: 16 Oct 2006 Posts: 8
|
|
Posted: Tue Oct 17, 2006 1:55 pm |
|
|
Thank you PCM programmer,
I will look your suggestions. |
|
|
onurdo
Joined: 16 Oct 2006 Posts: 8
|
|
Posted: Tue Oct 17, 2006 2:53 pm |
|
|
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
|
|
Posted: Tue Oct 17, 2006 3:16 pm |
|
|
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
|
|
Posted: Tue Oct 17, 2006 5:33 pm |
|
|
Okey, and thank you PCM Programmer, I will wait your massage
best regards, |
|
|
newguy
Joined: 24 Jun 2004 Posts: 1909
|
|
Posted: Tue Oct 17, 2006 7:03 pm |
|
|
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. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Oct 20, 2006 3:57 pm |
|
|
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
|
|
Posted: Fri Oct 27, 2006 5:28 am |
|
|
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. |
|
|
|