├── LICENSE.md ├── README.md ├── examples └── TriangleWave │ └── TriangleWave.ino ├── library.properties ├── src ├── MCPDAC.cpp └── MCPDAC.h └── uecide.json /LICENSE.md: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015, Majenko Technologies 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, 6 | * are permitted provided that the following conditions are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * * Redistributions in binary form must reproduce the above copyright notice, this 12 | * list of conditions and the following disclaimer in the documentation and/or 13 | * other materials provided with the distribution. 14 | * 15 | * * Neither the name of Majenko Technologies nor the names of its 16 | * contributors may be used to endorse or promote products derived from 17 | * this software without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 21 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 22 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 23 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 25 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 26 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 28 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | */ 30 | 31 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | MCPDAC 2 | ====== 3 | 4 | Library to control the MCP4822 and similar DAC chips 5 | -------------------------------------------------------------------------------- /examples/TriangleWave/TriangleWave.ino: -------------------------------------------------------------------------------- 1 | // MCPDAC relies on SPI. 2 | #include 3 | #include 4 | 5 | void setup() 6 | { 7 | // CS on pin 10, no LDAC pin (tie it to ground). 8 | MCPDAC.begin(10); 9 | 10 | // Set the gain to "HIGH" mode - 0 to 4096mV. 11 | MCPDAC.setGain(CHANNEL_A,GAIN_HIGH); 12 | 13 | // Do not shut down channel A, but shut down channel B. 14 | MCPDAC.shutdown(CHANNEL_A,false); 15 | MCPDAC.shutdown(CHANNEL_B,true); 16 | } 17 | 18 | void loop() 19 | { 20 | static unsigned int volts = 0; 21 | 22 | // Set the voltage of channel A. 23 | MCPDAC.setVoltage(CHANNEL_A,volts&0x0fff); 24 | 25 | // Increase the voltage in steps of 100mV. 26 | volts+=100; 27 | } 28 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=MCPDAC 2 | version=1.1.8 3 | author=Matt Jenkins 4 | maintainer=Matt Jenkins 5 | sentence=MCP4822 (and similar) DAC library 6 | paragraph=MCPDAC\n======\n\nLibrary to control the MCP4822 and similar DAC chips\n 7 | url=https://github.com/MajenkoLibraries/MCPDAC 8 | category=Signal Input/Output 9 | architectures=* 10 | includes=MCPDAC.h 11 | -------------------------------------------------------------------------------- /src/MCPDAC.cpp: -------------------------------------------------------------------------------- 1 | /*********************************************** 2 | * Microchip DAC library 3 | * (c) 2012 Majenko Technologies 4 | * 5 | * This library is offered without any warranty as to 6 | * fitness for purpose, either explicitly or implicitly 7 | * implied. 8 | ***********************************************/ 9 | 10 | /* 11 | * Microchip DAC library. This works with the MCP4822 chip and 12 | * similar chips that work with the same protocol. 13 | */ 14 | 15 | #include 16 | #include 17 | #include "MCPDAC.h" 18 | 19 | MCPDACClass MCPDAC; 20 | 21 | void MCPDACClass::begin() 22 | { 23 | this->begin(10); 24 | } 25 | 26 | void MCPDACClass::begin(uint8_t cspin) 27 | { 28 | this->ldac = false; 29 | this->cspin = cspin; 30 | pinMode(this->cspin,OUTPUT); 31 | digitalWrite(this->cspin,HIGH); 32 | SPI.begin(); 33 | } 34 | 35 | void MCPDACClass::begin(uint8_t cspin, uint8_t ldacpin) 36 | { 37 | this->begin(cspin); 38 | this->ldac = true; 39 | this->ldacpin = ldacpin; 40 | pinMode(this->ldacpin,OUTPUT); 41 | digitalWrite(this->ldacpin,HIGH); 42 | } 43 | 44 | void MCPDACClass::setGain(bool chan, bool gain) 45 | { 46 | this->gain[chan] = gain; 47 | } 48 | 49 | void MCPDACClass::shutdown(bool chan, bool sd) 50 | { 51 | this->shdn[chan] = sd; 52 | } 53 | 54 | void MCPDACClass::setVoltage(bool chan, uint16_t mv) 55 | { 56 | this->value[chan] = mv; 57 | this->updateRegister(chan); 58 | } 59 | 60 | void MCPDACClass::update() 61 | { 62 | if(this->ldac==false) 63 | return; 64 | digitalWrite(this->ldacpin,LOW); 65 | digitalWrite(this->ldacpin,HIGH); 66 | } 67 | 68 | void MCPDACClass::updateRegister(bool chan) 69 | { 70 | uint16_t command = 0; 71 | command |= (chan << REGAB); // set channel in register 72 | command |= (!this->gain[chan] << REGGA); // set gain in register 73 | command |= (!this->shdn[chan] << REGSHDN); // set shutdown in register 74 | command |= (this->value[chan] & 0x0FFF); // set input data bits (strip everything greater than 12 bit) 75 | 76 | SPI.setDataMode(SPI_MODE0); 77 | digitalWrite(this->cspin,LOW); 78 | SPI.transfer(command>>8); 79 | SPI.transfer(command&0xFF); 80 | digitalWrite(this->cspin,HIGH); 81 | } 82 | -------------------------------------------------------------------------------- /src/MCPDAC.h: -------------------------------------------------------------------------------- 1 | /*********************************************** 2 | * Microchip DAC library 3 | * (c) 2012 Majenko Technologies 4 | * 5 | * This library is offered without any warranty as to 6 | * fitness for purpose, either explicitly or implicitly 7 | * implied. 8 | ***********************************************/ 9 | 10 | /* 11 | * Microchip DAC library. This works with the MCP4822 chip and 12 | * similar chips that work with the same protocol. 13 | */ 14 | 15 | #define CHANNEL_A false 16 | #define CHANNEL_B true 17 | #define GAIN_LOW false 18 | #define GAIN_HIGH true 19 | 20 | #define REGAB 15 21 | #define REGGA 13 22 | #define REGSHDN 12 23 | 24 | class MCPDACClass { 25 | 26 | private: 27 | bool ldac = false; 28 | bool gain[2] = { GAIN_LOW, GAIN_LOW }; 29 | bool shdn[2] = { true, true }; 30 | uint16_t value[2] = { 0, 0 }; 31 | uint8_t cspin; 32 | uint8_t ldacpin = 0; 33 | 34 | public: 35 | void begin(); 36 | void begin(uint8_t cspin); 37 | void begin(uint8_t cspin, uint8_t ldacpin); 38 | void setGain(bool chan, bool gain); 39 | void shutdown(bool chan, bool sd); 40 | void setVoltage(bool chan, uint16_t mv); 41 | void update(); 42 | void updateRegister(bool chan); 43 | }; 44 | 45 | extern MCPDACClass MCPDAC; 46 | -------------------------------------------------------------------------------- /uecide.json: -------------------------------------------------------------------------------- 1 | {"name":"MCPDAC","provides":"MCPDAC.h","license":"LICENSE.md","readme":"README.md","description":"MCP4822 (and similar) DAC library","category":"IO","subcategory":"Analog","maintainer":"Matt Jenkins ","version":"1.1.8","family":"all","core":"all","install":{"examples":"\/","library.properties":"\/","src":"\/"}} --------------------------------------------------------------------------------