View previous topic :: View next topic |
Author |
Message |
KoRGeNeRaL
Joined: 04 Feb 2008 Posts: 7
|
24LC128 reading problem |
Posted: Mon Feb 04, 2008 7:46 am |
|
|
Hi all,
I'm trying to use a 24LC128 EEPROM with the functions from "24128.c" and "external_eeprom.c"
I was able to write an 16bit variable to the EEPROM, but when i read it back it reads as 0xFFFF. What can be the reason of this?
I looked at all similar topics in this forum but wasn't be able to find any difference between the code&connections between the ones here and mine.
Version of my compiler is 4.032
Here is the code:
Code: | #include <16f877.h>
#FUSES NOWDT
#FUSES XT
#FUSES NOBROWNOUT
#FUSES NOPUT
#FUSES NOPROTECT
#define Fosc 4000000
#use delay(clock = Fosc)
#include <lcd.c>
#include <24128.c>
#include <external_eeprom.c>
int16 address = 0x0010, result;
void main()
{
lcd_init();
init_ext_eeprom();
delay_ms(3);
write_int16_ext_eeprom(address, 0xAADD);
delay_ms(1000);
lcd_gotoxy(1,1);
result= read_int16_ext_eeprom(address);
delay_ms(3);
printf(lcd_putc, "data: %Lu ",result);
while (1);
} |
It should just write the value 0xAADD to adress 10 and then read it back for displaying on the LCD.
I looked at the I2C bus events in a Circuit Simulator and saw that the internal adress pointer of the EEPROM take wrong values when i write the adress for a reading operation.
Thanks in advance. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Feb 04, 2008 12:39 pm |
|
|
1. Do you have pull-up resistors on the SDA and SCL signals ?
They must be used. (You can use 4.7K ohm resistors).
2. Add the NOLVP fuse. Example:
If that doesn't help, then:
A. Post the code for write_int16_ext_eeprom() and read_int16_ext_eeprom().
B. Post your #use i2c() statement. |
|
|
KoRGeNeRaL
Joined: 04 Feb 2008 Posts: 7
|
|
Posted: Mon Feb 04, 2008 1:20 pm |
|
|
Thanks for the response.
1 - Yes i have them.
2 - I tried but it didn't change anything
I won't post the code for write_int16_ext_eeprom() and read_int16_ext_eeprom() because the problem still exists when i try to read only one byte with the functions below. (Without using the 16bit ones)
My #use statement and 8bit i2c functions i'm using are below.
Code: | #define EEPROM_SDA PIN_C4
#define EEPROM_SCL PIN_C3
#define hi(x) (*((int8 *)&x+1))
#use i2c(master, sda=EEPROM_SDA, scl=EEPROM_SCL)
#define EEPROM_ADDRESS long int
#define EEPROM_SIZE 16384
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);
i2c_write(address);
i2c_write(data);
i2c_stop();
i2c_start();
status=i2c_write(0xa0);
while(status==1)
{
i2c_start();
status=i2c_write(0xa0);
}
}
BYTE read_ext_eeprom(long int address) {
BYTE data;
i2c_start();
i2c_write(0xa0);
i2c_write(address>>8);
i2c_write(address);
i2c_start();
i2c_write(0xa1);
data=i2c_read(0);
i2c_stop();
return(data);
} |
Last edited by KoRGeNeRaL on Mon Feb 04, 2008 1:23 pm; edited 1 time in total |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Feb 04, 2008 1:22 pm |
|
|
Quote: | void main()
{
lcd_init();
init_ext_eeprom();
delay_ms(3);
write_int16_ext_eeprom(address, 0xAADD);
delay_ms(1000);
lcd_gotoxy(1,1);
result = read_int16_ext_eeprom(address);
delay_ms(3);
printf(lcd_putc, "data: %Lu ",result);
while (1);
} |
Post the code for write_int16_ext_eeprom() and read_int16_ext_eeprom(). |
|
|
KoRGeNeRaL
Joined: 04 Feb 2008 Posts: 7
|
|
Posted: Mon Feb 04, 2008 1:29 pm |
|
|
The problem isn't related to 16bit functions, since the below code has the same problem. (If the problem is related to the code of course)
Code: | #include <16f877.h>
#FUSES NOWDT
#FUSES XT
#FUSES NOBROWNOUT
#FUSES NOPUT
#FUSES NOPROTECT
#FUSES NOLVP
#define Fosc 4000000
#use delay(clock = Fosc)
#include <lcd.c>
#include <24128.c>
int16 address = 0x0010;
byte result;
void main()
{
lcd_init();
init_ext_eeprom();
delay_ms(3);
write_ext_eeprom(address, 0xAA);
delay_ms(1000);
lcd_gotoxy(1,1);
result= read_ext_eeprom(address);
delay_ms(3);
printf(lcd_putc, "data: %u ",result);
while (1);
} |
Regards
Last edited by KoRGeNeRaL on Mon Feb 04, 2008 1:31 pm; edited 1 time in total |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Feb 04, 2008 1:31 pm |
|
|
Do you have all the connections to the eeprom done correctly ?
1. Address pins A0, A1, and A2 should be connected to ground.
2. Write protect pin WP should be connected to ground. |
|
|
KoRGeNeRaL
Joined: 04 Feb 2008 Posts: 7
|
|
Posted: Mon Feb 04, 2008 1:33 pm |
|
|
Yes, all the connections are okay. It's weird. Am i missing to enable/disable something? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Feb 04, 2008 1:49 pm |
|
|
Are you sure that you have the connections between the PIC and the
eeprom done correctly ? Make sure you don't have the wires crossed.
Code: |
PIC EEPROM
SDA ---> SDA
SCL ---> SCL
GND ---> GND |
Make sure the eeprom has power on the Vdd pin. |
|
|
KoRGeNeRaL
Joined: 04 Feb 2008 Posts: 7
|
|
Posted: Mon Feb 04, 2008 2:01 pm |
|
|
Yes i'm sure about the connection btw the PIC end EEPROM. The weird thing is, the problem also occurs in the simulation. According to the simulation logs, there isn't any problem by writing data, but it takes wrong adress from the PIC when reading.
Look at the i2c events log of the reading process below which is taken from the the simulation software (Proteus from Labcenter)
Last edited by KoRGeNeRaL on Mon Feb 04, 2008 2:04 pm; edited 1 time in total |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Feb 04, 2008 2:03 pm |
|
|
Are you ever testing this in hardware ?
Or is all of this being done in Proteus ? |
|
|
KoRGeNeRaL
Joined: 04 Feb 2008 Posts: 7
|
|
Posted: Mon Feb 04, 2008 2:05 pm |
|
|
Of course i'm testing it on the actual circuit too but the results are same.
If you are sure about the code, then i'll trying using another 24LC128. Perhaps the IC is damaged somehow and the simulation cannot be trusted in this case. |
|
|
KoRGeNeRaL
Joined: 04 Feb 2008 Posts: 7
|
|
Posted: Mon Feb 04, 2008 2:27 pm |
|
|
The problem is solved. The EEPROM was damaged. I don't have the slightest idea how it is damaged but it is.
Thank you for your interest |
|
|
cem_turkmen
Joined: 01 Apr 2008 Posts: 1
|
plz hepling |
Posted: Tue Apr 01, 2008 12:39 am |
|
|
Hey friends,
I have a huge problem. I did everything to read and write ext eeprom but I am so tired of it. I only read FF. What am I doing wrong. ? PLZ help here is the code. I did it with every possible way and everything but couldn't make it work. Mabe some one knows...Here is the code.
Code: | #include <2402.C>
#use I2C(MASTER, sda=PIN_C4, scl=PIN_C3,FAST,FORCE_HW )
#include <flex_lcd.C>
int8 veri=0x00,adres=0x00;
int i,d[10];
void main()
{ #use I2C(MASTER, sda=PIN_C4, scl=PIN_C3,FAST,FORCE_HW )
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);;
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
lcd_init();
init_ext_eeprom();
delay_ms(300);
for(i=0;i<10;i++)
{
write_ext_eeprom(adres,veri);
adres++;
veri++;
printf(lcd_putc,"\f %x ...%x",adres,veri);
}
adres=0x00;
for(i=0;i<10;i++)
{
d[i]=read_ext_eeprom(adres);
adres++;
printf(lcd_putc,"\f %x d[%d]=%x",adres,i,d[i]);
delay_ms(500);
}
} |
I used pull up resistors 4.7K. I am using ISIS Prof the eeprom is 24C02C, pic is 16877A. Very easy but I have to admit I am fed up. A0 A1 A2 are connected to the ground. WP is connected to the ground. Eeprom writes and read. I get the ack but eeprom reads FF all the time. How can write the eeprom the correct bytes?? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Apr 01, 2008 1:10 am |
|
|
Quote: | #include <2402.C>
#use I2C(MASTER, sda=PIN_C4, scl=PIN_C3,FAST,FORCE_HW ) #include <flex_lcd.C>
int8 veri=0x00,adres=0x00;
int i,d[10];
void main()
{ #use I2C(MASTER, sda=PIN_C4, scl=PIN_C3,FAST,FORCE_HW ) |
Remove the two lines shown in bold. The 2402.c file already has a
#use i2c() statement in it. Edit the 2402.c file and change the #use i2c
statement so it uses pins C4 and C3. Don't use FAST. Don't use
FORCE_HW. If you get the code to work, you can add those parameters
later. |
|
|
|