Library for MCP4822 SPI 12-bit DAC, both outputs supported, nLDAC connected to GND.
MCP4822.cpp
- Committer:
- Lerche
- Date:
- 2011-01-21
- Revision:
- 1:5c1e0d492b59
- Parent:
- 0:014a10a4fed1
- Child:
- 11:96eff565b2c0
File content as of revision 1:5c1e0d492b59:
/* MCP4822 library * Copyright (c) 2008-2010, 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" #include "MCP4822.h" using namespace mbed; MCP4822::MCP4822(PinName mosi, PinName sclk, PinName ncs) : _spi(mosi, NC, sclk), _ncs(ncs) { _init(); } void MCP4822::_init() { _spi.format(16, 0); // Setup the SPI return; } void MCP4822::writeA(int ValueA) { _ncs = 0; // Chipselect the device. ValueA = ValueA &= 0x0FFF; ValueA = ValueA |= 0x1000; // Write to A register _spi.write(ValueA); _ncs = 1; return; } void MCP4822::writeB(int ValueB) { _ncs = 0; // Chipselect the device. ValueB = ValueB &= 0x0FFF; ValueB = ValueB |= 0x9000; // Write to B register _spi.write(ValueB); _ncs = 1; return; }