Post Go back to editing

Problem with AD7856 SPI in Arduino... Help!!

Hi, I try using the AD7856 in Arduino Mega ADK board, I read the datasheet and I have programing under Mode 2, but the AD7856 don't answer me, I connect a oscilloscope to MOSI and MISO lines and Arduino board send correctly the 16bits in MOSI line but in MISO only LOW! so this is my code in Arduino IDE, I hope you can help me!

#include <SPI.h>
uint8_t adcMSB = 0, adcLSB = 0;
unsigned int read_adc;
void setup()
{
  SPI.begin();
  SPI.setClockDivider(SPI_CLOCK_DIV4); //master clock 4MHZ
  SPI.setDataMode(SPI_MODE3);               //mode SPI 3
  SPI.setBitOrder(MSBFIRST);
  pinMode(53,OUTPUT);     
  digitalWrite(53,HIGH);
  pinMode(46,OUTPUT);
  digitalWrite(46,HIGH);
  Serial.begin(9600);
}


void loop()
{
  digitalWrite(46,LOW);     //this start the conversion in specific pin CONVST
  delay(1);                              //wait the device busy pin
  digitalWrite(46,HIGH);     //and start the trans.
  digitalWrite(53,LOW);     //enable device
  adcMSB=SPI.transfer(0b11100001); //here I configure the bits in Control Register, read channel 1, power mode normal
  adcLSB=SPI.transfer(0b00010001); //this read from ADC OUTPUT DATA REGISTER, start the Conversion, select mode 2
  digitalWrite(53,HIGH); //disable device
  delay(300);
  
//read for results
  read_adc= word(adcMSB, adcLSB);
  Serial.print(read_adc,HEX);
  Serial.print(" ");
  Serial.println(read_adc,DEC);
  
}

and this is my schematic:

  • Yes, the 4th image is the response to channel one to 5VDC (AN1).

    In the datasheet the SPI mode of ADC for microcontrollers is mode 3:

    For all the mControllers the CPOL bit is set to 1 and for the 68HC11/16/L11

    the CPHA bit is set to 1.

    but if I put the mode3 in the instruction:

    SPI.setDataMode(SPI_MODE);

    the ADC don't work. Should I change the mode?

    Best regards!!

    Miguel

  • Hi, Miguel.

    Yes, you should change SPI mode to 3. Datasheet recommendations should be followed, else, we cannot guarantee the part’s performance.

    Can we review this issue from the beginning so we can see things more clearly? Let me know if I understand this correctly.

    With the first code you sent, where you set SPI mode to 3, you only get all 0s from MISO.

    You then attached the second code where you’ve set SPI mode to 0. With this, you got some response from the ADC. But you got different codes all for an input of 5VDC on AIN1? Furthermore, this code results in the ADC working for a few seconds only; after which the ADC gets a power cycle (turns off, then on) and works for a few seconds again.

    Most recently, you made the third version wherein you added for loops to get the part to read 2 channels. I asked you to check your configuration and pointed out the need for SCK to idle high. This is still with the SPI mode set to 0. Do I understand correctly that the ADC does not stop with this code but you need to write zeros to the SPI bus to be able to read samples?

    Also, can you clarify what you meant when you said the ADC doesn’t work when you set SPI mode to 3? Do the MISO lines go back to giving all zeros again? Can you share scope shots with SPI mode set to 3?

    Regards,

    Karen

  • Hi Karen thanks for helping me, excuse my bad English.

    KarenNE escribió:

    Hi, Miguel.

    Yes, you should change SPI mode to 3. Datasheet recommendations should be followed, else, we cannot guarantee the part’s performance.

    Can we review this issue from the beginning so we can see things more clearly? Let me know if I understand this correctly.

    With the first code you sent, where you set SPI mode to 3, you only get all 0s from MISO. Yes, in mode 3 the ADC stops ie the ADC sends zeros on MISO after a short time but in mode 3 the ADC converted successfully before stopping. When the ADC stops I have to restart for the ADC to work again.

    You then attached the second code where you’ve set SPI mode to 0. With this, you got some response from the ADC. But you got different codes all for an input of 5VDC on AIN1? Furthermore, this code results in the ADC working for a few seconds only; after which the ADC gets a power cycle (turns off, then on) and works for a few seconds again. I changed to mode 0 because I wrote a code to software SPI (this code I don't upload) and this code I wrote in mode 0 and the ADC don't stops so I porting  this SPI software to SPI hardware with mode 0 and 125kHz in speed bus.

    Most recently, you made the third version wherein you added for loops to get the part to read 2 channels. I asked you to check your configuration and pointed out the need for SCK to idle high. This is still with the SPI mode set to 0. Do I understand correctly that the ADC does not stop with this code but you need to write zeros to the SPI bus to be able to read samples? Yes, Arduino don't have a code to read directly  in SPI bus, so I have sending zeros to generate the clock in SCK pin. Loops and 15 ms in the third version are to prevent conversions overlap because if I read the AN1 and after AN2 the channels values overlapping.

    Also, can you clarify what you meant when you said the ADC doesn’t work when you set SPI mode to 3?

    Yes, in mode 3 the ADC stops ie the ADC sends zeros on MISO after a short time but in mode 3 the ADC converted successfully before stopping. When the ADC stops I have to restart for the ADC to work again.

    Do the MISO lines go back to giving all zeros again? Yes.

    Can you share scope shots with SPI mode set to 3? Do I make catches with loops and time of 15 ms or without these?

    Do you have any ideas for the source code?

    Regards,

    Karen

  • Hi Karen!

    This is the scope shoots with mode3 and 4MHz in sck.

    The SS signal:

    the Master output Slave input data:

    the Master input Slave output data tied to 5VDC:

    best regards!

    Miguel

  • Hi, Miguel.

    Thanks for following through. From the scope shots you sent, one difference I noticed is that MOSI is 1111000100010001 (AIN2 is the selected channel for conversion). Assuming that 5VDC is also the input on this channel, MISO is about right, although there’s a difference of a few codes. 

    Are all signals (/SYNC, MOSI, and SCK) still properly setup when MISO goes to all 0s? I am not an expert on Arduino parts, but from the code you last sent, the sequence that you are using is:

    ·         Set /CONVST high

    ·         Set /SYNC high

    ·         Wait 150 ms

    ·         setChannel (AIN1)

    ·         wait 10 us

    ·         readChannel (AIN1)

    ·         setChannel (AIN2)

    ·         wait 10 us

    ·         readChannel (AIN2)

    ·         print results

    ·         this loops consecutively

    with setChannel,

    ·         pulse /CONVST low for 1us then high again  //can you include a check for the BUSY signal after this?

    ·         Wait 10us

    ·         Pulse /SYNC low

    ·         Write to config register

    ·         Pulse /SYNC high

    ·         Wait 15 ms

    With readChannel,

    ·         pulse /CONVST low for 1us then high again  //can you include a check for the BUSY signal after this?

    ·         Wait 10us

    ·         Pulse /SYNC low

    ·         Generate SCK (125 kHz?)

    ·         Read ADC output register

    ·         Pulse /SYNC high

    I am assuming that pin 12 is /CONVST and pin 53 is /SYNC. Please confirm if i got this right.

    As per Page 23 of the AD7856 datasheet, “when writing to the part via the DIN line there is the possibility of writing data into the incorrect registers, such as the test register for instance, or writing the incorrect data and corrupting the serial interface." What I recommend for you to do other than the comments on the sequence is to write the 16-bit word 0100 0000 0000 0010 to restore the test register to its default value.  I noticed you had this word declared but didn't use it.

    Hope this helps.

    Regards,

    Karen

  • Hi Karen! thanks for help me!

    Are all signals (/SYNC, MOSI, and SCK) still properly setup when MISO goes to all 0s?

    Yes the Arduino board sends the correct signals in SS, MOSI and SCK. and the ADC in MISO goes to all zeros after little time.


    can you include a check for the BUSY signal after this?

    Yes of course I can put in the source code, upload some screenshot?. I assumed that the time to conversion is 3.5us as said the page 5 in datasheet Figure 2. Timing Diagram for Interface Mode 2 (Reading/Writing After Conversion).On the other hand, Figure 3 shows that the maximum time conversion is 3.5us, should not be longer than this time compared with that of Figure 2?, In future I hope to work only with the four SPI signals and remove the CONVST pin.

    I am assuming that pin 12 is /CONVST and pin 53 is /SYNC. Please confirm if i got this right.

    Yes the connections Master-Slave:


    AD7856   | Arduino Mega

    24 SYNC  <- 53 SS

    23 SCLK  <- 52 SCK

    21 DIN    <- 51 MOSI

    20 DOUT  -> 50 MISO

    1 CONVST <- 12 pin


    In pin 22 ADC CLKIN I use 6MHZ oscilator I have the A version, AD7856ANZ.

    As per Page 23 of the AD7856 datasheet, “when writing to the part via the DIN line there is the possibility of writing data into the incorrect registers, such as the test register for instance, or writing the incorrect data and corrupting the serial interface." What I recommend for you to do other than the comments on the sequence is to write the 16-bit word 0100 0000 0000 0010 to restore the test register to its default value.  I noticed you had this word declared but didn't use it.

    Yes, I reset the registry but the ADC is in the same behavior.

    I do not know what to do with the ADC, serve something if I show the connection of the ADC?

  • Hi,

    Yes, it would be great if you could include BUSY on your source code and send in scope shots such as shown in Figure 2. Another thing to note is that when using the software conversion start, you “must ensure that the control register write operation extends beyond the falling edge of BUSY. The falling edge of BUSY resets the /CONVST bit to 0 and only after this time can it be reprogrammed to 1 to start the next conversion.”

    As for the conversion times on the figures in page 5,  both are 3.5us. The tCONVERT on Figure 3 just looks longer to emphasize when reading/writing takes place.

    Please also note that if resetting the interface is required, the SYNC must be taken high and then low.

    I believe you have attached your ADC connection before. I would suggest you make the analog and digital sections (supplies included) separated and to minimize coupling. Please also ensure that the digital and analog ground planes are joined in one place as close as possible to the device.

    Regards,

    Karen

  • This question has been assumed as answered either offline via email or with a multi-part answer. This question has now been closed out. If you have an inquiry related to this topic please post a new question in the applicable product forum.

    Thank you,
    EZ Admin