forked from jknipper/mcp4728_program_address
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mcp4728_program_address.ino
68 lines (57 loc) · 1.59 KB
/
mcp4728_program_address.ino
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
#include <Arduino.h>
#include <SoftI2cMaster.h>
// Change to LDAC pin here
#define LDAC_PIN 2
// Define address here (0-7)
#define OLD_ADDRESS 0
#define NEW_ADDRESS 1
#ifdef CORE_TEENSY
#define SDA_PIN 18
#define SCL_PIN 19
#else // Arduino
#define SDA_PIN A4
#define SCL_PIN A5
#endif
SoftI2cMaster i2c;
void setup() {
Serial.begin(9600);
pinMode(LDAC_PIN, OUTPUT);
i2c.init(SCL_PIN, SDA_PIN);
delay(250);
writeAddress(OLD_ADDRESS, NEW_ADDRESS, LDAC_PIN);
delay(250);
uint8_t addr = readAddress(LDAC_PIN);
Serial.println("Address: ");
Serial.println(addr);
Serial.println("Done.");
}
void loop() {}
uint8_t readAddress(int LDACpin) {
digitalWrite(LDACpin, HIGH);
int ack1 = i2c.start(0B00000000);
int ack2 = i2c.ldacwrite(0B00001100, LDACpin); // modified command for LDAC pin latch
int ack3 = i2c.restart(0B11000001);
uint8_t address = i2c.read(true);
i2c.stop();
delay(100);
digitalWrite(LDACpin, HIGH);
Serial.println("");
Serial.println(ack1);
Serial.println(ack2);
Serial.println(ack3);
return address;
}
void writeAddress(int oldAddress, int newAddress, int LDACpin) {
digitalWrite(LDACpin, HIGH);
int ack1 = i2c.start( 0B11000000 | (oldAddress << 1));
int ack2 = i2c.ldacwrite(0B01100001 | (oldAddress << 2), LDACpin); // modified command for LDAC pin latch
int ack3 = i2c.write( 0B01100010 | (newAddress << 2));
int ack4 = i2c.write( 0B01100011 | (newAddress << 2));
i2c.stop();
delay(100); // wait for eeprom write
Serial.println("");
Serial.println(ack1);
Serial.println(ack2);
Serial.println(ack3);
Serial.println(ack4);
}