View previous topic :: View next topic |
Author |
Message |
kloppy
Joined: 28 Jul 2004 Posts: 32
|
2 software i2c and hardware spi |
Posted: Wed Aug 25, 2004 4:46 am |
|
|
is it possible to implement 2 software i2c and 1 hardward spi in a 16f876? |
|
|
Charlie U
Joined: 09 Sep 2003 Posts: 183 Location: Somewhere under water in the Great Lakes
|
|
Posted: Wed Aug 25, 2004 6:17 am |
|
|
Yes, I have done it, and it worked quite well. |
|
|
treitmey
Joined: 23 Jan 2004 Posts: 1094 Location: Appleton,WI USA
|
|
|
Charlie U
Joined: 09 Sep 2003 Posts: 183 Location: Somewhere under water in the Great Lakes
|
|
Posted: Wed Aug 25, 2004 4:41 pm |
|
|
Sorry, but I can't post the actual code because it belongs to one of my clients. However, it isn't that tough to recreate. The major hurdle (which isn't very high ;-) ) is how to organize access through the various interfaces.
I'll walk through the concept and let you ask questions later if this doesn't end up being very clear.
Let's say you are controlling a digital pot with the hardware spi interface, and you want to communicate with a real time clock via I2C and a memory chip via I2C. Unfortunately, the rtc and memory have the same i2c address. You now need to use 2 separate i2c interfaces. One great feature of the CCS compiler is that hardware and software I2C interfaces are handled the same. The compiler takes care of all of the house keeping.
First, set up the spi as you would for any other application. There are plenty of examples on this forum and in the example files. I would then create an include file that contains all of the functions that you need to control the digital pot. Call this file dig_pot.c for example.
Next, create a file for functions that access the rtc for setting and reading the time, etc. Call this file rtc_io.c for example. At the beginning of this file include the directive:
#use I2C (master, sda=RTC_SDA, scl=RTC_SCL, FAST)
where RTC_SDA and RTC_SCL are previously defined pins that you are using for the rtc i2c interface. Include in this file your set of functions for accessing the rtc such as rtc_set_minutes(); rtc_set_hours(); rtc_read_minutes(); etc. These functions will include the standard CCS I2C access calls.
Next, create a file for functions that read and write the memory. Call this file mem_io.c for example. At the beginning of this file include the directive:
#use I2C (master, sda=MEM_SDA, scl=MEM_SCL, FAST)
where MEM_SDA and MEM_SCL are previously defined pins that you are using for the memory i2c interface. Again, include in this file your set of functions for reading and writing memory such as read_ext_mem(); or write_ext_mem(). As above, these functions will include the standard CCS I2C access calls.
The compiler will automatically use the correct pins for each function, based on the previous #use directive. As I recall, you should use standard io for the ports that include the soft i2c interfaces, since the data pin of an i2c interface changes from an output to an input and back during a single i2c call.
Let us know if this is helpful.
Charlie |
|
|
treitmey
Joined: 23 Jan 2004 Posts: 1094 Location: Appleton,WI USA
|
|
Posted: Thu Aug 26, 2004 9:54 am |
|
|
Any considerations for the soft port. Is open collector OK? or will most any pin do OK? |
|
|
Charlie U
Joined: 09 Sep 2003 Posts: 183 Location: Somewhere under water in the Great Lakes
|
|
Posted: Thu Aug 26, 2004 10:23 am |
|
|
Any port should work fine, open collector or not. When a pin is idle, the compiler will make it an input so it does not load the bus. Don't forget the standard i2c pullups on each bus, both clock and data, somewhere between 2.2k and 4.7k. Other values may work, but this is the common range. |
|
|
treitmey
Joined: 23 Jan 2004 Posts: 1094 Location: Appleton,WI USA
|
|
Posted: Fri Aug 27, 2004 8:03 am |
|
|
Thanks |
|
|
|