MODULES FOR MCP23008 AND MCP9800

Coding and general discussion relating to user created compiler modules

Moderators: David Barker, Jerry Messina

Post Reply
LEEDNH
Posts: 42
Joined: Fri Feb 03, 2017 8:44 pm

MODULES FOR MCP23008 AND MCP9800

Post by LEEDNH » Tue Nov 26, 2019 2:07 am

Hello everyone,
I am looking for help with 2 modules written by Jon Chandler.
The two modules are related to the use of i2c with Swordfish for the MCP23008 AND MCP9800 parts.
Unfortunately, the "DIY" site appears to be down (at least for me).

Any help is appreciated!
regards
Lee

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

Re: MODULES FOR MCP23008 AND MCP9800

Post by octal » Tue Nov 26, 2019 9:42 am

The MCP23008 modules works almost the same as the MCP23S17 for which I already published an Swordfish BASIC module some years ago. Check the modules page http://www.sfcompiler.co.uk/wiki/pmwiki ... er.Modules

If you have special requirements, I can take a bit of time to make a generic module that works for any port expander.

For the temp sensor MCP9800, the specs are easy to follow on the datasheet. I don't own any of them but I can try to help you on this one if you can do all testing part.

Regards

Jerry Messina
Swordfish Developer
Posts: 1469
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: MODULES FOR MCP23008 AND MCP9800

Post by Jerry Messina » Tue Nov 26, 2019 11:21 am

Here's some old code I had hanging around for an MCP9800... hopefully it's enough to get you started

Code: Select all

//
//------------------------------------------------------------------------------
//  Name    : mcp9800.bas
//  Author  : Jerry Messina
//  Date    : 9/25/2014
//  Version : 1.0.1
//  Notes   :
//      MCP9800 I2C temperature driver
//
//------------------------------------------------------------------------------
//
module mcp9800

// I2C support
include "I2C.bas"

// I2C constants...
public const 
    I2C_ACK = 0,        // acknowledge data bit (acknowledge)
    I2C_NACK = 1        // acknowledge data bit (NOT acknowledge)

// I2C RD/WR# bit (lsb of the address byte)... 1=READ, 0=WRITE
public const 
    RD_WRN  = 0,    // bit(0)
    R_WN    = (1<<RD_WRN)
    
// mcp9800 device address
const
    MCP9800_ADDR as byte = %10010000    // single address

// mcp9800 register addresses
const
    TA      as byte = $00,      // temperature reg
    TCFG    as byte = $01,      // configuration reg
    THYST   as byte = $02,      // temp hysteresis reg
    TSET    as byte = $03       // temp limit-set reg


// TCFG sensor config register bits
// PON default: continous conversion, 9-bit, Tset=80, Thyst=75
const
    SHUTDOWN    = 0,    // 0=disable (default), 1=enable
    COMP_INT    = 1,    // 0=comparator mode (default), 1=interrupt mode
    ALERT_POL   = 2,    // 0=active low (default), 1=active high
    FAULT_0     = 3,    // 00=1(default), 01=2, 10=4, 11=6
    FAULT_1     = 4,
    RES_0       = 5,    // 00=9bit (0.5)(default), 01=10bit (0.25),
    RES_1       = 6,    // 10=11bit (0.125), 11=12bit (0.0635)
    ONESHOT     = 7     // 0=disabled (default), 1=enabled

const
    DEFAULT_TCFG = %00000000
    
// all temp data registers are comprised of two bytes and are in degrees C (2's compliment format)
//
// MSB: SIGN    2^6     2^5     2^4     2^3     2^2     2^1     2^0 
// LSB: 2^-1    2^-2    2^-3    2^-4    0       0       0       0
//
// Thyst and Tset are only 0.5degC resolution (2^-1, 9-bit), and rest are 0's
//

// conversion time and resolution
//  9-bit   0.5     30-75ms     33 samples/sec
//  10-bit  0.25    60-150ms    17 samples/sec
//  11-bit  0.125   120-300ms   8 samples/sec
//  12-bit  0.0625  240-600ms   4 samples/sec
//
// at powerup there is a 2ms delay before initialization and conversions begin
//

// This function checks the status of WriteByte operations. If the slave receiver 
// acknowledged the transfer (bit 9 ACK=0) then AckStat=true.
// If the receiver fails to ack the transfer (bit 9 ACK=1) this means the
// slave is busy (or isn't present) and AckStat=false. You should
// resend the byte
public function AckStat() as boolean
    if (I2C.ACKSTAT = 0) then
        result = true
    else
        result = false
    endif                
end function

//
// device_check
// attempts to verify that it can read/write to a chip
// returns 0=ok, other=fail
//
public function device_check() as byte
    result = 0      // assume ok
    Start()
    WriteByte(MCP9800_ADDR)
    if (AckStat() = false) then     // we got NACK'ed
        result = 1                  // set fail... not detected
    endif
    Stop()
end function

public sub write_config(b as byte=DEFAULT_TCFG)
    Start()
    WriteByte(MCP9800_ADDR)
    WriteByte(TCFG)
    WriteByte(b)
    Stop()
end sub

public function read_config() as byte
    Start()
    WriteByte(MCP9800_ADDR)
    WriteByte(TCFG)
    Restart()
    WriteByte(MCP9800_ADDR + R_WN)
    result = ReadByte(I2C_NACK)
    Stop()
end function


public function read_temp() as integer
    Start()
    WriteByte(MCP9800_ADDR)
    WriteByte(TA)
    Restart()
    WriteByte(MCP9800_ADDR + R_WN)
    result.byte1 = ReadByte(I2C_ACK)
    result.byte0 = ReadByte(I2C_NACK)
    Stop()
end function

end module

bitfogav
Registered User
Registered User
Posts: 169
Joined: Sat Oct 09, 2010 1:39 pm
Location: United Kingdom

Re: MODULES FOR MCP23008 AND MCP9800

Post by bitfogav » Tue Nov 26, 2019 7:48 pm

I have the module by Jon here for the MCP23008 - I'm not sure if its the latest version but it was one I had in my library.. :)
MCP23008.zip
(4.48 KiB) Downloaded 203 times

LEEDNH
Posts: 42
Joined: Fri Feb 03, 2017 8:44 pm

Re: MODULES FOR MCP23008 AND MCP9800

Post by LEEDNH » Tue Nov 26, 2019 9:30 pm

Thank you octal, Jerry, and bitfogav!

I have had success with swordfish basic i2c interfacing to eeprom and an fs6377 pll.
I wanted to have a look at the code for the MCP23008 AND MCP9800 to see how I might improve my skills.
I cannot access digitaldiy - it may just be me....
Thanks again, guys!

LEEDNH
Posts: 42
Joined: Fri Feb 03, 2017 8:44 pm

Re: MODULES FOR MCP23008 AND MCP9800

Post by LEEDNH » Tue Nov 26, 2019 10:09 pm

octal, thanks for pointing out the module you wrote for the mcp23s17.
It looks like the module mcp23s17 may also be a good match for the MCP3302/04.
regards lee

Post Reply