12-Bit DAC with internal Vref and SPI interface
Embed:
(wiki syntax)
Show/hide line numbers
MCP4822.cpp
00001 /* mbed MCP4822 Library, for driving the 12-Bit DAC with internal Vref and SPI interface 00002 * Copyright (c) 2015, Created by Steen Joergensen (stjo2809) 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 //============================================================================= 00027 // Public functions 00028 //============================================================================= 00029 00030 MCP4822::MCP4822(SPI& spi, PinName nLDAC, PinName nCs) : _spi(spi), _nLDAC(nLDAC), _nCs(nCs) 00031 { 00032 00033 } 00034 00035 00036 MCP4822::MCP4822(PinName nLDAC, PinName mosi, PinName miso,PinName sck, PinName nCs) : _nLDAC(nLDAC), _mosi(mosi), _miso(miso), _sck(sck), _nCs(nCs) 00037 { 00038 SPI _spi(_mosi,_miso,_sck); 00039 } 00040 00041 void MCP4822::a(bool gain, int data) 00042 { 00043 _write(0, gain, 1, data); 00044 } 00045 00046 void MCP4822::b(bool gain, int data) 00047 { 00048 _write(1, gain, 1, data); 00049 } 00050 00051 void MCP4822::ldac(bool act) 00052 { 00053 _nLDAC = ~act; 00054 } 00055 00056 void MCP4822::shdn(bool output, bool act) 00057 { 00058 _write(output, 1, ~act, 0x00); 00059 } 00060 00061 //============================================================================= 00062 // Private functions 00063 //============================================================================= 00064 00065 char MCP4822::_make_upper_half(bool output, bool gain, bool shdn, int data) 00066 { 00067 char responce = 0; 00068 00069 if(output == '0') 00070 { 00071 responce = responce | CB_OUTPUT_A; 00072 } 00073 else 00074 { 00075 responce = responce | CB_OUTPUT_B; 00076 } 00077 00078 if(gain == '0') 00079 { 00080 responce = responce | CB_GAIN_2X; 00081 } 00082 else 00083 { 00084 responce = responce | CB_GAIN_1X; 00085 } 00086 00087 if(shdn == '0') 00088 { 00089 responce = responce | CB_NSHDN; 00090 } 00091 else 00092 { 00093 responce = responce | CB_SHDN; 00094 } 00095 00096 if(data > 0xff && data < 0xfff) 00097 { 00098 responce = responce | (data >> 8); 00099 } 00100 00101 return responce; 00102 } 00103 00104 char MCP4822::_make_lower_half(int data) 00105 { 00106 return (data & 0xff); 00107 } 00108 00109 void MCP4822::_write(bool output, bool gain, bool shdn, int data) 00110 { 00111 00112 _upper_half = _make_upper_half(output, gain, shdn, data); 00113 _lower_half = _make_lower_half(data); 00114 00115 _nCs = 0; 00116 _spi.write(_upper_half); 00117 _spi.write(_lower_half); 00118 _nCs = 1; 00119 }
Generated on Sat Jul 16 2022 14:19:40 by 1.7.2