I've just remembered one more thing....
at the datasheet on page 24 is says:
The default state of the MAX1415/MAX1416 is to wait for a write to the communications register.
Any write or read operation on the MAX1415/MAX1416 is a two-step process:
- First, a command byte is written to the communications register. This command selects the input channel, the desired register for the next read or write operation, and whether the next operation is a read or a write.
- The second step is to read from or write to the selected register. At the end of the data-transfer cycle, the device returns to the default state.
See the Performing a Conversion section for examples.
If the serial communication is lost, write 32 ones to the serial interface to return the MAX1415/MAX1416 to the default state. The registers are not reset after this operation.
In the way I see a read or write operation is both the write to comm reg, and write/read the specified reg.. In your timing diagrams, as you can see, the CS is always low in read/write operations...
**broken link removed**
This Is not always happening in your code as I will show you:
This part of the code is respecting the above statements bit you are not waiting for data... so this code will stuck here... you need the data ready pooling
Code C - [expand] |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| void SeriesCommandBit()
{
//DataNotReady(); Remove this... you are not waiting for data!
digitalWrite(ss,LOW); // CS LOW
// Write Op
SPI.transfer(0x20);//WRITE TO COM REG - command for comm reg to select ch1 and write to clock register ( dec 32)
SPI.transfer(0xA5);//WRITE TO 8BIT REG -command for clock reg to set 2,4576Mhz ( dec 165)
// End of Write Op
// Write Op
SPI.transfer(0x10);//WRITE TO COM REG -command for comm reg to write setup register (dec 16)
SPI.transfer(0x44);//WRITE TO 8BIT REG -command for setup reg to self calibration,unipolar,unbuffered, (dec 68)
// End of Write Op
digitalWrite(ss,HIGH); // CS HIGH
} |
In the next code, i think that you are not respecting the read diagram that you posted above, because you put CS HIGH between the 1st and 2nd phase of the Read operation... As result you loose the communication... For recover communication with the ADC you need to send it 32 ones (0xFFFFFFFF), and it will be back the default state of wait for read and write operations (waiting for the first phase of read or write OP)...
Code C - [expand] |
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
| void loop()
{
// DataNotReady(); Remove this
digitalWrite(reset,LOW); // RESET LOW ->I don't know how to handle the reset pin
digitalWrite(ss,LOW); // CS LOW
delay(100);
digitalWrite(reset,HIGH); //Reset HIGH ->I don't know how to handle the reset pin
digitalWrite(ss,HIGH); // CS HIGH
SeriesCommandBit(); // This Sub IS correctly Handled as long you remove the wait for data pooling....
// DataNotReady(); remove this
digitalWrite(ss,LOW); // CS LOW
// READ OP
SPI.transfer(0x38); // WRITE TO COM REG
delayMicroseconds(1);
digitalWrite(ss,HIGH); // CS HIGH ->Here you violate the READ OPERATION and you LOOSE the communication
DataNotReady();
digitalWrite(ss,LOW); // CS LOW -> removing the above cs high you can also remove this
highByte = SPI.transfer(0x00);// READ HIGH BYTE OF WORD REG
lowByte = SPI.transfer(0x00);// READ LOW BYTE OF WORD REG
// END OF READ OP
adcValue = highByte << 8;
adcValue = adcValue | lowByte;
digitalWrite(ss,HIGH); // CS HIGH
// SEND DATA to PC
Serial.print("analog value =");
Serial.println(adcValue, DEC);
Serial.print('\n');
delay(1000);
} |
About the reset pin... I think You are adding complexity to the subject... You should try to avoid using reset pin until you figure out how to handle comm...
I also suggest you to code a read data function, a write config function, and two poling functions(by hardware , and by soft... in order for you to be able to perform tests...)...
One other advice... start from a working code( for example start with I code I gave You).. Perform small changes to code, and test them to see if it works... If you want to poll the status by hardware, you should start for try changing my code for polling by hardware...
I have a few ideas on how to do it, but I'm afraid of make you loose the fun off figuring it out...
---------- Post added at 13:10 ---------- Previous post was at 12:35 ----------
....
and yes putting the cs always low still works in your previous code
In the my code there is no CS HIGH as in the code you posted on line 45
---------- Post added at 14:00 ---------- Previous post was at 13:10 ----------
Here is a version of my code with CS handling... I hope you can understand how to handle CS here... and I use the data busy pooling only before read the data...
Code C - [expand] |
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
| #include "SPI.h"
int ss=10;
unsigned int adcValue;
void setup()
{
pinMode(ss, OUTPUT);
SPI.begin();
SPI.setBitOrder(MSBFIRST);
Serial.begin(9600);
}
void MAX1416_Config()//You can modify it for handle channels
{
//series of commandbit
digitalWrite(ss,LOW); // Enable ADC SPI
//Write OP
SPI.transfer(0x20);//command for comm reg to select ch1 and write to clock register ( dec 32)
SPI.transfer(0xA5);//command for clock reg to set 2,4576Mhz ( dec 165)
//End Write OP
//Write OP
SPI.transfer(0x10);//command for comm reg to write setup register (dec 16)
SPI.transfer(0x44);//command for setup reg to self calibration,unipolar,unbuffered, (dec 68)
//End Write OP
digitalWrite(ss,HIGH); // Disable ADC SPI
}
void MAX1416_WaitForData_Soft()
{
char DataNotReady = 0x80;
digitalWrite(ss,LOW); // Enable ADC SPI
while(DataNotReady) // wait for end of conversion
{
// Read OP
SPI.transfer(0x08);//command for comm reg to read (dec 8)
DataNotReady =SPI.transfer(0x00); // Read comm register
// End Read OP
DataNotReady &= 0x80;
}
digitalWrite(ss,HIGH); // Disable ADC SPI
}
unsigned int MAX1416_ReadCH0Data() //You can modify it to read other channels
{
unsigned int uiData;
byte highByte;
byte lowByte;
digitalWrite(ss,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(ss,HIGH); // Disable ADC SPI
uiData = highByte << 8;
uiData |= lowByte;
return uiData;
}
void loop()
{
MAX1416_Config();
while(1)
{
MAX1416_WaitForData_Soft() ;
adcValue = MAX1416_ReadCH0Data();
Serial.print("analog value =");
Serial.println(adcValue, DEC) );
Serial.print('\n');
delay(1000);
}
} |
You can include the data ready pooling in the read channel function.. that way you can reduce cs pin activity ...
Please let me know if the above code works...
Any way, with all this said, I'm pretty sure that the main reason for the your code not to be working is the underhanded use of the DRDY poling... You check DRDY only before read data...
And I still have a few doubts on the CS usage.. is possible that I miss understood the read/write MAX1416 OP with the read/write SPI OP... Any first I need to know the result of testing the code I've provided to be able to exclude the Data Ready ISSUE