1
\$\begingroup\$

I am working with ATmega32-A micro controller. I am trying to read ADC (AD7798) values. I am setting ADC registers as shown below. Images also attached.

char spi(char data)
{
    //Start transmision
    SPDR = data;
    //Wait for transmision complete
    while(!(SPSR & 0x80));
    return SPDR;
} 

void setupAd(){
    ADC_CS=0; 
    SPCR = 0x5D;
    spi(0x10);   //set up communication register for configuration reg (16bit).
    spi(0x07);    // sending 07 as MSB     
    spi(0x10);    //sending 10 as LSB 
    ADC_CS=1; 
}

I am trying to read this configuration setup using this

void ReadAdConfReg(void)
{              
    SPCR = 0x5D;
    ADC_CS=0;
    spi(0x50);
    adcConfig = spi(0xFF)<<8;
    adcConfig |= spi(0xFF);   
    ADC_CS=1;
}

printing like this

ReadAdConfReg();
printf("configreg:%d",adcConfig);

But my problem setup of that configuration registor was not working. I have checked with oscilloscope any mistakes in clock polarity and chip select. I have attached images as shown below.Clock and write (MOSI)(Din pin of ADC) Clock and Read (MISO)(Dout pin of ADC) Clock and chip select

Please help me why i am not able to set configuration register. clock polarities and chip select every thing ok according to ATmega32-A data sheet and AD7798 datasheet. please help me what mistake i am doing. or If I want to check my configuration register setup on oscilloscope What i have to do.

\$\endgroup\$

migrated from stackoverflow.com Oct 15 '12 at 14:52

This question came from our site for professional and enthusiast programmers.

  • 1
    \$\begingroup\$ What is the yellow trace? MOSI? What about MISO? Do you have a 4 channel scope so you can see all the signals? You can go bit by bit and see exactly what you sent and exactly what you receive. One of those won't be what you expect. \$\endgroup\$ – TJD Oct 12 '12 at 14:48
  • 1
    \$\begingroup\$ Also, don't try and start with something like writing a config register. If SPI isn't working yet, start by just doing the simplest command like reading the chip ID. That way you know exactly what to expect. \$\endgroup\$ – TJD Oct 12 '12 at 14:50
  • \$\begingroup\$ @TJD yes, blue signal is colck, Yellow is MISO, MOSI, CS in diffrent images. I am reciving what i am sending. I have tried to read Id register it giving value"48". But in the data sheet Id register contains"0xX8". So i am confusing where is the fault either in SPI or Register setup or readsetup. \$\endgroup\$ – verendra Oct 12 '12 at 15:00
  • \$\begingroup\$ @verenda: when you say that reading the ID register gives a value "48" do you mean 48 decimal or 0x48? The latter seems to be a correct response. \$\endgroup\$ – Michael Burr Oct 13 '12 at 7:34
  • 1
    \$\begingroup\$ why did you remove the images? \$\endgroup\$ – Ben Voigt Oct 23 '12 at 18:35
1
\$\begingroup\$

I have changed the functions to read 8 bit and 16 bit registers then it worked.

unsigned int Bit_16(unsigned char cmd, unsigned int value) 
{
    unsigned int data; 
    SPCR = 0x5D;        // mode #3 F_CPU/16 
    ADC_CS = 0;                 // enable 
    spi(cmd);                   //Place instruction in communication register 
    data = spi(value >> 8);     // read hi-byte 
    data = (data << 8) | spi(value);    // and lo-byte. 
    ADC_CS = 1;                 // disable 
    return data; 
} 

for write

AD7798_16(0x08, 0x000A);
\$\endgroup\$
0
\$\begingroup\$

You need to read the ADC datasheet more closely.

For each communication with the ADC you need to send a value for the communication register. Per the datasheet, CR7 must be low or no other bits are clocked into the communication register.

So for the first byte you send it must have the MSB cleared.

Bit 6 is whether you want a read or a write. Bits 5-3 are what register you want to operate on. Bit 2 is whether you want to continuously read Bits 1-0 should be 0's.

Please look over the datasheet's "Digital Interface" section, as well as the "On-Chip Registers" section.

\$\endgroup\$
  • \$\begingroup\$ Yes I have done that. I have that task more than 100 times. I have set all configuration right. You can see in the above code what I have set for communication register. \$\endgroup\$ – verendra Oct 13 '12 at 9:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.