-
Notifications
You must be signed in to change notification settings - Fork 0
/
MLX90392_Arduino.ino
164 lines (158 loc) · 4.99 KB
/
MLX90392_Arduino.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/**
* @copyright (C) 2021 Melexis <http://www.melexis.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
* Rev2.0: RequestFrom return can be used to check for Nack.
*/
#include <Wire.h>
char Cmd = '0'; // Char received from the PC.
int Nack = 0; // >0 If the IC did not send an Ack.
int Nbytes = 0; // Number of bytes to read from IC.
int Flag = 0; // Flag to indicate termination of burst writing to the IC.
byte Register = 0; // Register to read from or write to.
long freq = 100000; // I2C Frequency, in Hz.
void setup()
{
Wire.begin();
Wire.setClock(freq);
Serial.begin(9600);
}
void loop()
{
if (Serial.available() > 0) {
Cmd = Serial.read();
}
switch (Cmd) {
case 'A':
// Read Nbytes (if 0, then 16 bytes), starting from the specified Register.
while (!Serial.available()) {}
Register = hex2dec(Serial.read());
while (!Serial.available()) {}
Register = 16 * Register + hex2dec(Serial.read());
while (!Serial.available()) {}
Nbytes = hex2dec(Serial.read());
if (Nbytes == 0) {
Nbytes = 16;
}
Wire.beginTransmission(0x0C);
Wire.write(Register);
Nack = Wire.endTransmission(false);
Nack = Nack + (Nbytes - Wire.requestFrom(0x0C, Nbytes, true));
Serial.print(Nack);
Serial.print(";");
while (Nbytes > 0) {
Serial.print(Wire.read());
Serial.print(";");
Nbytes--;
}
Serial.print("\n");
Cmd = '0';
break;
case 'B':
// Read direct Nbytes (if 0, then 16 bytes).
while (!Serial.available()) {}
Nbytes = hex2dec(Serial.read());
if (Nbytes == 0) {
Nbytes = 16;
}
Nack = (Nbytes - Wire.requestFrom(0x0C, Nbytes, true));
Serial.print(Nack);
Serial.print(";");
while (Nbytes > 0) {
Serial.print(Wire.read());
Serial.print(";");
Nbytes--;
}
Serial.print("\n");
Cmd = '0';
break;
case 'D':
// Addressed reset.
while (!Serial.available()) {}
Register = hex2dec(Serial.read());
while (!Serial.available()) {}
Register = 16 * Register + hex2dec(Serial.read());
Wire.beginTransmission(Register);
Wire.write(byte(0x11));
Wire.write(byte(0x06));
Nack = Wire.endTransmission(true);
Serial.print(Nack);
Serial.print("\n");
Cmd = '0';
break;
case 'E':
// Write consecutive bytes into the registers of the IC, starting from the specified Register.
while (!Serial.available()) {}
Register = hex2dec(Serial.read());
while (!Serial.available()) {}
Register = 16 * Register + hex2dec(Serial.read());
Wire.beginTransmission(0x0C);
Wire.write(Register);
// Write byte per byte, checking each time if writing should be terminated (reception of char 'T').
Flag = 0;
while (Flag == 0) {
while (!Serial.available()) {}
Cmd = Serial.read();
if (Cmd == 'T') {
Flag = 1;
} else {
while (!Serial.available()) {}
Wire.write(byte(16 * hex2dec(Cmd) + hex2dec(Serial.read())));
}
}
Nack = Wire.endTransmission(true);
Serial.print(Nack);
Serial.print("\n");
Cmd = '0';
break;
case 'W':
// Change the I2C frequency.
while (!Serial.available()) {}
freq = hex2dec(Serial.read());
while (!Serial.available()) {}
freq = 10 * freq + hex2dec(Serial.read());
while (!Serial.available()) {}
freq = 10 * freq + hex2dec(Serial.read());
while (!Serial.available()) {}
freq = 10 * freq + hex2dec(Serial.read());
while (!Serial.available()) {}
freq = 10 * freq + hex2dec(Serial.read());
while (!Serial.available()) {}
freq = 10 * freq + hex2dec(Serial.read());
while (!Serial.available()) {}
freq = 10 * freq + hex2dec(Serial.read());
Wire.setClock(freq);
Serial.print("I2C freq now: ");
Serial.print(freq);
Serial.print(" Hz\n");
Cmd = '0';
break;
default:
Cmd = '0';
break;
}
}
int hex2dec(char input)
// Convert the hexadecimal ASCII input to the integer it represents.
{
int result = 0;
if (input >= 48 && input <= 57) {
result = (int) input - 48;
}
if (input >= 65 && input <= 70) {
result = (int) input - 55;
}
return result;
}