-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathMCP4261.h
96 lines (71 loc) · 2.93 KB
/
MCP4261.h
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
92
93
// MCP4261 2-channel Digital Potentiometer
// ww1.microchip.com/downloads/en/DeviceDoc/22059b.pdf
#ifndef MCP4261_h
#define MCP4261_h
class MCP4261
{
public:
// You must at least specify the slave select pin and the rated resistance
MCP4261(uint8_t slave_select, float rAB_ohms);
// If you have measured wiper resistance, rW
MCP4261(uint8_t slave_select, float rAB_ohms, float rW_ohms);
// The resistance scaling, defaults to rAB_ohms
float scale;
// Read potentiometer values
float wiper0();
float wiper1();
unsigned int wiper0_pos();
unsigned int wiper1_pos();
// Write potentiometer values
void wiper0(float ohms);
void wiper1(float ohms);
void wiper0_pos(unsigned int wiper_pos);
void wiper1_pos(unsigned int wiper_pos);
// // Not implemented
// // Connect / disconnect potentiometers
// bool pot0_connected(bool terminal_a, bool wiper, bool terminal_b);
// bool pot1_connected(bool terminal_a, bool wiper, bool terminal_b);
// void pot0_connect(bool terminal_a, bool wiper, bool terminal_b);
// void pot1_connect(bool terminal_a, bool wiper, bool terminal_b);
//
// bool pot0_shutdown();
// bool pot1_shutdown();
// void pot0_shutdown(bool shutdown);
// void pot1_shutdown(bool shutdown);
//
// bool hw_shutdown();
protected:
const float rW_ohms_typical = 117.50;
const static unsigned int resolution_7bit = 128;
const static unsigned int resolution_8bit = 256;
// Other devices can be configured below vv as per the device numbering scheme:
// MCP4N-- N=1 single pot, N=2 dual pot
// MCP4--N N=1 potentiometer, N=2 rheostat
// MCP4-N- N=3 7-bit volatile, N=4 7-bit non-volatile, N=5 8-bit volatile, N=6 8-bit non-volatile
const static bool non_volatile = true;
const static unsigned int resolution = resolution_8bit;
float rW_ohms;
float rAB_ohms;
float rAW_ohms_max;
uint8_t slave_select_pin;
const static uint8_t kADR_WIPER0 = B00000000;
const static uint8_t kADR_WIPER1 = B00010000;
const static uint8_t kCMD_READ = B00001100;
const static uint8_t kCMD_WRITE = B00000000;
const static uint8_t kADR_VOLATILE = B00000000;
const static uint8_t kADR_NON_VOLATILE = B00100000;
const static uint8_t kTCON_REGISTER = B01000000;
const static uint8_t kSTATUS_REGISTER = B01010000;
uint16_t byte2uint16(byte high_byte, byte low_byte);
byte uint16_high_byte(uint16_t uint16);
byte uint16_low_byte(uint16_t uint16);
void setup_ss(uint8_t slave_select_pin);
void setup_resistance(float rAB_ohms, float rW_ohms);
float step_increment();
unsigned int ohms2wiper_pos(float ohms);
float wiper_pos2ohms(unsigned int wiper_pos);
uint16_t read(byte cmd_byte);
void write(byte cmd_byte, byte data_byte);
void wiper_pos(byte pot, unsigned int wiper_pos);
};
#endif // Mcp4261_h