Library for MCP4822 SPI 12-bit DAC, both outputs supported, nLDAC connected to GND.

Dependents:   MCP4822_2aLerche

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MCP4822.cpp Source File

MCP4822.cpp

00001 /* MCP4822 library 
00002  * Copyright (c) 2008-2010, Lerche
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy
00005  * of this software and associated documentation files (the "Software"), to deal
00006  * in the Software without restriction, including without limitation the rights
00007  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008  * copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020  * THE SOFTWARE.
00021  */
00022 
00023 #include "mbed.h"
00024 #include "MCP4822.h"
00025 
00026 using namespace mbed;
00027 
00028 MCP4822::MCP4822(PinName mosi, PinName sclk, PinName ncs) : _spi(mosi, NC, sclk), _ncs(ncs) {
00029     _init();
00030 }
00031 
00032 void MCP4822::_init() {
00033     _spi.format(16, 0);  // Setup the SPI
00034     return;
00035 }
00036 
00037 void MCP4822::writeA(int ValueA) {
00038     ValueA = ValueA &= 0x0FFF;
00039     ValueA = ValueA |= 0x1000;      // Write to A register  
00040     _ncs = 0;       // Chipselect the device.
00041     _spi.write(ValueA);
00042     _ncs = 1;
00043     return;
00044 }
00045 
00046 void MCP4822::writeB(int ValueB) {
00047     ValueB = ValueB &= 0x0FFF;
00048     ValueB = ValueB |= 0x9000;      // Write to B register
00049     _ncs = 0;       // Chipselect the device.
00050     _spi.write(ValueB);
00051     _ncs = 1;
00052     return;
00053 }
00054 
00055 void MCP4822::write(char chan, int value) {
00056     value = value &= 0x0FFF;
00057     switch(chan){
00058         case('A'):
00059             value = value |= 0x1000;
00060             break; 
00061         case('B'):
00062             value = value |= 0x9000;
00063             break;
00064         case('a'):
00065             value = value |= 0x1000;
00066             break; 
00067         case('b'):
00068             value = value |= 0x9000;
00069             break;
00070         case(0x01):
00071             value = value |= 0x1000;
00072             break; 
00073         case(0x02):
00074             value = value |= 0x9000;
00075             break;
00076         default: 
00077             break;        
00078     }
00079     _ncs = 0;
00080     _spi.write(value);
00081     _ncs = 1;
00082     return;
00083 }
00084 
00085 void MCP4822::shdn() {
00086     _ncs = 0;
00087     _spi.write(0x0000);
00088     _ncs = 1;
00089     return;
00090 }