-
Notifications
You must be signed in to change notification settings - Fork 0
/
a1454_i2c_programmer.ino_
107 lines (92 loc) · 3.1 KB
/
a1454_i2c_programmer.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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <Wire.h>
// this sketch writes an A1454 I2C address to its EEPROM
// make sure to set the sensor's ADR0 and ADR1 pin to VCC to use the custom address
// start reading code and comments in setup procedure
void scan(){
Serial.println(" Scanning I2C Addresses");
uint8_t cnt=0;
for(uint8_t i=0;i<128;i++){
Wire.beginTransmission(i);
uint8_t ec=Wire.endTransmission(true);
if(ec==0){
if(i<16)Serial.print('0');
Serial.print(i,HEX);
cnt++;
}
else Serial.print("..");
Serial.print(' ');
if ((i&0x0f)==0x0f)Serial.println();
}
Serial.print("Scan Completed, ");
Serial.print(cnt);
Serial.println(" I2C Devices found.");
}
bool i2cReady(uint8_t adr){
uint32_t timeout=millis();
bool ready=false;
while((millis()-timeout<100)&&(!ready)){
Wire.beginTransmission(adr);
ready=(Wire.endTransmission()==0);
}
return ready;
}
const int CUST_ACC_ADR=0x24;
const int CURRENT_SENSOR_ADR=0x00;
void activateCustomerAccessMode() {
byte buffer[4]; // magic string to make sensor writeable until next power cycle (lol, hardware)
buffer[0] = 0x2c;
buffer[1] = 0x41;
buffer[2] = 0x35;
buffer[3] = 0x34;
Wire.beginTransmission(CURRENT_SENSOR_ADR);
Wire.write(CUST_ACC_ADR);
Serial.print("no. of bytes written to cust. access register: ");
Serial.println(Wire.write(buffer, 4));
Serial.print("end transmission error code for cust. access register: ");
Serial.println(Wire.endTransmission(true));
}
int addrRegister(bool persistant) { return persistant ? 0x03 : 0x0b; }
// persistent eeprom address can only be written 1000 times!
void writeI2Caddress(bool persistant, int newSensorAdress) {
int addr = addrRegister(persistant);
Wire.beginTransmission(CURRENT_SENSOR_ADR);
byte buffer[4]; // see data sheet page 13 for register details
buffer[0] = 0x00;
buffer[1] = 0x00;
buffer[2] = 0x00;
buffer[3] = newSensorAdress;
Wire.write(addr);
Serial.print("no. of bytes written to address register: ");
Serial.println(Wire.write(buffer, 4));
Serial.print("end transmission error code for address register: ");
Serial.println(Wire.endTransmission(true));
}
void readI2Caddress(bool persistant) {
int addr = addrRegister(persistant);
Wire.beginTransmission(CURRENT_SENSOR_ADR);
Wire.write(addr);
Wire.endTransmission(false);
Wire.requestFrom(CURRENT_SENSOR_ADR, 4, true);
Serial.print("bytes in address register ");
Serial.print(addr, HEX);
Serial.print(" (hex): ");
while (Wire.available()) {
int i = Wire.read();
Serial.print(i, HEX);
Serial.print( " ");
}
Serial.println("");
}
void setup(){
Serial.begin(115200);
Wire.begin();
Serial.println();
//activateCustomerAccessMode(); delay(100);
//writeI2Caddress(true, 0x40); delay(100); // bool param is whether to write persistent (only 1000 times possible!) or transient address register
//readI2Caddress(true); // bool param is whether to read persistent or transient address register
// scan for i2c devices and print their addresses.
// changing address of A1454 seems to require writing to persistent address register and power cycle the device
scan();
Serial.println();
}
void loop(){}