CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

SPI comunication problem

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
John Ilicitean



Joined: 07 Jul 2005
Posts: 27
Location: Rotova

View user's profile Send private message

SPI comunication problem
PostPosted: Wed Feb 22, 2006 4:42 am     Reply with quote

Hello to EveryONE!

I connect a PIC16f877 to AD with the bus SPI. This is my first code of SPI. For the line DOUT I want to send the data "3c". The AD have a CS active a "0".
I connect: the DIN of PIC with DIN of AD
the DOUT of PIC with DOUT of AD
the SCLK of PIC with SCLK of AD

This is my code:
Code:

#include <16F877.h>

#fuses HS,NOWDT,NOPROTECT,NOLVP,NOCPD,NOWRT
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

int x = 0;

void main()

{
      
   setup_spi(SPI_MASTER|SPI_L_TO_H|SPI_CLK_DIV_4);
   while(1);
   {
            
      spi_write(0x3c);
      if(spi_data_is_in())
      {
         x=spi_read();
         delay_ms(11);
      }
      delay_ms(11);
   }
}            



The SCLK not send the pulses of CLK and the DIN not send the data "0x3c".


The way to work in SPI bus is correct??

I have correctly connected the lines of the SPI bus???

It is necessary to put R pull-up???
Ttelmah
Guest







PostPosted: Wed Feb 22, 2006 5:09 am     Reply with quote

General comment. Normally Data out on one device, goes to Data in on the other.
Now, the clock line should run, even with this wrong. Are you sure you would see the clock signal. It'll be at 1.25MHz with the timings you have selected. Given there are only eight clock pulses, this wll be difficult to see, unless you have a storage scope.
Second comment, you don't say what DAC is involved, but normally, you have to send the command bytes, and then _clock back_ the reply. The SPI_read, will return the byte clocked back, while the command was sent. Normally this is a dummy byte, and you then then have to clock a second byte out, to get the reply. SPI_DATA_IS_IN, will always be true after sending the first byte, since the master has developed the clock for a byte.
So:
Check the data connections. The data sheet should say which is the input, and the output pin. You want the input connected to the PIC's 'out', and vice-versa.
Then try:
Code:

      spi_write(0x3c);
      //Check the data sheet - does the chip need a delay, before
      //data can be read?.
      x=spi_read(0);
      //Adding the '0' here, makes the master clock out a dummy byte
      //to get the returned data.
      delay_ms(11);

I'd suggest posting the ADC part number, since we can then tell you whether this is right. At present it is a 'guess', based on some SPI ADC's I have used.

Best Wishes
John Ilicitean



Joined: 07 Jul 2005
Posts: 27
Location: Rotova

View user's profile Send private message

PostPosted: Wed Feb 22, 2006 6:08 am     Reply with quote

Hi!!
The AD is the AD7789. In the datasheet show in the "Continuous Read Mode" if I send the "0x3c" for DIN , in DOUT is put the data register.

I have a digital storage oscilloscope to be able to see the clock pulses.

In the line SCLK not appears this pulses(I am afraid that here is the problem).

I have put by defect "SPI_CLK_DIV_4" but I don't know the differences between putting "SPI_CLK_DIV_16" or "SPI_CLK_DIV_64"
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Feb 22, 2006 9:32 am     Reply with quote

Quote:
The SCLK not send the pulses of CLK


Quote:
I connect: the DIN of PIC with DIN of AD
the DOUT of PIC with DOUT of AD


Quote:
{

setup_spi(SPI_MASTER|SPI_L_TO_H|SPI_CLK_DIV_4);
while(1); {

spi_write(0x3c);
if(spi_data_is_in())
{
x=spi_read();
delay_ms(11);
}
delay_ms(11);
}
}



What happened to your promise to use an A/D chip that
already has a known-good, working driver ?

You are going on the same path as the previous time.

You need to take a class in hardware and in the C language.
If you did this, you would not make the mistakes shown above.
John Ilicitean



Joined: 07 Jul 2005
Posts: 27
Location: Rotova

View user's profile Send private message

PostPosted: Wed Feb 22, 2006 9:51 am     Reply with quote

I went to buy the converter that you said to me, and they said that I send
to me in a month.I cannot wait for as much time. For this reason I buy the AD7789.

Single I need aid, like everybody.
Ttelmah
Guest







PostPosted: Wed Feb 22, 2006 10:09 am     Reply with quote

John Ilicitean wrote:
Hi!!
The AD is the AD7789. In the datasheet show in the "Continuous Read Mode" if I send the "0x3c" for DIN , in DOUT is put the data register.

I have a digital storage oscilloscope to be able to see the clock pulses.

In the line SCLK not appears this pulses(I am afraid that here is the problem).

I have put by defect "SPI_CLK_DIV_4" but I don't know the differences between putting "SPI_CLK_DIV_16" or "SPI_CLK_DIV_64"


No.
You have to send 3C, wait for the conversion to complete, and _then_ clock back _three_ bytes of data.
Look at page 16 of the data sheet.
You will need to be a bit more ingenious than your current code to do this. You will need a pull up resistor on the Dout pin of the chip (the chip uses this pin to signal that a conversion has completed, as well as to transfer the data.
You then need something like:
Code:

int32 value;
SPI_WRITE(0x3C);
while(input_bit(PIN_C4)); //Wait for the data to be ready
value=make32(read_spi(0),read_spi(0),read_spi(0));

You also need to set the CKP bit in the SPI configuration, so that the SPI idles high, or the chip will continuously reset.
John Ilicitean



Joined: 07 Jul 2005
Posts: 27
Location: Rotova

View user's profile Send private message

PostPosted: Fri Feb 24, 2006 6:13 am     Reply with quote

Hello!!!
Ttelmah thank's for to help me Wink .I proved your code. In the SCLK line I see the pulses of CLK ,four trains of pulses of 8 bits,like in the figure 16.
In the first train of pulses I see in the DIN line the data of "3c",until here all correct one.
According with datasheet , now the DOUT must to appear the data register but in this line appears, in the second trains of pulses( in the third bit of CLK) a pulses on DOUT and the rest of time are a low level.In this line are a Pull-up Resistor of 1kohm.

The Vdd are 5v,REFIN(+)=2.5V,REFIN(-)=0V.

The code is:


Code:

#include <16F877.h>


#fuses HS,NOWDT,NOPROTECT,NOLVP,PUT//,BROWNOUT
#use delay(clock=20000000)
#define ad_DI     PIN_C5
#define ad_DO     PIN_C4
#define ad_CLK    PIN_C3
int x,y,z ;
int32 value;
void init_ad()
{

    short int i;

      output_high(ad_DI);
      output_high(ad_CLK);           
   setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_CLK_DIV_16);

}

void main()

{
   while(1)
   {
      init_ad();
      
      
      SPI_WRITE(0x3C);
      while(input(PIN_C4)); //Wait for the data to be ready
      value=make32(spi_read(0),spi_read(0),spi_read0));
      
      DELAY_MS(11);
   }
}


I don't undestand because in DOUT don't appears the data register.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group