-
Notifications
You must be signed in to change notification settings - Fork 1
/
max1415.cpp
88 lines (70 loc) · 1.9 KB
/
max1415.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
78
79
80
81
82
83
84
85
86
87
88
#include "max1415.h"
int ChipSelect;
int DRDY;
max1415::max1415(int CS, int Select)
{
ChipSelect = CS;
DRDY = Select;
ADCSerialInt();
ChipConfig();
}
void max1415::ADCSerialInt()
{
digitalWrite(ChipSelect, LOW); // Enable ADC SPI
SPI.transfer(0xFF);
SPI.transfer(0xFF);
SPI.transfer(0xFF);
SPI.transfer(0xFF);
digitalWrite(ChipSelect,HIGH);
}
void max1415::ChipConfig()
{
//series of commandbit
digitalWrite(ChipSelect,LOW); // Enable ADC SPI
//Write OP
SPI.transfer(0x20);//command for comm reg to select ch1 and write to clock register
delay(100);
SPI.transfer(0xA7);//command for clock reg to set 2,4576Mhz
//End Write OP
delay(100);
//Write OP
SPI.transfer(0x10);//command for comm reg to write setup register
delay(100);
SPI.transfer(0x44);//command for setup reg to self calibration,unipolar,unbuffered,
//End Write OP
digitalWrite(ChipSelect,HIGH); // Disable ADC SPI
}
unsigned int max1415::GetReading()
{
unsigned int uiData;
byte highByte;
byte lowByte;
digitalWrite(ChipSelect,LOW); // Enable ADC SPI
// READ Data OPERATION
SPI.transfer(0x38);//command for the comm to read data register for channel 1 (dec 56)
//read 16bit of data ADC
highByte = SPI.transfer(0x00);
lowByte = SPI.transfer(0x00);
// End Read Data
digitalWrite(ChipSelect,HIGH); // Disable ADC SPI
uiData = highByte;
uiData <<= 8;
uiData |= lowByte;
return uiData;
}
void max1415::WaitForData()
{
char DataNotReady = 1;
char value;
while(DataNotReady) // wait for end of conversion
{
// Read OP
value = digitalRead(DRDY); // Read comm register
if (value == LOW)
DataNotReady = 0;
else
DataNotReady = 1;
// End Read OP
//Serial.println("NOT READY");
}
}