MCP4822 dual 12-bit Digital to Analog Converter (DAC) chip.
The MCP4822 is a dual 12-bit Digital to Analog Converter that is controlled via an SPI interface. It is available in PDIP, SOIC or MSOP packages. The documentation for the chip is available at Microchip's MC4822 page.
The range also includes 8 or 10-bit DACs, which this library could easily be converted to.
MCP4822.h@2:e60995ceccbd, 2013-02-15 (annotated)
- Committer:
- NickRyder
- Date:
- Fri Feb 15 16:28:57 2013 +0000
- Revision:
- 2:e60995ceccbd
- Parent:
- 1:68b3a24fd2a4
- Child:
- 3:7484c0fe6f4d
Add functions to shut down the two channels separately.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
NickRyder | 0:74353da3eacd | 1 | #ifndef __MCP4822_H__ |
NickRyder | 0:74353da3eacd | 2 | #define __MCP4822_H__ |
NickRyder | 0:74353da3eacd | 3 | |
NickRyder | 0:74353da3eacd | 4 | #include "mbed.h" |
NickRyder | 0:74353da3eacd | 5 | |
NickRyder | 1:68b3a24fd2a4 | 6 | /** MCP4822 dual 12-bit DAC |
NickRyder | 1:68b3a24fd2a4 | 7 | * @author NickRyder, atyeomans |
NickRyder | 2:e60995ceccbd | 8 | * @version 1.1 |
NickRyder | 1:68b3a24fd2a4 | 9 | * @section LICENSE |
NickRyder | 1:68b3a24fd2a4 | 10 | * The MIT License (MIT) |
NickRyder | 1:68b3a24fd2a4 | 11 | * Copyright (c) 2013 Nick Ryder, Andrew Yeomans |
NickRyder | 1:68b3a24fd2a4 | 12 | * |
NickRyder | 1:68b3a24fd2a4 | 13 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
NickRyder | 1:68b3a24fd2a4 | 14 | * of this software and associated documentation files (the "Software"), to deal |
NickRyder | 1:68b3a24fd2a4 | 15 | * in the Software without restriction, including without limitation the rights |
NickRyder | 1:68b3a24fd2a4 | 16 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
NickRyder | 1:68b3a24fd2a4 | 17 | * of the Software, and to permit persons to whom the Software is furnished to do so, |
NickRyder | 1:68b3a24fd2a4 | 18 | * subject to the following conditions: |
NickRyder | 1:68b3a24fd2a4 | 19 | * |
NickRyder | 1:68b3a24fd2a4 | 20 | * The above copyright notice and this permission notice shall be included in all |
NickRyder | 1:68b3a24fd2a4 | 21 | * copies or substantial portions of the Software. |
NickRyder | 1:68b3a24fd2a4 | 22 | * |
NickRyder | 1:68b3a24fd2a4 | 23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
NickRyder | 1:68b3a24fd2a4 | 24 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
NickRyder | 1:68b3a24fd2a4 | 25 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
NickRyder | 1:68b3a24fd2a4 | 26 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
NickRyder | 1:68b3a24fd2a4 | 27 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
NickRyder | 1:68b3a24fd2a4 | 28 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
NickRyder | 1:68b3a24fd2a4 | 29 | */ |
NickRyder | 1:68b3a24fd2a4 | 30 | |
NickRyder | 0:74353da3eacd | 31 | class MCP4822 { |
NickRyder | 0:74353da3eacd | 32 | public: |
NickRyder | 0:74353da3eacd | 33 | /** Create an MCP4822 instance |
NickRyder | 0:74353da3eacd | 34 | * @param dataout SPI MOSI pin (should be p5 or p11) |
NickRyder | 0:74353da3eacd | 35 | * @param clock SPI clock pin (should be p7 or p13) |
NickRyder | 0:74353da3eacd | 36 | * @param chipselect SPI chip select pin (can be any) |
NickRyder | 0:74353da3eacd | 37 | * @param latch Latch pin updates the DAC out. Can be any pin or NC, if NC then the latch pin on the DAC chip should be held to GND. |
NickRyder | 0:74353da3eacd | 38 | */ |
NickRyder | 0:74353da3eacd | 39 | MCP4822(PinName dataout, PinName clock, PinName chipselect, PinName latch); |
NickRyder | 0:74353da3eacd | 40 | //! Set output A to voltage |
NickRyder | 0:74353da3eacd | 41 | void setA(float voltage); |
NickRyder | 0:74353da3eacd | 42 | //! Set output B to voltage |
NickRyder | 0:74353da3eacd | 43 | void setB(float voltage); |
NickRyder | 0:74353da3eacd | 44 | //! Set both output A to voltageA and outputB to voltageB |
NickRyder | 0:74353da3eacd | 45 | void set(float voltageA, float voltageB); |
NickRyder | 0:74353da3eacd | 46 | //! Latch the stored values to the output pins |
NickRyder | 0:74353da3eacd | 47 | void latch(); |
NickRyder | 0:74353da3eacd | 48 | //! shut down output from the DAC chip |
NickRyder | 0:74353da3eacd | 49 | void shutdown(); |
NickRyder | 2:e60995ceccbd | 50 | //! shut down output form channel A |
NickRyder | 2:e60995ceccbd | 51 | void shutdownA(); |
NickRyder | 2:e60995ceccbd | 52 | //! shut down output form channel B |
NickRyder | 2:e60995ceccbd | 53 | void shutdownB(); |
NickRyder | 0:74353da3eacd | 54 | private: |
NickRyder | 0:74353da3eacd | 55 | DigitalOut cs, latchpin; |
NickRyder | 0:74353da3eacd | 56 | SPI spi; |
NickRyder | 0:74353da3eacd | 57 | void write(bool chanB, bool gain1, unsigned int voltage, bool shutdown = false); |
NickRyder | 0:74353da3eacd | 58 | void setvoltage(float voltage, bool chanB = false); |
NickRyder | 0:74353da3eacd | 59 | }; |
NickRyder | 0:74353da3eacd | 60 | |
NickRyder | 0:74353da3eacd | 61 | |
NickRyder | 0:74353da3eacd | 62 | #endif |