-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathMCP492X.h
More file actions
91 lines (80 loc) · 2.71 KB
/
MCP492X.h
File metadata and controls
91 lines (80 loc) · 2.71 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* MCP4921/MCP4922 12 bit DAC Library
* By MichD
*
* GitHub: https://github.com/michd/Arduino-MCP492X
*
* The Microchip MCP4921 and MCP4922 are 12 bit digital to analog converters
* with an SPI interface. The MCP4921 has one DAC output, the MCP4922 has two.
*
* This library is built around the Arduino default SPI library and uses the
* designated SPI pins, in addition to a configurable chip select pin, passed
* in the constructor.
* Look up the designated pins for your arduino board, and wire up as follows:
*
* | Arduino | MCP492X | MCP4921 pin | MCP4922 pin |
* |---------|-----------|-------------|-------------|
* | SCK | SCK | 3 | 4 |
* | MOSI | SDI | 4 | 5 |
* | your CS | CS | 2 | 3 |
*
* Where "your CS" is whichever pin you'd like to use as chip select on
* your arduino board.
*/
#include <Arduino.h>
#include <SPI.h>
// Ensure we don't double-define the functionality
#ifndef MCP492X_h
#define MCP492X_h
class MCP492X {
public:
// Constructor, takes the chip select pin
// Use outside any functions:
// `MCP492X myDac(pinNumber);`
MCP492X(uint8_t);
// Initilize, starts the SPI bus. Call in setup()
// Example:
// ```
// void setup() {
// myDac.begin();
// }
//
void begin();
// Writes a 12 bit value to the output.
// If on the MCP4922, defaults to DAC output 0 (A).
// Example:
// ```
// myDac.analogWrite(1234);
// ```
void analogWrite(unsigned int); // Write a 12 bit value
// Writes a 12 bit value to a given DAC output (0 or 1 / A or B)
// Param 1 = DAC selection
// Param 2 = 12 bit value
// Example:
// ```
// myDac.analogWrite(1, 4095);
// ```
void analogWrite(bool, unsigned int); // Write a 12 bit value to a specific output (only MCP4922)
// Writes a 12 bit value to a given DAC output (0 or 1 / A or B),
// and allows setting every config bit individually.
// Param 1 = DAC selection
// Param 2 = buffer input
// Param 2 = gain mode (1 = 1x, 0 = 2x)
// Param 3 = shut down output (1 = active, 0 = shutdown)
// Param 4 = 12 bit value
// Example:
// ```
// myDac.analogWrite(1, 0, 1, 1, 2480);
// ```
void analogWrite(bool, bool, bool, bool, unsigned int); // Full control over control bits
private:
// Internal fields/methods you should not need to worry about.
// Holds onto the chip select pin number
uint8_t _pinChipSelect;
// SPI settings for this chip, set up in begin()
SPISettings _spiSettings;
// Internal helpers to start/end transmission
void _beginTransmission();
void _endTransmission();
};
#endif // MCP921X_h