Developing Library for the MAX7314 I2C 16-bit I/O Expander w/ PWM
Dependencies: mbed PCA9538_Expander
Revision 0:7e32b01354d3, committed 2011-04-13
- Comitter:
- uasenden
- Date:
- Wed Apr 13 23:17:22 2011 +0000
- Commit message:
- Rev 0
Changed in this revision
diff -r 000000000000 -r 7e32b01354d3 MAX7314.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MAX7314.cpp Wed Apr 13 23:17:22 2011 +0000 @@ -0,0 +1,211 @@ +/* + + MAX7314.cpp + + Library implementation file for the MAX7314 16-bit I/O Expander with PWM + + Tom Gambone + Chad Joppeck + + Based on code by mBed user: "Ale C.-" / Suky (PCA9538_Expander Library) + + 4/13/2011 - Initial porting + +*/ + +#include "mbed.h" + + + + +//***************************************************************************** + + + +MAX7314_OutputPin::MAX7314_OutputPin(ExpPinName Pin,PinName PIN_SDA,PinName PIN_SCL,unsigned char Address) + :Bus(PIN_SDA,PIN_SCL),_Pin(Pin),_Address(Address<<1) { + + unsigned char Temp; + + Bus.start(); + Bus.write(_Address & 0xFE); + Bus.write(0x03); + Bus.start(); + Bus.write(_Address | 0x01); + Temp=Bus.read(0); + Bus.stop(); + + Bus.start(); + Bus.write(_Address & 0xFE); + Bus.write(0x03); + Bus.write((~(0x01<<_Pin))&Temp); + Bus.stop(); +} + +void MAX7314_OutputPin::vWrite(int value) { + unsigned char Temp; + + Bus.start(); + Bus.write(_Address & 0xFE); + Bus.write(0x01); + Bus.start(); + Bus.write(_Address | 0x01); + Temp=Bus.read(0); + Bus.stop(); + + Bus.start(); + Bus.write(_Address & 0xFE); + Bus.write(0x01); + if (value==0) { + Bus.write((~(0x01<<_Pin))&Temp); + } else { + Bus.write((0x01<<_Pin)|Temp); + } + Bus.stop(); +} + +int MAX7314_OutputPin::read() { + unsigned char Temp; + + Bus.start(); + Bus.write(_Address & 0xFE); + Bus.write(0x01); + Bus.start(); + Bus.write(_Address | 0x01); + Temp=Bus.read(0); + Bus.stop(); + + return((Temp>>_Pin)&0x01); +} + +MAX7314_OutputPin& MAX7314_OutputPin::operator= (int value) { + vWrite(value); +} + +MAX7314_OutputPin::operator int() { + + return(read()); +} + + + +//***************************************************************************** + + + +MAX7314_InputPin::MAX7314_InputPin(ExpPinName Pin,PinName PIN_SDA,PinName PIN_SCL,unsigned char Address) + :Bus(PIN_SDA,PIN_SCL),_Pin(Pin),_Address(Address<<1) { + unsigned char Temp; + + Bus.start(); + Bus.write(_Address & 0xFE); + Bus.write(0x03); + Bus.start(); + Bus.write(_Address | 0x01); + Temp=Bus.read(0); + Bus.stop(); + + Bus.start(); + Bus.write(_Address & 0xFE); + Bus.write(0x03); + Bus.write((0x01<<_Pin)|Temp); + Bus.stop(); +} + +int MAX7314_InputPin::read() { + unsigned char Temp; + + Bus.start(); + Bus.write(_Address & 0xFE); + Bus.write(0x00); + Bus.start(); + Bus.write(_Address | 0x01); + Temp=Bus.read(0); + Bus.stop(); + + return((Temp>>_Pin)&0x01); +} + +MAX7314_InputPin::operator int() { + + return(read()); +} + + + +//***************************************************************************** + + + +MAX7314::MAX7314(PinName PIN_SDA,PinName PIN_SCL,unsigned char Address,PinName PIN_INT) + : Bus(PIN_SDA,PIN_SCL),_PIN_INT(PIN_INT),MAX7314_Event(PIN_INT) { + _Address=Address<<1; +} + +void MAX7314::vInit(unsigned char Dir,void (*fptr)(void)) { + + //Bus.frequency(400000); + + Bus.start(); + Bus.write(_Address & 0xFE); + Bus.write(0x03); + Bus.write(Dir); + Bus.stop(); + + if (fptr!=NULL) { + MAX7314_Event.fall(fptr); + } +} + +void MAX7314::vSetConfiguration(unsigned char Dir) { + + Bus.start(); + Bus.write(_Address & 0xFE); + Bus.write(0x03); + Bus.write(Dir); + Bus.stop(); +} + +void MAX7314::vSetPolarity(unsigned char Pol) { + + Bus.start(); + Bus.write(_Address & 0xFE); + Bus.write(0x02); + Bus.write(Pol); + Bus.stop(); +} + +unsigned char MAX7314::cRead(void) { + unsigned char Temp; + + Bus.start(); + Bus.write(_Address & 0xFE); + Bus.write(0x00); + Bus.start(); + Bus.write(_Address | 0x01); + Temp=Bus.read(0); + Bus.stop(); + + return(Temp); +} + +void MAX7314::vWrite(unsigned char Data) { + + Bus.start(); + Bus.write(_Address & 0xFE); + Bus.write(0x01); + Bus.write(Data); + Bus.stop(); +} + +void MAX7314::vEnableSetInterrupt(void (*fptr)(void)) { + MAX7314_Event.fall(fptr); +} + +void MAX7314::vDisableInterrupt(void) { + MAX7314_Event.fall(NULL); +} + +bool MAX7314::bReadPinINT(void) { + return(_PIN_INT); +}
diff -r 000000000000 -r 7e32b01354d3 MAX7314.hpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MAX7314.hpp Wed Apr 13 23:17:22 2011 +0000 @@ -0,0 +1,100 @@ +/* + + MAX7314.hpp + + Library include file for the MAX7314 16-bit I/O Expander with PWM + + Tom Gambone + Chad Joppeck + + Based on code by mBed user: "Ale C.-" / Suky (PCA9538_Expander Library) + + 4/13/2011 - Initial porting + +*/ + +#include "mbed.h" + +enum ExpPinName { + exp_p0=0, + exp_p1, + exp_p2, + exp_p3, + exp_p4, + exp_p5, + exp_p6, + exp_p7, + exp_p8, + exp_p9, + exp_p10, + exp_p11, + exp_p12, + exp_p13, + exp_p14, + exp_p15, +}; + + +//***************************************************************************** + + +class MAX7314_OutputPin { + +public: + MAX7314_OutputPin(ExpPinName Pin,PinName PIN_SDA,PinName PIN_SCL,unsigned char Address); + void vWrite(int value); + int read(); +#ifdef MBED_OPERATORS + MAX7314_OutputPin& operator= (int value); + operator int(); +#endif +protected: + ExpPinName _Pin; + unsigned char _Address; + I2C Bus; +}; + + + +//***************************************************************************** + + + +class MAX7314_InputPin { + +public: + MAX7314_InputPin(ExpPinName Pin,PinName PIN_SDA,PinName PIN_SCL,unsigned char Address); + int read(); +#ifdef MBED_OPERATORS + operator int(); +#endif +protected: + ExpPinName _Pin; + unsigned char _Address; + I2C Bus; +}; + + + +//***************************************************************************** + + + +class MAX7314 { + +public: + MAX7314(PinName PIN_SDA,PinName PIN_SCL,unsigned char Address,PinName PIN_INT=NC); + void vInit(unsigned char Dir,void (*fptr)(void)); + void vSetConfiguration(unsigned char Dir); + void vSetPolarity(unsigned char Pol); + unsigned char cRead(void); + void vWrite(unsigned char Data); + void vEnableSetInterrupt(void (*fptr)(void)); + void vDisableInterrupt(void); + bool bReadPinINT(void); +protected: + I2C Bus; + InterruptIn MAX7314_Event; + DigitalIn _PIN_INT; + unsigned char _Address; +};
diff -r 000000000000 -r 7e32b01354d3 PCA9538.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PCA9538.h Wed Apr 13 23:17:22 2011 +0000 @@ -0,0 +1,299 @@ +/* + \file PCA9538.h + \version: 1.0 + + \brief Este fichero contiene class para control de PCA9538 creando pin digitales + de salida o entrada, o control por registro (8-bits). + + \web www.micros-designs.com.ar + \date 31/01/11 + + *- Version Log --------------------------------------------------------------* + * Fecha Autor Comentarios * + *----------------------------------------------------------------------------* + * 31/01/11 Suky Original * + *----------------------------------------------------------------------------*/ +/////////////////////////////////////////////////////////////////////////// +//// //// +//// //// +//// (C) Copyright 2011 www.micros-designs.com.ar //// +//// Este código puede ser usado, modificado y distribuido libremente //// +//// sin eliminar esta cabecera y sin garantÃa de ningún tipo. //// +//// //// +//// //// +/////////////////////////////////////////////////////////////////////////// + +/* EXAMPLE +#include "mbed.h" +#include "PCA9538.h" + +//PCA9538 MyExpand(p9,p10,0x70,p21); // sda,scl,address,Interrupts +DigitalOut myled(LED1); +DigitalOut myled2(LED2); +PCA9538PinOut myled3(exp_p4,p9,p10,0x70); // pin del PCA,sda,scl,address +PCA9538PinOut myled4(exp_p7,p9,p10,0x70); +PCA9538PinIn mypuls(exp_p0,p9,p10,0x70); +//void vISRExpand(void); + +int main() { + //MyExpand.vInit(0x0F,&vISRExpand); + //MyExpand.vWrite(0x00); + while(1) { + if(mypuls==0){ + myled2=1; + wait(0.3); + myled2=0; + wait(0.3); + }else{ + myled3=1; + myled4=1; + myled=myled3; + wait(0.5); + myled = 0; + myled3=0; + myled4=0; + wait(0.5); + } + } +} + + +//void vISRExpand(void){ +// +// myled2=!myled2; +// MyExpand.vWrite(MyExpand.cRead()<<4); +//} +*/ +#include "mbed.h" + +enum ExpPinName{ + exp_p0=0, + exp_p1, + exp_p2, + exp_p3, + exp_p4, + exp_p5, + exp_p6, + exp_p7, +}; + +class PCA9538PinOut{ + public: + PCA9538PinOut(ExpPinName Pin,PinName PIN_SDA,PinName PIN_SCL,unsigned char Address); + void vWrite(int value); + int read(); + #ifdef MBED_OPERATORS + PCA9538PinOut& operator= (int value); + operator int(); + #endif + protected: + ExpPinName _Pin; + unsigned char _Address; + I2C Bus; +}; + +PCA9538PinOut::PCA9538PinOut(ExpPinName Pin,PinName PIN_SDA,PinName PIN_SCL,unsigned char Address) + :Bus(PIN_SDA,PIN_SCL),_Pin(Pin),_Address(Address<<1){ + unsigned char Temp; + + Bus.start(); + Bus.write(_Address & 0xFE); + Bus.write(0x03); + Bus.start(); + Bus.write(_Address | 0x01); + Temp=Bus.read(0); + Bus.stop(); + + Bus.start(); + Bus.write(_Address & 0xFE); + Bus.write(0x03); + Bus.write((~(0x01<<_Pin))&Temp); + Bus.stop(); +} + +void PCA9538PinOut::vWrite(int value){ + unsigned char Temp; + + Bus.start(); + Bus.write(_Address & 0xFE); + Bus.write(0x01); + Bus.start(); + Bus.write(_Address | 0x01); + Temp=Bus.read(0); + Bus.stop(); + + Bus.start(); + Bus.write(_Address & 0xFE); + Bus.write(0x01); + if(value==0){ + Bus.write((~(0x01<<_Pin))&Temp); + }else{ + Bus.write((0x01<<_Pin)|Temp); + } + Bus.stop(); +} + +int PCA9538PinOut::read(){ + unsigned char Temp; + + Bus.start(); + Bus.write(_Address & 0xFE); + Bus.write(0x01); + Bus.start(); + Bus.write(_Address | 0x01); + Temp=Bus.read(0); + Bus.stop(); + + return((Temp>>_Pin)&0x01); +} + +PCA9538PinOut& PCA9538PinOut::operator= (int value){ + vWrite(value); +} + +PCA9538PinOut::operator int(){ + + return(read()); +} +//***************************************************************************** +class PCA9538PinIn{ + public: + PCA9538PinIn(ExpPinName Pin,PinName PIN_SDA,PinName PIN_SCL,unsigned char Address); + int read(); + #ifdef MBED_OPERATORS + operator int(); + #endif + protected: + ExpPinName _Pin; + unsigned char _Address; + I2C Bus; +}; + +PCA9538PinIn::PCA9538PinIn(ExpPinName Pin,PinName PIN_SDA,PinName PIN_SCL,unsigned char Address) + :Bus(PIN_SDA,PIN_SCL),_Pin(Pin),_Address(Address<<1){ + unsigned char Temp; + + Bus.start(); + Bus.write(_Address & 0xFE); + Bus.write(0x03); + Bus.start(); + Bus.write(_Address | 0x01); + Temp=Bus.read(0); + Bus.stop(); + + Bus.start(); + Bus.write(_Address & 0xFE); + Bus.write(0x03); + Bus.write((0x01<<_Pin)|Temp); + Bus.stop(); +} + +int PCA9538PinIn::read(){ + unsigned char Temp; + + Bus.start(); + Bus.write(_Address & 0xFE); + Bus.write(0x00); + Bus.start(); + Bus.write(_Address | 0x01); + Temp=Bus.read(0); + Bus.stop(); + + return((Temp>>_Pin)&0x01); +} + +PCA9538PinIn::operator int(){ + + return(read()); +} +//***************************************************************************** +class PCA9538{ + public: + PCA9538(PinName PIN_SDA,PinName PIN_SCL,unsigned char Address,PinName PIN_INT=NC); + void vInit(unsigned char Dir,void (*fptr)(void)); + void vSetConfiguration(unsigned char Dir); + void vSetPolarity(unsigned char Pol); + unsigned char cRead(void); + void vWrite(unsigned char Data); + void vEnableSetInterrupt(void (*fptr)(void)); + void vDisableInterrupt(void); + bool bReadPinINT(void); + protected: + I2C Bus; + InterruptIn PCA9538_Event; + DigitalIn _PIN_INT; + unsigned char _Address; +}; + +PCA9538::PCA9538(PinName PIN_SDA,PinName PIN_SCL,unsigned char Address,PinName PIN_INT) + : Bus(PIN_SDA,PIN_SCL),_PIN_INT(PIN_INT),PCA9538_Event(PIN_INT){ + _Address=Address<<1; +} + +void PCA9538::vInit(unsigned char Dir,void (*fptr)(void)){ + + //Bus.frequency(400000); + + Bus.start(); + Bus.write(_Address & 0xFE); + Bus.write(0x03); + Bus.write(Dir); + Bus.stop(); + + if (fptr!=NULL){ + PCA9538_Event.fall(fptr); + } +} + +void PCA9538::vSetConfiguration(unsigned char Dir){ + + Bus.start(); + Bus.write(_Address & 0xFE); + Bus.write(0x03); + Bus.write(Dir); + Bus.stop(); +} + +void PCA9538::vSetPolarity(unsigned char Pol){ + + Bus.start(); + Bus.write(_Address & 0xFE); + Bus.write(0x02); + Bus.write(Pol); + Bus.stop(); +} + +unsigned char PCA9538::cRead(void){ + unsigned char Temp; + + Bus.start(); + Bus.write(_Address & 0xFE); + Bus.write(0x00); + Bus.start(); + Bus.write(_Address | 0x01); + Temp=Bus.read(0); + Bus.stop(); + + return(Temp); +} + +void PCA9538::vWrite(unsigned char Data){ + + Bus.start(); + Bus.write(_Address & 0xFE); + Bus.write(0x01); + Bus.write(Data); + Bus.stop(); +} + +void PCA9538::vEnableSetInterrupt(void (*fptr)(void)){ + PCA9538_Event.fall(fptr); +} + +void PCA9538::vDisableInterrupt(void){ + PCA9538_Event.fall(NULL); +} + +bool PCA9538::bReadPinINT(void){ + return(_PIN_INT); +}
diff -r 000000000000 -r 7e32b01354d3 PCA9538_Expander.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PCA9538_Expander.lib Wed Apr 13 23:17:22 2011 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/Suky/code/PCA9538_Expander/#d0e0b38e5991
diff -r 000000000000 -r 7e32b01354d3 PCF8575.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PCF8575.cpp Wed Apr 13 23:17:22 2011 +0000 @@ -0,0 +1,42 @@ +/* mbed PCF8574 Library, for driving the I2C I/O Expander + * Copyright (c) 2008-2010, cstyles, sford (Originally PCF8574 lib) + * new created by Lerche + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "PCF8575.h" +#include "mbed.h" + +PCF8575::PCF8575(PinName sda, PinName scl, int address) + : _i2c(sda, scl) { + _address = address; +} + +int PCF8575::read() { + char foo[2]; + _i2c.read(_address, foo, 2); + return (foo[0] << 8) | foo[1]; +} + +void PCF8575::write(int data) { + char foo[2]; + foo[0]=data; + foo[1]=data>>8; + _i2c.write(_address, foo, 2); +}
diff -r 000000000000 -r 7e32b01354d3 PCF8575.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PCF8575.h Wed Apr 13 23:17:22 2011 +0000 @@ -0,0 +1,56 @@ +/* mbed PCF8574 Library, for driving the I2C I/O Expander + * Copyright (c) 2008-2010, cstyles, sford (Originally PCF8574 lib) + * new created by Lerche + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "mbed.h" + +#ifndef MBED_PCF8575_H +#define MBED_PCF8575_H + +/** Interface to the PCF8575 I2C 16 Bit IO expander */ +class PCF8575 { +public: + /** Create an instance of the PCF8575 connected to specfied I2C pins, with the specified address. + * + * @param sda The I2C data pin + * @param scl The I2C clock pin + * @param address The I2C address for this PCF8575 + */ + PCF8575(PinName sda, PinName scl, int address); + + /** Read the IO pin level + * + * @return The two bytes read + */ + int read(); + + /** Write to the IO pins + * + * @param data The 16 bits to write to the IO port + */ + void write(int data); + +private: + I2C _i2c; + int _address; +}; + +#endif
diff -r 000000000000 -r 7e32b01354d3 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Wed Apr 13 23:17:22 2011 +0000 @@ -0,0 +1,39 @@ +#include "mbed.h" +#include "MAX7314.h" + +//MAX7314 MyExpand(p9,p10,0x70,p21); // sda,scl,address,Interrupts +DigitalOut myled(LED1); +DigitalOut myled2(LED2); +MAX7314_PinOut myled3(exp_p4,p9,p10,0x70); // pin on the expander, mbed sda pin, mbed scl pin,address +MAX7314_PinOut myled4(exp_p7,p9,p10,0x70); +MAX7314_PinIn mypuls(exp_p0,p9,p10,0x70); +//void vISRExpand(void); + +int main() { + //MyExpand.vInit(0x0F,&vISRExpand); + //MyExpand.vWrite(0x00); + while(1) { + if(mypuls==0){ + myled2=1; + wait(0.3); + myled2=0; + wait(0.3); + }else{ + myled3=1; + myled4=1; + myled=myled3; + wait(0.5); + myled = 0; + myled3=0; + myled4=0; + wait(0.5); + } + } +} + + +//void vISRExpand(void){ +// +// myled2=!myled2; +// MyExpand.vWrite(MyExpand.cRead()<<4); +//}
diff -r 000000000000 -r 7e32b01354d3 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Wed Apr 13 23:17:22 2011 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/63bcd7ba4912