![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
IVSC Project
MCP4651.cpp@4:262764d24e4d, 2013-06-07 (annotated)
- Committer:
- kevinkent
- Date:
- Fri Jun 07 19:59:29 2013 +0000
- Revision:
- 4:262764d24e4d
- Parent:
- 1:82f2ef52759e
Version 1.2
; Added MSG[0] = 0x06 tow write one pot chain with a single message
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kevinkent | 1:82f2ef52759e | 1 | /* mbed MCP4651 DigiPot Driver Library |
kevinkent | 1:82f2ef52759e | 2 | * |
kevinkent | 1:82f2ef52759e | 3 | * Copyright (c) 2012, Kevin Kent |
kevinkent | 1:82f2ef52759e | 4 | * |
kevinkent | 1:82f2ef52759e | 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
kevinkent | 1:82f2ef52759e | 6 | * of this software and associated documentation files (the "Software"), to deal |
kevinkent | 1:82f2ef52759e | 7 | * in the Software without restriction, including without limitation the rights |
kevinkent | 1:82f2ef52759e | 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
kevinkent | 1:82f2ef52759e | 9 | * copies of the Software, and to permit persons to whom the Software is |
kevinkent | 1:82f2ef52759e | 10 | * furnished to do so, subject to the following conditions: |
kevinkent | 1:82f2ef52759e | 11 | * |
kevinkent | 1:82f2ef52759e | 12 | * The above copyright notice and this permission notice shall be included in |
kevinkent | 1:82f2ef52759e | 13 | * all copies or substantial portions of the Software. |
kevinkent | 1:82f2ef52759e | 14 | * |
kevinkent | 1:82f2ef52759e | 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
kevinkent | 1:82f2ef52759e | 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
kevinkent | 1:82f2ef52759e | 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
kevinkent | 1:82f2ef52759e | 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
kevinkent | 1:82f2ef52759e | 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
kevinkent | 1:82f2ef52759e | 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
kevinkent | 1:82f2ef52759e | 21 | * THE SOFTWARE. |
kevinkent | 1:82f2ef52759e | 22 | */ |
kevinkent | 1:82f2ef52759e | 23 | |
kevinkent | 1:82f2ef52759e | 24 | |
kevinkent | 1:82f2ef52759e | 25 | #include "MCP4651.h" |
kevinkent | 1:82f2ef52759e | 26 | #include "mbed.h" |
kevinkent | 1:82f2ef52759e | 27 | |
kevinkent | 1:82f2ef52759e | 28 | /* |
kevinkent | 1:82f2ef52759e | 29 | Constructor, pin names for I2C and the I2C offset address of the device |
kevinkent | 1:82f2ef52759e | 30 | */ |
kevinkent | 1:82f2ef52759e | 31 | MCP4651::MCP4651(PinName sda, PinName scl, int addr) |
kevinkent | 1:82f2ef52759e | 32 | : _i2c(sda, scl) { |
kevinkent | 1:82f2ef52759e | 33 | |
kevinkent | 1:82f2ef52759e | 34 | _i2c.frequency(1000000); |
kevinkent | 1:82f2ef52759e | 35 | |
kevinkent | 1:82f2ef52759e | 36 | // Full I2C address = 0101b + A2:A1:A0:R/W |
kevinkent | 1:82f2ef52759e | 37 | _addr = MCP4651_BASE + (addr << 1); |
kevinkent | 1:82f2ef52759e | 38 | |
kevinkent | 1:82f2ef52759e | 39 | } |
kevinkent | 1:82f2ef52759e | 40 | |
kevinkent | 1:82f2ef52759e | 41 | /* |
kevinkent | 1:82f2ef52759e | 42 | Sets the value of the selected wiper |
kevinkent | 1:82f2ef52759e | 43 | */ |
kevinkent | 1:82f2ef52759e | 44 | |
kevinkent | 1:82f2ef52759e | 45 | int MCP4651::SetValue (int wiper, int value) { |
kevinkent | 1:82f2ef52759e | 46 | int reg; |
kevinkent | 1:82f2ef52759e | 47 | if (wiper == 0) {reg = MCP4651_VOL_WIPER0;} |
kevinkent | 1:82f2ef52759e | 48 | if (wiper == 1) {reg = MCP4651_VOL_WIPER1 << 4;} |
kevinkent | 1:82f2ef52759e | 49 | |
kevinkent | 1:82f2ef52759e | 50 | _write(reg, value); |
kevinkent | 1:82f2ef52759e | 51 | |
kevinkent | 1:82f2ef52759e | 52 | return(0); |
kevinkent | 1:82f2ef52759e | 53 | } |
kevinkent | 1:82f2ef52759e | 54 | |
kevinkent | 1:82f2ef52759e | 55 | |
kevinkent | 1:82f2ef52759e | 56 | |
kevinkent | 1:82f2ef52759e | 57 | |
kevinkent | 1:82f2ef52759e | 58 | // private functions for low level IO |
kevinkent | 1:82f2ef52759e | 59 | |
kevinkent | 1:82f2ef52759e | 60 | void MCP4651::_write(int reg, int data) { |
kevinkent | 1:82f2ef52759e | 61 | char args[2]; |
kevinkent | 1:82f2ef52759e | 62 | args[0] = reg; |
kevinkent | 1:82f2ef52759e | 63 | args[1] = data; |
kevinkent | 1:82f2ef52759e | 64 | _i2c.write(_addr, args,2); |
kevinkent | 1:82f2ef52759e | 65 | } |
kevinkent | 1:82f2ef52759e | 66 | |
kevinkent | 1:82f2ef52759e | 67 | int MCP4651::_read(int reg) { |
kevinkent | 1:82f2ef52759e | 68 | char args[2]; |
kevinkent | 1:82f2ef52759e | 69 | args[0] = reg; |
kevinkent | 1:82f2ef52759e | 70 | _i2c.write(_addr, args, 1); |
kevinkent | 1:82f2ef52759e | 71 | _i2c.read(_addr, args, 1); |
kevinkent | 1:82f2ef52759e | 72 | return(args[0]); |
kevinkent | 1:82f2ef52759e | 73 | } |
kevinkent | 1:82f2ef52759e | 74 | |
kevinkent | 1:82f2ef52759e | 75 | |
kevinkent | 1:82f2ef52759e | 76 | |
kevinkent | 1:82f2ef52759e | 77 | |
kevinkent | 1:82f2ef52759e | 78 |