Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 0:960d250e49b2, committed 2012-05-17
- Comitter:
- kevinkent
- Date:
- Thu May 17 23:11:08 2012 +0000
- Child:
- 1:82f2ef52759e
- Commit message:
- got mcp working
;
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/MCP4661.cpp Thu May 17 23:11:08 2012 +0000
@@ -0,0 +1,126 @@
+/* mbed MCP4661 DigiPot Driver Library
+ *
+ * Copyright (c) 2012, Kevin Kent
+ *
+ * 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 "MCP4661.h"
+#include "mbed.h"
+
+/*
+ Constructor, pin names for I2C and the I2C address of the device
+ */
+MCP4661::MCP4661(PinName sda, PinName scl, int addr)
+ : _i2c(sda, scl) {
+
+ _i2c.frequency(1000000);
+
+ _addr = addr;
+
+}
+
+
+int MCP4661::SetValue (int wiper, int value) {
+ int reg;
+ if (wiper == 0) {reg = MCP4661_VOL_WIPER0;}
+ if (wiper == 1) {
+ reg = MCP4661_VOL_WIPER1;
+ reg = (reg << 4 );
+ }
+ reg |= MCP4661_WR;
+ _write(reg, value);
+ return(0);
+}
+int MCP4661::SetMode (int mask, int mode) {
+ if ( (mode < 0) || (mode > 3) ) {
+ return(1);
+ } else {
+ for (int i=0 ; i < 16 ; i++ ) {
+
+ // if this matches, set the LED to the mode
+ if (mask & (0x1 << i)) {
+ SetLed(i,mode);
+ }
+ }
+ }
+ return(0);
+}
+
+
+
+/*
+ led is in the range 0-15
+ mode is inthe range 0-3
+ */
+int MCP4661::SetLed(int led, int mode) {
+
+ int reg = 0;
+ int offset = (led % 4);
+
+ printf("\nSetLed(%d,%d)\n", led, mode);
+
+ // makesure mode is within bounds
+ if ( (mode < 0) || (mode > 3) ) {
+ printf("Error : Invalid mode supplied\n");
+ return(1);
+ }
+
+ // determine which register this is,
+
+
+ // read the current status of the register
+ char regval = _read(reg);
+
+ // clear the two bit slice at the calculated offset
+ regval &= ~(0x3 << (2 * offset));
+
+ // now OR in the mode, shifted by 2*offset
+ regval |= (mode << (2 * offset));
+
+ // write the new value back
+ _write(reg, regval);
+
+ return(0);
+}
+
+
+
+// private functions for low level IO
+
+void MCP4661::_write(int reg, int data) {
+ char args[2];
+ args[0] = reg;
+ args[1] = data;
+ _i2c.write(_addr, args,2);
+}
+
+int MCP4661::_read(int reg) {
+ char args[2];
+ args[0] = reg;
+ _i2c.write(_addr, args, 1);
+ _i2c.read(_addr, args, 1);
+ return(args[0]);
+}
+
+
+
+
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/MCP4661.h Thu May 17 23:11:08 2012 +0000
@@ -0,0 +1,69 @@
+/* mbed MCP4661 DigiPot Driver Library
+ *
+ * Copyright (c) 2012, Kevin Kent Trane US Inc
+ *
+ * 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.
+ */
+
+#ifndef MCP4661_H
+#define MCP4661_H
+
+#include "mbed.h"
+
+// register names
+#define MCP4661_VOL_WIPER0 0
+#define MCP4661_VOL_WIPER1 1
+#define MCP4661_NV_WIPER0 2
+#define MCP4661_NV_WIPER1 3
+#define MCP4661_REG_TCON 4
+#define MCP4661_REG_STATUS 5
+#define MCP4661_EEPROM_0 6
+// #define MCP4661_EEPROM1 7
+// The Data EEPROM is 0x6 to 0xf
+
+
+// Command modes
+#define MCP4661_WR 0
+#define MODE_ON 1
+#define MODE_PWM0 2
+#define MODE_PWM1 3
+
+class MCP4661 {
+
+public:
+
+ MCP4661(PinName sda, PinName scl, int addr);
+
+ int SetLed (int led, int mode);
+ int SetMode (int mask, int mode);
+ int Duty (int channel, float duty);
+ int Period (int channel, float period);
+ int SetValue (int wiper, int value);
+
+protected:
+
+ void _write(int reg, int data);
+ int _read(int reg);
+ int _addr;
+ I2C _i2c;
+
+};
+
+
+#endif
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/PCA9532.cpp Thu May 17 23:11:08 2012 +0000
@@ -0,0 +1,191 @@
+/* mbed PCF9532 LED Driver Library
+ *
+ * Copyright (c) 2010, cstyles (http://mbed.org)
+ *
+ * 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 "PCA9532.h"
+#include "mbed.h"
+
+/*
+ Constructor, pin names for I2C and the I2C addrss of the device
+ */
+PCA9532::PCA9532(PinName scl, PinName sda, int addr)
+ : _i2c(scl, sda) {
+
+ _i2c.frequency(1000000);
+
+ _addr = addr;
+
+}
+
+
+
+
+
+/*
+ Set the period
+ */
+int PCA9532::Period (int channel, float period) {
+
+ char reg = 0;
+
+ if (channel == 0) {
+ reg = PCA9532_REG_PSC0;
+ } else if (channel == 1) {
+ reg = PCA9532_REG_PSC1;
+ } else {
+ return (1);
+ }
+
+ if (period > 1.0) {
+ period = 255;
+ } else if ( period < 0.0 ) {
+ period = 0;
+ } else {
+ period = 256 * period;
+ }
+
+ _write(reg, period);
+ return(0);
+
+}
+
+
+/*
+ Set the duty cycle
+ */
+int PCA9532::Duty (int channel, float d) {
+
+ char duty = 0;
+ char reg = 0;
+
+ if (channel == 0) {
+ reg = PCA9532_REG_PWM0;
+ } else if (channel == 1) {
+ reg = PCA9532_REG_PWM1;
+ } else {
+ return (1);
+ }
+
+ if (d > 1.0) {
+ duty = 255;
+ } else if ( d < 0.0 ) {
+ duty = 0;
+ } else {
+ duty = 256 * d;
+ }
+
+ _write(reg, duty);
+ return(0);
+
+}
+
+/*
+ Set each of the LEDs in this mask to the give mode
+ Loop through the mask calling SetLed on each match
+ alt_mode specifies the mode of the non-Matches of SetMode
+ */
+int PCA9532::SetMode (int mask, int mode) {
+ if ( (mode < 0) || (mode > 3) ) {
+ return(1);
+ } else {
+ for (int i=0 ; i < 16 ; i++ ) {
+
+ // if this matches, set the LED to the mode
+ if (mask & (0x1 << i)) {
+ SetLed(i,mode);
+ }
+ }
+ }
+ return(0);
+}
+
+
+
+/*
+ led is in the range 0-15
+ mode is inthe range 0-3
+ */
+int PCA9532::SetLed(int led, int mode) {
+
+ int reg = 0;
+ int offset = (led % 4);
+
+ printf("\nSetLed(%d,%d)\n", led, mode);
+
+ // makesure mode is within bounds
+ if ( (mode < 0) || (mode > 3) ) {
+ printf("Error : Invalid mode supplied\n");
+ return(1);
+ }
+
+ // determine which register this is,
+ if (led < 4) {
+ reg = PCA9532_REG_LS0;
+ } else if ( (led > 3) && (led < 8) ) {
+ reg = PCA9532_REG_LS1;
+ } else if ( (led > 7) && (led < 12) ) {
+ reg = PCA9532_REG_LS2;
+ } else if ( (led > 11) && (led < 16) ) {
+ reg = PCA9532_REG_LS3;
+ } else {
+ return(1);
+ }
+
+ // read the current status of the register
+ char regval = _read(reg);
+
+ // clear the two bit slice at the calculated offset
+ regval &= ~(0x3 << (2 * offset));
+
+ // now OR in the mode, shifted by 2*offset
+ regval |= (mode << (2 * offset));
+
+ // write the new value back
+ _write(reg, regval);
+
+ return(0);
+}
+
+
+
+// private functions for low level IO
+
+void PCA9532::_write(int reg, int data) {
+ char args[2];
+ args[0] = reg;
+ args[1] = data;
+ _i2c.write(_addr, args,2);
+}
+
+int PCA9532::_read(int reg) {
+ char args[2];
+ args[0] = reg;
+ _i2c.write(_addr, args, 1);
+ _i2c.read(_addr, args, 1);
+ return(args[0]);
+}
+
+
+
+
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/PCA9532.h Thu May 17 23:11:08 2012 +0000
@@ -0,0 +1,68 @@
+/* mbed PCF9532 LED Driver Library
+ *
+ * Copyright (c) 2010, cstyles (http://mbed.org)
+ *
+ * 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.
+ */
+
+#ifndef PCA9532_H
+#define PCA9532_H
+
+#include "mbed.h"
+
+// register names
+#define PCA9532_REG_INPUT0 0
+#define PCA9532_REG_INPUT1 1
+#define PCA9532_REG_PSC0 2
+#define PCA9532_REG_PWM0 3
+#define PCA9532_REG_PSC1 4
+#define PCA9532_REG_PWM1 5
+#define PCA9532_REG_LS0 6
+#define PCA9532_REG_LS1 7
+#define PCA9532_REG_LS2 8
+#define PCA9532_REG_LS3 9
+
+// LED modes
+#define MODE_OFF 0
+#define MODE_ON 1
+#define MODE_PWM0 2
+#define MODE_PWM1 3
+
+class PCA9532 {
+
+public:
+
+ PCA9532(PinName sda, PinName scl, int addr);
+
+ int SetLed (int led, int mode);
+ int SetMode (int mask, int mode);
+ int Duty (int channel, float duty);
+ int Period (int channel, float period);
+
+protected:
+
+ void _write(int reg, int data);
+ int _read(int reg);
+ int _addr;
+ I2C _i2c;
+
+};
+
+
+#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Thu May 17 23:11:08 2012 +0000
@@ -0,0 +1,27 @@
+#include "mbed.h"
+#include "MCP4661.h"
+
+DigitalOut myled(LED1);
+DigitalOut res(LED2);
+MCP4661 pot1(p28, p27, 0x5c);
+Serial pc(USBTX,USBRX);
+
+
+int main() {
+ int mid = 0x1;
+ int address = 0x5c;
+ pc.printf("Hello Mbed World\n");
+ while(1) {
+ myled = 1;
+ wait(0.2);
+ myled = 0;
+ res = pot1.SetValue(0,mid);
+ pc.printf("Val = %d",mid);
+ //data[0] = 0x04;
+ //res = i2c.write(address, data, 1);
+ mid++;
+ wait(1.0);
+ res= 0;
+ wait(1.0);
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.lib Thu May 17 23:11:08 2012 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/projects/libraries/svn/mbed/trunk@43 \ No newline at end of file