View previous topic :: View next topic |
Author |
Message |
Guest
|
I2C problem |
Posted: Tue Sep 29, 2009 8:30 am |
|
|
Hi! I'm working on a project with pic 18F252.
The circuit has a 24LC32 connected like this: (software I2C).
Code: |
#define EEPROM_SDA PIN_A2
#define EEPROM_SCL PIN_A1
|
I can't change the pins used for clock and data. Each line has a 4k7 pullup.
Sometimes the memory fails to read/write or its TOO slow... its unusual, but happens with some chips.
I measured the frequency of the SCL line and I have 10khz. I'm using a 20Mhz xtal. I know that if I use the software i2c I can't complain about the speed. But I need more stability.
What can I do? Higher pullups? Some modification on the code? Note that I CAN'T change the PCB.
Thanks! |
|
![](templates/subSilver/images/spacer.gif) |
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Tue Sep 29, 2009 9:25 am |
|
|
Post your code so we can see what you're doing. Include your header as well so it can be compiled. It's kind of hard to trouble shoot when we can't see what's going on.
Ronald |
|
![](templates/subSilver/images/spacer.gif) |
Guest
|
|
Posted: Tue Sep 29, 2009 9:55 am |
|
|
The code is like 4500 lines.
The code that matters could be this?
definitions
Code: | #include <18f252.h>
#device ADC=8
#fuses HS, PROTECT, PUT, NOWDT, BROWNOUT, NOLVP, BORV42, CPD
#use delay(clock=18432000)
#use rs232(baud=19200, xmit=PIN_C6, rcv=PIN_C7)
#define EEPROM_SDA PIN_A2
#define EEPROM_SCL PIN_A1
#include <2432.c>
#include <lcd.c>
#include <bootloader.h>//sacar si no se va a usar el bootloader
#use fast_io(A)
#use fast_io(B)
#use fast_io(C)
|
tristate:
Code: |
set_tris_a (0b00010001); //Ra7-Ra6-Ra5-Ra4-Ra3-Ra2-Ra1-Ra0
set_tris_b (0b11000000); //Rb7-Rb6-Rb5-Rb4-Rb3-Rb2-Rb1-Rb0
delay_ms(50);
set_tris_b (0b00000000); //Rb7-Rb6-Rb5-Rb4-Rb3-Rb2-Rb1-Rb0
set_tris_c (0b10111101); //Rb7-Rb6-Rb5-Rb4-Rb3-Rb2-Rb1-Rb0 |
then, to read or write eeprom:
Code: | ///////////////////////////////////////////////////////////////////////////////
void write_int32_ext_eeprom(int16 direccion, int32 dato)
{
int dato_0,dato_1,dato_2,dato_3;
dato_3 = make8(dato,3); // Gets MSB of x
dato_2 = make8(dato,2);
dato_1 = make8(dato,1);
dato_0 = make8(dato,0);
write_ext_eeprom(direccion,dato_3);
direccion++;
write_ext_eeprom(direccion,dato_2);
direccion++;
write_ext_eeprom(direccion,dato_1);
direccion++;
write_ext_eeprom(direccion,dato_0);
return;
}
int32 read_int32_ext_eeprom(int16 direccion)
{
int32 dato;
int dato_0,dato_1,dato_2,dato_3;
dato_3=read_ext_eeprom(direccion);
direccion++;
dato_2=read_ext_eeprom(direccion);
direccion++;
dato_1=read_ext_eeprom(direccion);
direccion++;
dato_0=read_ext_eeprom(direccion);
dato=make32(dato_3,dato_2,dato_1,dato_0);
return(dato);
}
/////////////////////////////////////////////////////////////////////////////// |
thanks! |
|
![](templates/subSilver/images/spacer.gif) |
Guest
|
|
Posted: Tue Sep 29, 2009 9:58 am |
|
|
Sorry! Me again, the xtal is 18.432 Mhz, not 20mhz like I said on the first post.
Compiler version 4.057 |
|
![](templates/subSilver/images/spacer.gif) |
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Tue Sep 29, 2009 12:04 pm |
|
|
Just a few quick observations:
The driver file 2432.c has EEPROM_SDA & EEPROM_SCL defined already. You have them defined in your code. I'm not sure how the compiler will end up defining these. Most likely will use the last definition entered so it might depend on if your own definition comes before or after the inclusion of 2432.c.
You are using fast_io. You are responsible for controlling the direction of the tris during each I/O function. I'm not sure if the compiler will switch the pins appropriately during your software I2C functions. Could be flakey.
Make sure the hardware address of the eeprom is the same as the address being used in 2432.c
Your write_int32_ext_eeprom() function is declared as type VOID yet you have a return at the end. You might try removing this since it should not be needed. Might be causing something unsteady to happen, not sure.
Ronald |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
![](templates/subSilver/images/spacer.gif) |
Guest
|
|
Posted: Wed Sep 30, 2009 10:06 am |
|
|
Thanks for you answers. I'm looking for some configuration problem... like tris or timing. I don't know... its something that happens sometimes.
What is the recommended configuration for i2c? (tris, pullups, etc?)
thanks! |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Sep 30, 2009 10:09 am |
|
|
Let the compiler set the TRIS. Use 4.7K pullups. |
|
![](templates/subSilver/images/spacer.gif) |
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Wed Sep 30, 2009 11:26 am |
|
|
Don't use fast_io and don't set the tris. Take those commands out so the compiler can take care of it. You should only be controlling the direction of the port if you're an advanced programmer. If you don't keep track of things exactly then you will have problems.
Ronald |
|
![](templates/subSilver/images/spacer.gif) |
|