-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperipherals.ino
More file actions
180 lines (152 loc) · 5.54 KB
/
peripherals.ino
File metadata and controls
180 lines (152 loc) · 5.54 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//This contains subroutines for generic peripherals
//MAX5387 Variable resistor
//MAX1932 HV PSU
void ThresholdSet(int ThreshA, int ThreshB) {
//This kinda works
// Serial.println("Test of threshold setting");
// Serial.print(ThreshA);
//Serial.println(analogRead(A1)); //internal readback of the DREF value only, not returned to main code.
Wire.beginTransmission(I2CPot); // transmit to device #94 (0x5F)
// device address is specified in datasheet
Wire.write(0x11); // sends instruction byte
Wire.write(ThreshA); // sends potentiometer value byte
Wire.endTransmission(); // stop transmitting
// Serial.println(" "+analogRead(A1));
// Serial.println("Setting Ch2");
// Serial.print(ThreshB);
//Serial.println(analogRead(A2)); //internal readback of the DREF value only, not returned to main code.
Wire.beginTransmission(I2CPot); // transmit to device #94 (0x5F)
// device address is specified in datasheet
Wire.write(0x12); // sends instruction byte
Wire.write(ThreshB); // sends potentiometer value byte
Wire.endTransmission(); // stop transmitting
// Serial.println(" "+ analogRead(A2));
}
void ThresholdCalc(int ThreshA[1000], int ThreshB[1000]) {
//This isn't working right now, need to check the addressing and the way of setting A and B registers
// Serial.println("Test of threshold setting");
// Serial.print(ThreshA);
//Serial.println(analogRead(A1)); //internal readback of the DREF value only, not returned to main code.
// Wire.beginTransmission(0x28); // transmit to device #94 (0x5F)
// device address is specified in datasheet
// Wire.write(0x11); // sends instruction byte
// Wire.write(ThreshA); // sends potentiometer value byte
// Wire.endTransmission(); // stop transmitting
// Serial.println(" "+analogRead(A1));
// Serial.println("Setting Ch2");
// Serial.print(ThreshB);
//Serial.println(analogRead(A2)); //internal readback of the DREF value only, not returned to main code.
// Wire.beginTransmission(I2CPot); // transmit to device #94 (0x5F)
// device address is specified in datasheet
// Wire.write(0x12); // sends instruction byte
// Wire.write(ThreshB); // sends potentiometer value byte
// Wire.endTransmission(); // stop transmitting
// Serial.println(" "+ analogRead(A2));
}
void VbiasSet(int Vbias) {
byte _send = byte (Vbias);
Serial.print("Set the Vbias to ");
Serial.println(Vbias);
digitalWrite(SS_pin, LOW); // SS low
for (int i = 0; i < 8; i++) // There are 8 bits in a byte
{
digitalWrite(MOSI_pin, bitRead(_send, 7-i)); // Set MOSI
digitalWrite(SCK_pin, HIGH); // SCK high
//bitWrite(_receive, i, digitalRead(MISO_pin)); // Capture MISO
digitalWrite(SCK_pin, LOW); // SCK low
}
digitalWrite(SS_pin, HIGH); // SS high again
}
//bus scanning routines for debugging, borrowed from code on Arduino.cc
// according to the i2c scanner by Nick Gammon
// http://www.gammon.com.au/forum/?id=10896
void busscan0()
{
byte error, address;
int nDevices;
Serial.println("Scanning I2C Bus 0...");
nDevices = 0;
for(address = 1; address < 127; address++ )
{
// The i2c_scanner uses the return value of
// the Write.endTransmisstion to see if
// a device did acknowledge to the address.
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0)
{
Serial.print("I2C device found at address 0x");
if (address<16)
Serial.print("0");
Serial.print(address,HEX);
Serial.println(" !");
nDevices++;
}
else if (error==4)
{
Serial.print("Unknow error at address 0x");
if (address<16)
Serial.print("0");
Serial.println(address,HEX);
}
}
if (nDevices == 0)
Serial.println("No I2C devices found\n");
else
Serial.println("done\n");
delay(5000); // wait 5 seconds for next scan
}
void busscan1()
{
byte error, address;
int nDevices;
Serial.println("Scanning I2C Bus 1...");
nDevices = 0;
for(address = 1; address < 127; address++ )
{
// The i2c_scanner uses the return value of
// the Write.endTransmisstion to see if
// a device did acknowledge to the address.
Wire1.beginTransmission(address);
error = Wire1.endTransmission();
if (error == 0)
{
Serial.print("I2C device found at address 0x");
if (address<16)
Serial.print("0");
Serial.print(address,HEX);
Serial.println(" !");
nDevices++;
}
else if (error==4)
{
Serial.print("Unknow error at address 0x");
if (address<16)
Serial.print("0");
Serial.println(address,HEX);
}
}
if (nDevices == 0)
Serial.println("No I2C devices found\n");
else
Serial.println("done\n");
delay(5000); // wait 5 seconds for next scan
}
void clearI2C(){
// Issue 20 I2C clocks to make sure no slaves are hung in a read
pinMode(20, OUTPUT);
pinMode(21, OUTPUT);
pinMode(70, OUTPUT);
pinMode(71, OUTPUT);
digitalWrite(20, LOW);
digitalWrite(70, LOW);
for (int i = 0; i < 20; i++)
{
digitalWrite(21, LOW);
digitalWrite(71, LOW);
delayMicroseconds(10);
digitalWrite(21, HIGH);
digitalWrite(71, HIGH);
delayMicroseconds(10);
}
}