View previous topic :: View next topic |
Author |
Message |
gianni_pancrazi
Joined: 13 Feb 2007 Posts: 4
|
spi_clock |
Posted: Tue Feb 13, 2007 7:23 am |
|
|
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
|
|
Posted: Tue Feb 13, 2007 10:18 am |
|
|
Post the manufacturer and part number of the ADC chip. |
|
|
Ttelmah Guest
|
|
Posted: Tue Feb 13, 2007 11:02 am |
|
|
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
|
Controlling AD7691 |
Posted: Wed Feb 14, 2007 7:41 am |
|
|
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
|
|
Posted: Wed Feb 14, 2007 12:33 pm |
|
|
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
|
ADC Acquisition |
Posted: Thu Feb 15, 2007 10:03 am |
|
|
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?
I have a stupid question
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
|
|
Posted: Thu Feb 15, 2007 1:20 pm |
|
|
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. |
|
|
|