forked from hwstar/MCP4726
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MCP4726.cpp
74 lines (60 loc) · 2.05 KB
/
MCP4726.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**************************************************************************/
/*!
@file MCP4726.cpp
@author S. Rodgers
@license BSD (see license.txt)
Based on original work from Adafruit Industries.
Modified for MCP4706 by Pio Baettig
Modified for MCP4726 by S. Rodgers
I2C Driver for Microchip's MCP4726 I2C DAC
This is a library for the MCP4726 12-bit DAC modified from:
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
@section HISTORY
v1.0 - First release
*/
/**************************************************************************/
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include <Wire.h>
#include "MCP4726.h"
/**************************************************************************/
/*!
@brief Instantiates a new MCP4726 class
*/
/**************************************************************************/
MCP4726::MCP4726() {
}
/**************************************************************************/
/*!
@brief Setups the HW
*/
/**************************************************************************/
void MCP4726::begin(uint8_t addr) {
_i2caddr = addr;
Wire.begin();
}
/**************************************************************************/
/*!
@brief Sets the output voltage to a fraction of source vref. (Value
can be 0..4096)
@param[in] output
The 12-bit value representing the relationship between
the DAC's input voltage and its output voltage.
*/
/**************************************************************************/
void MCP4726::setVoltage( uint16_t output)
{
uint8_t twbrback = TWBR;
TWBR = 12; // 400 khz
// TWBR = 72; // 100 khz
Wire.beginTransmission(_i2caddr);
Wire.write((uint8_t) ((output >> 8) & 0x0F)); // MSB: (D11, D10, D9, D8)
Wire.write((uint8_t) (output)); // LSB: (D7, D6, D5, D4, D3, D2, D1, D0)
Wire.endTransmission();
TWBR = twbrback;
}