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

hardware i2c
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ZeRon



Joined: 15 Dec 2007
Posts: 6

View user's profile Send private message

hardware i2c
PostPosted: Sat Dec 22, 2007 3:23 pm     Reply with quote

hello
i connected an 24LC64 EEPROM to a PIC16F876 I used the 2464.c file that is available with CCS. PIN_B6 and PIN_B7 for SDA and SCL .
I just want to asked ,is this a software I2C ?
because I noticed that when using the wizard we have the option of hardware connection and normally I2C pins are C4 and C3. so is there a difference. ?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Dec 22, 2007 4:06 pm     Reply with quote

Quote:

I connected an 24LC64 EEPROM to a PIC16F876 I used the 2464.c file
that is available with CCS. PIN_B6 and PIN_B7 for SDA and SCL .
Is this software I2C ?

Yes.

Quote:

I noticed that when using the wizard we have the option of hardware connection and normally I2C pins are C4 and C3

You can use software i2c on most PIC pins. Many PICs have an MSSP
module, which has dedicated hardware i2c pins. With those pins (C3 and
C4 for the 16F876), you have the option of using either hardware or
software i2c.
ZeRon



Joined: 15 Dec 2007
Posts: 6

View user's profile Send private message

PostPosted: Sun Dec 23, 2007 4:44 am     Reply with quote

Thx PCM programmer for your reply
but is there an advantage of hardware over software because im having trouble with reading data from the eeprom for example to fill an array
for (j=0;j<40;j++){
P[j]=read_ext_eeprom(j);
}
the array is not filled right,
note that im also using the RTC interrupt to update Pins on portB and im using PIN_B6 and PIN_B7 for i2c communication with the eeprom so might the software i2c be affected by the change of other pins on portb using rtc interrupt?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Dec 23, 2007 5:56 pm     Reply with quote

You can find out if interrupts are causing a probelm by disabling Global
interrupts during the i2c operations.
Here's an example of how to modify an i2c function to do this.
Add the lines shown in bold to the start and the end of each i2c function.
Quote:
void PCF8583_write_byte(int8 address, int8 data)
{
disable_interrupts(GLOBAL);
i2c_start();
i2c_write(PCF8583_WRITE_ADDRESS);
i2c_write(address);
i2c_write(data);
i2c_stop();
enable_interrupts(GLOBAL);
}
ZeRon



Joined: 15 Dec 2007
Posts: 6

View user's profile Send private message

PostPosted: Thu Dec 27, 2007 12:40 pm     Reply with quote

Hello PCM Programmer
Well I changed the EXTEE example a little bit to try my hardware I2C
well it worked by using
#use i2c(Master,Fast,sda=PIN_C4,scl=PIN_C3,force_hw)
and added some lines to it
void main()
{
BYTE value, cmd ,i,j;
EEPROM_ADDRESS address;

setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);

init_ext_eeprom();
for (i=0 ;i<100;i++){WRITE_EXT_EEPROM( i+15, i );}
do {
printf("\r\n\nEEPROM:\r\n"); // Display contents of the first 64
for(i=0; i<=10; ++i) { // bytes of the data EEPROM in hex
for(j=0; j<=15; ++j) {
printf( "%2x ", READ_EXT_EEPROM( i*16+j ) );
}
printf("\n\r");
}
do {

printf("\r\nRead or Write: ");
cmd=getc();
cmd=toupper(cmd);
putc(cmd);
} while ( (cmd!='R') && (cmd!='W') );

printf("\n\rLocation: ");

#if sizeof(EEPROM_ADDRESS)==1
address = gethex();
#else
#if EEPROM_SIZE>0xfff
address = gethex();
#else
address = gethex1();
#endif
address = (address<<8)+gethex();
#endif

if(cmd=='R')
printf("\r\nValue: %X\r\n",READ_EXT_EEPROM( address ) );

if(cmd=='W') {
printf("\r\nNew value: ");
value = gethex();
printf("\n\r");
WRITE_EXT_EEPROM( address, value );
}
} while (TRUE);
}

well everything worked but when i added the
for (i=0 ;i<100;i++){WRITE_EXT_EEPROM( i+15, i );}
the program would not respond
i noticed when i removed this and run the program again that the only the first byte is right "at address 15"
EEPROM:
00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 00
ff ff ff a1 ff b1 ff a1 a1 a1 ff a1 ff a1 ff a1
a1 a1 22 ff ff a1 a1 a1 ff a1 ff a1 ff a1 ff a1
ff a1 ff a1 ff a1 ff a1 ff a1 ff a1 ff b1 ff a1
ff a1 ff a1 ff a1 ff a1 ff a1 ff a1 ff a1 ff a1
ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff

any ideas?
thx you again for your time
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Dec 27, 2007 1:15 pm     Reply with quote

This test program is too complicated for me.
Heath



Joined: 21 Dec 2007
Posts: 41

View user's profile Send private message

PostPosted: Thu Dec 27, 2007 1:36 pm     Reply with quote

What do you mean by "the program would not respond"?
ZeRon



Joined: 15 Dec 2007
Posts: 6

View user's profile Send private message

PostPosted: Thu Dec 27, 2007 1:47 pm     Reply with quote

never mind PCM programmer thank you for your time again .
Well ,Hello Heath,i mean that the program wouldnt continue, the "EEPROM ...." text doenst appear and program stops . when i remove the
"for (i=0 ;i<100;i++){WRITE_EXT_EEPROM( i+15, i );} "
the programs runs normally and the first 64 bytes are read.
Heath



Joined: 21 Dec 2007
Posts: 41

View user's profile Send private message

PostPosted: Thu Dec 27, 2007 2:00 pm     Reply with quote

Well, i'm not an expert on I2C but I would try the following.

  • Try to write to one location 100 times.
  • Try to write to just one location once.
  • Try to write to a smaller number of locations.


Do you have any other kind of peripherals with this setup that communicate over the I2C bus? Do you have any other interrupts setup? (Timers, serial, etc)

Did you disable/enable interrupts like PCMP suggested?
Heath



Joined: 21 Dec 2007
Posts: 41

View user's profile Send private message

PostPosted: Thu Dec 27, 2007 2:09 pm     Reply with quote

One thing I noticed that would need to be changed in the future is your 'i' counter that you're using for an address. It is defined as a byte (int8). The default is unsigned so your range would be 0-255. The Write function you're using specifies a 'long int' or int16 that has a range of 0-65535.
ckielstra



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

View user's profile Send private message

PostPosted: Thu Dec 27, 2007 3:25 pm     Reply with quote

Heath wrote:
One thing I noticed that would need to be changed in the future is your 'i' counter that you're using for an address. It is defined as a byte (int8). The default is unsigned so your range would be 0-255. The Write function you're using specifies a 'long int' or int16 that has a range of 0-65535.
The compiler should automatically upgrade the parameter to an int16 so this isn't an error.
ckielstra



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

View user's profile Send private message

PostPosted: Thu Dec 27, 2007 3:30 pm     Reply with quote

ZeRon,

Which compiler version are you using?

When posting program code please use the 'code' buttons to preserve formatting for much easier reading.

Please post a short and complete test program. The code you have posted now is containing code you don't use (the handling of 'R' and 'W' commands) and is incomplete (missing the #fuse settings).

The easier you make it for us to read and reproduce your problem, the quicker and better responses you will get.

If you use the unmodified version of the EXTEE example can you then read and write to the EEPROM using the 'R' and 'W' commands?


Last edited by ckielstra on Thu Dec 27, 2007 3:32 pm; edited 1 time in total
Heath



Joined: 21 Dec 2007
Posts: 41

View user's profile Send private message

PostPosted: Thu Dec 27, 2007 3:31 pm     Reply with quote

ckielstra wrote:
Heath wrote:
One thing I noticed that would need to be changed in the future is your 'i' counter that you're using for an address. It is defined as a byte (int8). The default is unsigned so your range would be 0-255. The Write function you're using specifies a 'long int' or int16 that has a range of 0-65535.
The compiler should automatically upgrade the parameter to an int16 so this isn't an error.


I believe you're talking about how the compiler will change the int8 to an int16 when it is passed to the function. That is probably true but that is not what I was pointing out. When he's having 'i' count up he'll never reach a large portion of his addresses because he only has it defined as an int8.

In other words, he'll cover 0-270 but he won't be able to write to 271-65535. His final program won't have this for loop or the 'i' counter being responsible for the addressing but I just thought I would point that out.
ckielstra



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

View user's profile Send private message

PostPosted: Thu Dec 27, 2007 3:33 pm     Reply with quote

Heath,
Ok, then I'm sorry for misunderstanding what you tried to point out.
Heath



Joined: 21 Dec 2007
Posts: 41

View user's profile Send private message

PostPosted: Thu Dec 27, 2007 3:36 pm     Reply with quote

no problem at all sir. I should have been clearer in my third post!
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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