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