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 problem

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








I2C problem
PostPosted: Tue Sep 29, 2009 8:30 am     Reply with quote

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!
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Tue Sep 29, 2009 9:25 am     Reply with quote

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
Guest








PostPosted: Tue Sep 29, 2009 9:55 am     Reply with quote

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!
Guest








PostPosted: Tue Sep 29, 2009 9:58 am     Reply with quote

Sorry! Me again, the xtal is 18.432 Mhz, not 20mhz like I said on the first post.

Compiler version 4.057
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Tue Sep 29, 2009 12:04 pm     Reply with quote

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
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Sep 29, 2009 12:13 pm     Reply with quote

His pin numbers will be used, because the 2432.c file has an #ifndef
block around the pins in the driver file. His pins will replace these pins:
Code:

#ifndef EEPROM_SDA
#define EEPROM_SDA  PIN_C4
#define EEPROM_SCL  PIN_C3
#endif


My suggestion is to make a stripped down test program that doesn't
use interrupts, and only tests the external eeprom. Examples:
http://www.ccsinfo.com/forum/viewtopic.php?t=28199&start=1
http://www.ccsinfo.com/forum/viewtopic.php?t=17787&start=2
Guest








PostPosted: Wed Sep 30, 2009 10:06 am     Reply with quote

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!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Sep 30, 2009 10:09 am     Reply with quote

Let the compiler set the TRIS. Use 4.7K pullups.
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Wed Sep 30, 2009 11:26 am     Reply with quote

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
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