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 support@ccsinfo.com

spi_read doesn't work

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



Joined: 12 Jul 2007
Posts: 32

View user's profile Send private message

spi_read doesn't work
PostPosted: Tue Jul 24, 2007 8:35 am     Reply with quote

Hi,
I've still have a problem with my SPI. I use PIC18F2480 and AD7790. The connection is via SPI.
My code is as followed:
Code:

#include <18f2480.h>
#device *=16
#device ADC=10

#use delay(clock=8000000)

#fuses NOWDT,HS,NOPUT,NOPROTECT,NOBROWNOUT


#define SPI_MODE_0_0 (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_0_1 (SPI_L_TO_H)
#define SPI_MODE_1_0 (SPI_H_TO_L)
#define SPI_MODE_1_1 (SPI_H_TO_L | SPI_XMIT_L_TO_H)

#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7,BRGH1OK,stream=debug)


#define SPANNUNG_REF 4
#define KOMPERATOR_REF   0
#define LUFTBLASENSENSOR 1

#define CS PIN_C2

#define CREAD 0x3C      // continous read


void main()
{

   int8 ad_in1;
   int8 ad_in2;
   int8 ad_in3;


   setup_spi(SPI_MASTER | SPI_MODE_1_1 | SPI_CLK_DIV_64);
   setup_adc(adc_off);


   output_high(CS);

   delay_ms(100);


   while (TRUE)      
   {
      
      delay_ms(20);

      if (input(PIN_B0))
         {
         printf("Yes;");
         }
      else
         {
         printf("No;");
         }
   
      output_low(CS);   

      
      spi_write(CREAD);


      while(input(PIN_C5))

      ad_in1 = spi_read(0);
      ad_in2 = spi_read(0);
      ad_in3 = spi_read(0);
      output_high(CS);


      fprintf(debug,"%u %u %u;\n", ad_in1, ad_in2, ad_in3);

   }
}


I also found out that DIN and DOUT is always low. The code stops when using spi_read(0). Why?
Does the spi_write really writes on the communication register of the AD7790?

Thanks!
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Tue Jul 24, 2007 9:02 am     Reply with quote

Don't start a new thread when the same problem still applies.

Code:
#device *=16
Get rid of this line as it is only confusing things. A PIC18 processor always uses 16 bit pointers.


Are you using a low voltage programmer? 99% of the people don't and than it is better to add the NOLVP fuse. With the wrong setting for this fuse a voltage spike on pin B5 will cause your processor to stall.


Code:
      while(input(PIN_C5))           <--   ';' is missing !!

      ad_in1 = spi_read(0);
To avoid this error it is better to use a coding style like
Code:
      while(input(PIN_C5))
      {
        // wait
      };

      ad_in1 = spi_read(0);
Ttelmah
Guest







PostPosted: Tue Jul 24, 2007 9:04 am     Reply with quote

Double check your wiring. DIN on PIC to DOUT on AD7790, and DOUT on PIC to DIN on 7790. If the lines are 'permanently low', something is very wrong, since you send 0x3C at the start of the loop...
Now, the mode you are trying to use (continuous read, without raising CS), has some restrictions, that may cause problems with the code as shown. If you look at page 16 of the data sheet, in this mode, only 16 clocks should be applied for each read in this mode, since the chip does not clock back a status byte in this mode. You also should check that the DIN line, has been pulled low, before starting the read. Now, I'd guess that the while, testing C5, is meant to give this delay, but because it lacks the ';', the next line will keep being called, resulting in data being clocked before the chip is ready, resetting it. Also, it is C4, that chould be monitored, not C5, and reading from C5, will set it's TRIS to '1', disabling the SPI on this pin(it must be set as an output for SPI operation)...

Best Wishes
spom



Joined: 12 Jul 2007
Posts: 32

View user's profile Send private message

PostPosted: Tue Jul 24, 2007 11:22 am     Reply with quote

Thanks for your help. I made the changes, but nothing positive happened.

Can be something wrong with the PIC's clock? As I want to detect the Voltage of the clock with an osci, I only got a sine wave with 5mV. And that's wrong isn't it? But I tried another PIC and there was the same.

Do you have some other ideas?
Guest








PostPosted: Wed Jul 25, 2007 8:05 am     Reply with quote

Hallo,
I think I need a little more help. My code changed a little:
Code:

#include <18f2480.h>
#device ADC=10

#use delay(clock=8000000)

#fuses NOWDT,HS,NOPUT,NOPROTECT,NOBROWNOUT,NOLVP


#define SPI_MODE_0_0 (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_0_1 (SPI_L_TO_H)
#define SPI_MODE_1_0 (SPI_H_TO_L)
#define SPI_MODE_1_1 (SPI_H_TO_L | SPI_XMIT_L_TO_H)

#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7,BRGH1OK,stream=debug)
#use standard_io(C)


#define SPANNUNG_REF 4
#define KOMPERATOR_REF   0
#define LUFTBLASENSENSOR 1

#define SPI_CS       PIN_C2
#define SPI_CLOCK    PIN_C3
#define SPI_DO       PIN_C4
#define SPI_DIN      PIN_C5

#define CREAD 0x38      // continous read


void main()
{


   int8 ad_in1;
   int8 ad_in2;


   setup_spi(SPI_MASTER | SPI_MODE_1_1 | SPI_CLK_DIV_64);


   SET_TRIS_C( 0x28 );   

   output_high(SPI_CLOCK);


   delay_ms(100);


   while (TRUE)      
   {
      
      delay_ms(20);

      output_low(SPI_CS);
      output_high(SPI_CLOCK);
      output_high(SPI_DO);


      while (!input(SPI_DO))
         {
         //wait SPI_DO high
         };


      spi_write(CREAD);
      while(input(SPI_DO))
         {
         //wait
         };

      
      output_high(SPI_DIN);


      while(!input(SPI_DIN))
         {
         //wait for SPI_DIN is high
         };

   
      ad_in1 = spi_read(0);
      ad_in2 = spi_read(0);
      

      while(!input(SPI_DO))
         {
         //wait for SPI_DO high
         };


      output_high(SPI_CS);
                fprintf(debug,"%u %u ;", ad_in1, ad_in2);
      
   }
}


I did it like it is explained in the datasheet of the AD7790 on page 15.
But the problem is, that the SPI_DIN don't go high. Why?
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