forked from prphntm63/DS1844
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDS1844.cpp
78 lines (58 loc) · 1.82 KB
/
DS1844.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
75
76
77
/*
DS1844.h - Library for controlling a Maxim Integrated DS1844 Quad Digital Potentiometer
Created by Matt Westwick; March 23, 2013
Released into the public domain.
UPDATES
2020/07/29:1544> (UTC-5)
- changed "int" to "int16_t". int is a signed 4-byte value on Arduino
Due and SAMD boards, while int is a signed 2-byte value on ATmega-
based boards which could cause some inconsistent behavior across
different architectures. (editor: Orlando S. Hoilett).
*/
#include "Arduino.h"
#include "Wire.h"
#include "DS1844.h"
DS1844::DS1844(int16_t address)
{
Wire.begin();
this->address=address;
/*
Addresses for common configurations:
A0, A1, A2 low -> 0101000 -> 0x28
A0 high; A1, A2 low -> 0101001 -> 0x29
A0, A1 high; A2 low -> 0101011 -> 0x2B
A0, A1, A2 high -> 0101111 -> 0x2F
*/
}
void DS1844::write(int16_t pot, int16_t value) { //value is between 0 and 63
if (value > 63) { //prevents values that are too large from overwriting address bits
value = 63;
}
if (pot > 3) { //prevents mistransmission of address bits
pot = 3;
}
byte bvalue = byte(value);
byte bpot = byte(pot);
bpot = bpot << 6;
byte SendWriteVal = bpot + bvalue;
Wire.beginTransmission(this->address);
Wire.write(SendWriteVal); //0101-000-0 then 00-****** then 01-****** etc.
Wire.endTransmission();
}
int16_t DS1844::read(int16_t pot)
{
int16_t PotRead[4];
int16_t j = 0;
int16_t address = this->address;
Wire.requestFrom(address, 4);
while (Wire.available())
{
PotRead[j] = Wire.read();
j++;
}
//moving bits by 2 based on comment from Reddit user: AGeekNamedRoss
//https://www.reddit.com/r/arduino/comments/99dxe2/digital_potentiometer_ds1844_with_arduino/
//Another GitHub user also noticed this problem
//https://github.com/RossJacobs/DS1844
return (PotRead[pot] & (0x3F));
}