# how to check SPI signals

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;
}

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


I am trying to read this configuration setup using this

void ReadAdConfReg(void)
{
SPCR = 0x5D;
spi(0x50);
}


printing like this

ReadAdConfReg();


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.

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.

-
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. – TJD Oct 12 '12 at 14:48
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. – TJD Oct 12 '12 at 14:50
@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. – verendra Oct 12 '12 at 15:00
@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. – Michael Burr Oct 13 '12 at 7:34
why did you remove the images? – Ben Voigt Oct 23 '12 at 18:35

## migrated from stackoverflow.comOct 15 '12 at 14:52

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
spi(cmd);                   //Place instruction in communication register
data = spi(value >> 8);     // read hi-byte
data = (data << 8) | spi(value);    // and lo-byte.
return data;
}


for write

AD7798_16(0x08, 0x000A);

-

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.

-
 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. – verendra Oct 13 '12 at 9:41