SI2C - Changing pins

General discussion relating to the library modules supplied with the compiler

Moderators: David Barker, Jerry Messina

Post Reply
User avatar
ohararp
Posts: 194
Joined: Tue Oct 03, 2006 11:29 pm
Location: Dayton, OH USA
Contact:

SI2C - Changing pins

Post by ohararp » Sat May 09, 2009 6:41 pm

All,

I have a project that requires multiple SI2C ports. Is there a simple way I can define 2 SI2C ports? Using the #option I am stuck with just 1 SI2C port currently. I know that you can use addressing with multiple devices on the same lines, but the ADC I am using is fixed in terms of its address and therefore I cannot have more than 1 of the same device on an SI2C port (MCP3421).
Thanks Ryan
$25 SMT Stencils!!!
www.ohararp.com/Stencils.html

User avatar
octal
Registered User
Registered User
Posts: 586
Joined: Thu Jan 11, 2007 12:49 pm
Location: Paris IDF
Contact:

Post by octal » Sat May 09, 2009 9:11 pm

Hello,
you cant have two instances of SI2C modules directly. You have to copy the whole SI2C module and do changes to use a second SI2C (and change the second module name), or you can rework the original module to handle an array of I2C modules and maybe add an index to the desired addressed module to all functions or subs that write/read something, or maybe add a global var in teh module to handle CURRENTly selected module.

The example you should try to see is the timers modules that can handle multiple timers at the same time. This is the technique I used sometimes ago for multiple SUSART modules.

This post may be of interrest ;)

http://www.sfcompiler.co.uk/forum/viewt ... highlight=

User avatar
ohararp
Posts: 194
Joined: Tue Oct 03, 2006 11:29 pm
Location: Dayton, OH USA
Contact:

Post by ohararp » Sun May 10, 2009 4:24 am

Octal,

Thanks for confirming my suspicions. All of your suggestions were some things I was going to try out, but I wanted to see if anybody had done this already. With that said I might try and implement a "set pin" routine similar to that used in the shift.bas library. I think this will keep code smaller than renaming the same library with different declares to the port pins. I'll be sure to report back with my findings/implementation!
Thanks Ryan
$25 SMT Stencils!!!
www.ohararp.com/Stencils.html

User avatar
ohararp
Posts: 194
Joined: Tue Oct 03, 2006 11:29 pm
Location: Dayton, OH USA
Contact:

Post by ohararp » Sun May 10, 2009 4:24 am

Octal,

Thanks for confirming my suspicions. All of your suggestions were some things I was going to try out, but I wanted to see if anybody had done this already. With that said I might try and implement a "set pin" routine similar to that used in the shift.bas library. I think this will keep code smaller than renaming the same library with different declares to the port pins. I'll be sure to report back with my findings/implementation!
Thanks Ryan
$25 SMT Stencils!!!
www.ohararp.com/Stencils.html

User avatar
ohararp
Posts: 194
Joined: Tue Oct 03, 2006 11:29 pm
Location: Dayton, OH USA
Contact:

Post by ohararp » Thu May 14, 2009 3:35 pm

A couple of people have asked for the MCP3421 code that I am using. This is currently only supporting a 16-bit read from the MCP3421.

Code: Select all

// if device and clock are omitted, then the compiler defaults to 
// 18F452 @ 20MHz - they are just used here for clarity...
Device = 18F452
Clock = 20

// import libraries...
Include "SI2C.bas"
Include "suart.bas"
Include "convert.bas"

// target 24LC128 I2C EEPROM device...
Const MCP3421_ADD = %11010000
Const MCP3421_CFG = %10011000 'Continuous Conversion,16-BIT Result,0x Gain
Const MCP3421_RD  = %11010001
{
****************************************************************************
* Name    : Setup
* Purpose : Setup MCP3421
****************************************************************************
}         
Sub Setup(pControl As Byte, pCmd As Byte)
    SI2C.Start
    SI2C.WriteByte(pControl)
    SI2C.WriteByte(pCmd)
    SI2C.Stop
End Sub
{
****************************************************************************
* Name    : ReadStr                                                        *
* Purpose : Read a string from a I2C EEPROM - uses 2 byte address          *
****************************************************************************
}
Sub Read16(pControl As Byte, ByRef pData As Word)
    SI2C.Start             
    SI2C.WriteByte(pControl)   
    pData.highbyte = SI2C.ReadByte()
    pData.lowbyte  =  SI2C.ReadByte()   
End Sub

// local variables...
Dim ADC As Word

UART.SetTX(PORTB.7)
UART.SetRX(PORTB.6)
UART.SetBaudrate(sbr9600)
UART.SetMode(umTrue)

While TRUE
    
    SI2C.Initialize
    
    Setup(MCP3421_ADD,MCP3421_CFG)
    Read16(MCP3421_RD,ADC)
    
    
    // display result...
    
    UART.Write(DecToStr(ADC), 13, 10)
    DelayMS(1000)
Wend
Thanks Ryan
$25 SMT Stencils!!!
www.ohararp.com/Stencils.html

Post Reply