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 clk frequency at 10KHz

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







I2C clk frequency at 10KHz
PostPosted: Thu Apr 03, 2003 6:34 am     Reply with quote

Hi!

How can i set the I2C clk frequency at 10Khz, i'm using a 16f877 device.
___________________________
This message was ported from CCS's old forum
Original Post ID: 13350
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

Re: I2C clk frquency at 10KHz
PostPosted: Thu Apr 03, 2003 1:40 pm     Reply with quote

<font face="Courier New" size=-1>:=Hi!
:=
:=How can i set the I2C clk frequency at 10Khz, i'm using a 16f877 device.
-------------------------------------------------

There are two ways.

1. You can use the hardware i2c module in the 16F877.
Use pins C3 and C4 for this. In the #use i2c statement
tell the compiler to "FORCE_HW", to enable H/W i2c.

Then, in your code, set the i2c clock speed by writing
directly to the SSPADD register.

You'll have to use a crystal of 5 MHz or less, in order to
get a frequency as low as 10 KHz. That's due to the baud rate
formula, and because only the lower 7 bits of SSPADD is used
by the baud rate generator.

The formula to calculate the SSPADD value is:

<PRE>
5 MHz
SSPADD = ---------- -1
4 x 10 KHz
</PRE>

So SSPADD = 124

To program SSPADD, use this code:

#byte SSPADD = 0x93 // Put this above main()

main()
{
SSPADD = 124;

// Put other code here.

}

--------------------------------

The 2nd way is to use your own i2c library.
Then you can set the clock speed by controlling the
length of the software delay loops.

Peter Anderson has some sample i2c code. You can see that
the clock speed is controlled by the delay_10us() function.
To slow down the clock, just use longer delays. Also, you
don't have to use his delay_10us() function. You can substitute
the acutal CCS delay routines. He just used his own delay
function because he wanted to make the code be compiler-independent. (Even though he uses the CCS compiler).
<a href="http://www.phanderson.com/PIC/PICC/CCS_PCM/24_256_1.html" TARGET="_blank"> <a href="http://www.phanderson.com/PIC/PICC/CCS_PCM/24_256_1.html" TARGET="_blank">http://www.phanderson.com/PIC/PICC/CCS_PCM/24_256_1.html</a></a>

--------------------------------

There is a 3rd "cheap" way, which is to change the #use delay
value so it's much faster than your actual crystal speed.
Then use software i2c. The compiler will insert more delay
loops than normal, and this slows down the software i2c clock.
I would not recommend this method, because it screws up your
delay_us and delay_ms functions. You'll always have to
remember to adjust them to give the proper delay you want.
It's not worth it. Certainly not in a real product, where
the code has to be maintained.


[Edited to correct the above paragraph, so that it says
"much faster". Ie., if you're running with a 4 MHz crystal,
set #use delay(clock=20000000) to make the delays be 5x longer.
ie., if you use delay_us(1), it will really be a 5us delay.
Again, I do not recommend doing this, for the reasons given
above.]</font>
___________________________
This message was ported from CCS's old forum
Original Post ID: 13370
Patrik Berglund
Guest







Re: I2C clk frquency at 10KHz
PostPosted: Fri Apr 04, 2003 12:28 pm     Reply with quote

:=:=Hi!
:=:=
:=:=How can i set the I2C clk frequency at 10Khz, i'm using a 16f877 device.
:=-------------------------------------------------
:=
:=There are two ways.
:=
:=1. You can use the hardware i2c module in the 16F877.
:= Use pins C3 and C4 for this. In the #use i2c statement
:= tell the compiler to "FORCE_HW", to enable H/W i2c.
:=
:=Then, in your code, set the i2c clock speed by writing
:=directly to the SSPADD register.
:=
:=You'll have to use a crystal of 5 MHz or less, in order to
:=get a frequency as low as 10 KHz. That's due to the baud rate
:=formula, and because only the lower 7 bits of SSPADD is used
:=by the baud rate generator.
:=
:=The formula to calculate the SSPADD value is:
:=
:=<PRE>
:= 5 MHz
:=SSPADD = ---------- -1
:= 4 x 10 KHz
:=</PRE>
:=
:=So SSPADD = 124
:=
:=To program SSPADD, use this code:
:=
:=#byte SSPADD = 0x93 // Put this above main()
:=
:=main()
:={
:=SSPADD = 124;
:=
:=// Put other code here.
:=
:=}
:=
:=--------------------------------
:=
:=The 2nd way is to use your own i2c library.
:=Then you can set the clock speed by controlling the
:=length of the software delay loops.
:=
:=Peter Anderson has some sample i2c code. You can see that
:=the clock speed is controlled by the delay_10us() function.
:=To slow down the clock, just use longer delays. Also, you
:=don't have to use his delay_10us() function. You can substitute
:=the acutal CCS delay routines. He just used his own delay
:=function because he wanted to make the code be compiler-independent. (Even though he uses the CCS compiler).
:= <a href="http://www.phanderson.com/PIC/PICC/CCS_PCM/24_256_1.html" TARGET="_blank"> <a href="http://www.phanderson.com/PIC/PICC/CCS_PCM/24_256_1.html" TARGET="_blank">http://www.phanderson.com/PIC/PICC/CCS_PCM/24_256_1.html</a></a>
:=
:=--------------------------------
:=
:=There is a 3rd "cheap" way, which is to change the #use delay
:=value so it's much slower than your actual crystal speed.
:=Then use software i2c. The compiler will insert more delay
:=loops than normal, and this slows down the software i2c clock.
:=I would not recommend this method, because it screws up your
:=delay_us and delay_ms functions. You'll always have to
:=remember to adjust them to give the proper delay you want.
:=It's not worth it. Certainly not in a real product, where
:=the code has to be maintained.
___________________________
This message was ported from CCS's old forum
Original Post ID: 13408
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