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 CCS Technical Support

I2C clock frequency

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



Joined: 03 May 2004
Posts: 11

View user's profile Send private message

I2C clock frequency
PostPosted: Mon May 10, 2004 5:56 pm     Reply with quote

Hi,
I have some problems using fast mode of I2c and obtain different clock freqency.

1.
Here is my code
Code:

    #include <16f877.H>
    #fuses XT, NOPROTECT, BROWNOUT, PUT, NOWDT, NOLVP
    #use delay(clock=20000000)
    #use i2c(Master,fast,sda=PIN_C4, scl=PIN_C3, FORCE_HW)

    //========================
    #byte SSPADD = 0x93
    main ()
   {
       //output_low(PIN_C3);
      //output_low(PIN_C4);
     //SSPADD = 0X0C;   //400kHZ when SSPAD = 0X0C, (20)/[4*(12+1)]
       output_float(PIN_C3);
      output_float(PIN_C4);
       i2c_start();
       i2c_write(0x1E);
      i2c_write(0x0f);
      i2c_start();
      i2c_write(0x1f);
      i2c_read(0); 
      i2c_stop();

}


Using scope I found When I tried to read I only get 6 clock, and missing stop bit.

however if use

Code:


#include <16f877.H>
    #fuses XT, NOPROTECT, BROWNOUT, PUT, NOWDT, NOLVP
    #use delay(clock=4000000)
    #use i2c(Master,sda=PIN_C4, scl=PIN_C3, FORCE_HW)


I could always get last 8 clock and stop bits, which works correctly.
Does any one know what is problem here?

2. According to data sheet I could use sspdat to control clock frequency, for PIC 16f 877, clock frequency is 20M hz, if I want to have 400 khz, I assigned 0x0C, but no signal at all, sspadd only works at 0x0f.
All the other value such as 0x32(100khz), doesn't work at all. Did I miss something?

3. The most wierd thing is above code works fine except in read, however When I tried to add sspdat = 0x0c, It didn't produce any signal, That's fine, then comment this, recompile the program, orginal one doesn't work too(no signal looks like can not pull down), unless I put output_low(), comment output_float(), compile, run. then next time comment output_low(), uncommen output_float(), I'm really frustrated making such changes.
Does anyone know why? I'm using MPLAB ide 6.5, PCWH3.41, PCM3.181
PIC 16F877, pull up resister is 1K.

Thanks

Simin
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon May 10, 2004 6:29 pm     Reply with quote

Quote:
#include <16f877.H>
#fuses XT, NOPROTECT, BROWNOUT, PUT, NOWDT, NOLVP
#use delay(clock=20000000)


I see two things that could cause problems:

1. Your #fuses statement is set for the XT oscillator mode, but
you're using a 20 MHz crystal. It should be set for HS when
using this crystal. (Use XT for a 4 MHz crystal).

2. You're letting the code fall off the end of main(), where it will
hit a SLEEP instruction. Put a while(1); statement at the end
of main() to prevent this.
Guest








PostPosted: Mon May 10, 2004 7:16 pm     Reply with quote

PCM, Thanks for quick reply. Here is modified code
Code:

#include <16f877.H>
#fuses hs, NOPROTECT, BROWNOUT, PUT, NOWDT, NOLVP
#use delay(clock= 20000000)
#use i2c(Master, fast,sda=PIN_C4, scl=PIN_C3, FORCE_HW)

//========================
#byte SSPADD = 0x93
main ()
{
//output_low(PIN_C3);
//output_low(PIN_C4);
//SSPADD = 0X0F;   //400kHZ when SSPAD = 0X0C, (20)/[4*(12+1)]
output_float(PIN_C3);
output_float(PIN_C4);
i2c_start();
i2c_write(0x1E);
i2c_write(0x0f);
i2c_start();
i2c_write(0x1f);
i2c_read(0);
i2c_stop();
while(1)
{
;

}

}

Now I could get last 7 clock(sometimes 8 clock), but still miss last stop bit.
If I uncomment sspdat = 0x0f, it didn't produce any signal(this time even 0f not works). if I comment sspdat part, original code not works, unless I uncomment outputlow() part,comment output_float() part compile run it. Then back to orignal code it produce signal.

Thanks

Simin
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon May 10, 2004 7:40 pm     Reply with quote

1. What i2c device are you using ? (Give the manufacturer and
part number).

2. Your 1K pull-ups are a bit on the strong side, according to
the i2c specification. They lowest allowable value is 1.6K ohms.
Guest








PostPosted: Mon May 10, 2004 8:17 pm     Reply with quote

PCM, thanks. This I2c device is the chip which designed several years ago in our company, I'm now using i2c try to communicate with it, I didn't see the I2c specification yet. I will try to get it by Wednesday, Which key part of data do you think will not let sspdat and fast mode work? Why it works under 4Mhz(SSPDAT still not work).

Again, thanks a lot!

Simin
dais



Joined: 03 May 2004
Posts: 11

View user's profile Send private message

PostPosted: Mon May 10, 2004 10:40 pm     Reply with quote

I will try to test the code without slave. Question is
1.
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use i2c(master, fast, sda = PIN_C4, scl = PIN_C3, force_hw )

without assigning sspadd, what' default clock speed will SCL be?
Is the oscillator of PIC16F877 running at 20Mhz?

2. Using the SSPADD formula, if clock is 20Mhz we can get from
5Mhz(SSPADD = 0)- 19(SSPADD =255) Khz. according data sheet there are two mode one is 100 khz, the other is 400 khz. If in 400khz, it vary from 19khz-400khz? If in 100 khz, it vary from 19khz-100khz? Is there any minimum clock frequency required?


3. Without ackknowlegement from slave, Can I use sspadd to change speed of clock? For example only use I2c_write() function, and changing sspadd to see if clock speed changes.

thanks!

Simin
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue May 11, 2004 12:20 am     Reply with quote

Quote:
This I2c device is the chip which designed several years ago in our company, I'm now using i2c try to communicate with it, I didn't see the I2c specification yet. I will try to get it by Wednesday, Which key part of data do you think will not let sspdat and fast mode work? Why it works under 4Mhz(SSPDAT still not work).

The chip may have been designed to only work with a 100 KHz
i2c clock. (ie., slow speed). You need to find a data sheet
(if they made one) or talk to the chip's designer, or talk to
some other engineer who has used the chip.

Quote:
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use i2c(master, fast, sda = PIN_C4, scl = PIN_C3, force_hw )

Question is
1. Without assigning sspadd, what' default clock speed will SCL be?
Is the oscillator of PIC16F877 running at 20Mhz?


You have specified Fast mode, so the compiler will automatically
calculate the proper value for the SSPADD register, based on
your 20 MHz clock speed. The value it calculates is 0x0B (11 decimal).
The formula is:
Code:

                20 MHz
Baud rate =  ---------------  =  417 KHz
                4 * (11 +1)

Due to internal delays, the actual i2c clock speed may be somewhat
different than this.

Quote:
Is there any minimum clock frequency required?

As I recall, if you run the PIC at 20 MHz, the internal delays in the baud
rate generator will have less effect, and the baud rate will be closer to
the expected frequency (based on the formula above).
I have not done any recent experiments to test the SCL clock speed
if the crystal is a low frequency, so I can't answer that question.

Quote:
Without acknowlegement from slave, Can I use sspadd to change
speed of clock? For example only use I2c_write() function, and changing sspadd to see if clock speed changes.

Yes, you can write a program that has a while() loop, with only one
line of code in it: i2c_write(0x55);
Then the PIC will transmit the same byte continuously. You can
observe the SCL signal. You might want to disconnect the slave chip
from the i2c bus, to make sure it doesn't interfere with your test.
Guest








PostPosted: Tue May 11, 2004 11:46 am     Reply with quote

Thanks, I will test tomorrow and post results.

Simin
dais



Joined: 03 May 2004
Posts: 11

View user's profile Send private message

PostPosted: Wed May 12, 2004 3:55 pm     Reply with quote

Hi,
Bug fixed. Program is good, It is slave problem.
PCM, thanks for the help!

Simin
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