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_clock

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



Joined: 13 Feb 2007
Posts: 4

View user's profile Send private message

spi_clock
PostPosted: Tue Feb 13, 2007 7:23 am     Reply with quote

I have to get an output from a 18 bit ADC converter working with SPI-Bus. I'm writing my Program with a CCS-C compiler and I'm working with a PICF4620. I have not a great experience with PIC programming. I would like to acquire the data with a "spi_read()" Function. SCKL (PIN_C3) is my serial clock.

How does the ADc get the serial clock?
Should I use the standard command line, like follow:
Code:
 setup_spi (spi_master |spi_l_to_h | spi_clk_div_16 )

or better to write a my own clock program like:
Code:
output_toggle(SCKL);


Thank u for your Help
Andrea
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Feb 13, 2007 10:18 am     Reply with quote

Post the manufacturer and part number of the ADC chip.
Ttelmah
Guest







PostPosted: Tue Feb 13, 2007 11:02 am     Reply with quote

On the second part of the question, the hardware SPI, is faster, and easier to use. The send a byte using software toggling, is hard work...

Best Wishes
gianni_pancrazi



Joined: 13 Feb 2007
Posts: 4

View user's profile Send private message

Controlling AD7691
PostPosted: Wed Feb 14, 2007 7:41 am     Reply with quote

I'm using a differential ADC, from Analog Devices (AD7691). The configuration used is a 4-Wire CS Mode with Busy Indicator (see page 21/28 of the datasheet). With a pull-up on the SDO line, I can get an interrupt signal to initiate the data readback controlled by the PIC. For readback I'm using this code:


Code:
#include<Header.h>
#device *=16
#use delay(clock=16000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#define LED1 PIN_B2
#define LED2 PIN_B3
#define SDI    PIN_C4
#define SDO  PIN_C5
#define SCKL PIN_C3
#define CNV  PIN_A4

int16 x=0;
int a;
int i;


void main()
{
setup_spi(spi_master | spi_l_to_h | spi_clk_div_16 );

delay_ms(500);
output_low(SCKL);
delay_ms(500);
output_high(SDO);
delay_ms(500);
output_high(CNV);
delay_ms(500);
output_low(SDO);
delay_us(0.4);

while (TRUE)   
 {   a=spi_read();
   if (a==0)
      {
            for (i=0;i<=17;++i)
            {
               output_high(SCKL);
               delay_us(10);
               output_low(SCKL);
               if( spi_data_is_in()) bit_set(x,(i));
               delay_us(10);
               
            }
                  printf("\n %x", x );
      }
 }
   
} // main() END


The problem is, that I don't get a right result in conversion of input voltage. Do you have any suggestion?
Thank you very much for your help.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Feb 14, 2007 12:33 pm     Reply with quote

You appear to be mixing hardware and software SPI. That's wrong.
You should use one or the other, but don't mix them.
gianni_pancrazi



Joined: 13 Feb 2007
Posts: 4

View user's profile Send private message

ADC Acquisition
PostPosted: Thu Feb 15, 2007 10:03 am     Reply with quote

PCM programmer wrote:
You appear to be mixing hardware and software SPI. That's wrong.
You should use one or the other, but don't mix them.


What are u meaning with mixing Hardware and Software? Could u be more explicit? Question

I have a stupid question Embarassed
Do I have to manually turn the clock up and down, during acquisition of ADC output? Like:

Code:
for(i=18;i>0;--i)
               {   
               output_low(SCKL);
               output_high(SCKL);
               if( spi_data_is_in()) bit_set(x,(i));
               delay_cycles(1);
               }

Or the SPI function
Code:
setup_spi(SPI_MASTER|SPI_L_TO_H|SPI_CLK_DIV_4);

will provide the CLK automatically?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Feb 15, 2007 1:20 pm     Reply with quote

If you use the hardware SPI module, you can do a read operation
and the hardware module will generate the clock for you.

Code:
int8 c;

c = spi_read(0);

This code will send a byte of 0x00 on the SDO pin, and a clock
will be generated on the SCLK pin to do this. At the same time,
the incoming data from the SPI slave will be clocked in on the
SDI pin of the PIC.

The code shown above is the standard way to generate a clock
and read a byte from an SPI slave, while using the hardware SPI
module in the PIC.
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