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 Setup for Linear Tech 16 Bit ADC LTC1864

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



Joined: 08 Sep 2003
Posts: 5

View user's profile Send private message

SPI Setup for Linear Tech 16 Bit ADC LTC1864
PostPosted: Mon May 16, 2005 10:41 pm     Reply with quote

Hi, all. I was experimenting on LTC1864, particularly on how to setup my PIC18F452 (as spi master) to read the 16 bit data. Here are my codes:

Code:

#include <18F452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)  // Jumpers: 8 to 11, 7 to 12

#include <input.c>
#include "LTC1864SPI.c"


void main() {

   int16 dummydata;
   int8 count=1;

   init_ext_adc();

   do
   {
   dummydata= read_ext_adc();
   delay_us(50);
   count++;
   } while (count <=10);
  while(1);

}


and the driver file


Code:

#define ADC_CONV      PIN_D7
#define ADC_DO        PIN_C4   // SDI
#define ADC_DI        PIN_C5   // SDO
#define ADC_CLK       PIN_C3

#bit CKP_BIT=0xFC6.4
#bit CKE_BIT=0xFC7.6
#bit SMP_BIT=0xFC7.7


void init_ext_adc() {
   short int i;

   //output_low(ADC_DI);
   //output_high(ADC_CLK);
   output_low(ADC_CONV);
   i=input(ADC_DO);

   setup_spi(SPI_MASTER|SPI_H_TO_L|SPI_CLK_DIV_16|SPI_SS_DISABLED); //
   CKP_BIT=1;    //SCK IDLE=HIGH
   CKE_BIT=0;
   //SMP_BIT=1;

}



int16 read_ext_adc(void) {
   BYTE data_hi,data_lo,dummy;
   int16 data;

   output_high(ADC_CONV);
   delay_us(10);
   output_low(ADC_CONV);
   data_hi=spi_read(0);
   data_lo=spi_read(0);
   dummy=spi_read(0);  // SCK is supplied while Tconv=LOW=> ADC outputs ZEROs
   data=make16(data_hi,data_lo);
   return(data);
}


My questions:
[1] I tried with the CKP/CKE combination of 1,0 & 1,1. I cannot tell any difference on the SPI Data with respect to SCK clock on my scope. In actual case, what should be the correct setting? LTC1864 Datasheet timing diagram shows that the data changes on falling edges of SCK, so should I use CKP=1, CKE=0?

[2] Any concern about setting of SMP bit in the SSPSTAT register?

Warmest Regards
lguancho

Many thanks
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue May 17, 2005 3:05 pm     Reply with quote

Look at Figure 1 in the LTC1864 data sheet (page 9) and compare
it to the diagrams of the SPI modes shown in the following link:
http://www.totalphase.com/docs/articles/article03/#modes
The data sheet shows that the idle state of the clock is a high level,
and that it samples the data on the rising edge of clock.
In the link, this is shown as SPI mode 3. The following chart
shows the settings for the CKP and CKE bits for each SPI mode.
Quote:

SPI mode 0: CKP = 0 and CKE = 1
SPI mode 1: CKP = 0 and CKE = 0
SPI mode 2: CKP = 1 and CKE = 1
SPI mode 3: CKP = 1 and CKE = 0
ckielstra



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

View user's profile Send private message

PostPosted: Tue May 17, 2005 3:54 pm     Reply with quote

As additional info, I always add the following to my code:
Code:
// Added SPI Mode defines because CCS and Microchip made a very confusing mess of it.
//
//     MOTOROLA              MICROCHIP                 CCS
//---------------------------------------------------------------------------------
//   SPI Mode 0,0   ==    CKP = 0, CKE = 1   ==   SPI_L_TO_H | SPI_XMIT_L_TO_H
//   SPI Mode 0,1   ==    CKP = 0, CKE = 0   ==   SPI_L_TO_H
//   SPI Mode 1,0   ==    CKP = 1, CKE = 1   ==   SPI_H_TO_L
//   SPI Mode 1,1   ==    CKP = 1, CKE = 0   ==   SPI_H_TO_L | SPI_XMIT_L_TO_H
// Example: setup_spi(SPI_MASTER | SPI_MODE_0_0 | SPI_CLK_DIV_4 );
#define SPI_MODE_0_0  0x4000
#define SPI_MODE_0_1  0x0000
#define SPI_MODE_1_0  0x0010
#define SPI_MODE_1_1  0x4010
lguancho



Joined: 08 Sep 2003
Posts: 5

View user's profile Send private message

PostPosted: Tue May 17, 2005 10:30 pm     Reply with quote

Thanks, PCM Programmer and Ckielstra!. I will implement the spi modes as recommended.

About this LTC1864, I find that if I just do two spi-read, the SDATA line will just toogle between HIGH and LOW after the 16 bit data transfer. If I do another spi_read (third ones) while holding the CONV of LTC1864, SDATA line will stay at a nice LOW!!. In fact, this is exactly as per described in the datasheet! Missing this third SPI-READ will cause my code to go astray!

But anyone encounter this kind of issues?
guest
Guest







PLease help me on LTC1864 & 16F877A
PostPosted: Thu Jul 14, 2005 5:25 pm     Reply with quote

I modify lguancho's code to use LTC1864 with 16F877a. ADC reading is 0. I have verified the clock (C3), and CONV(D7), but just no signal on DI (C4). Can someone tell me what is wrong?

#include <16f877a.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

#define ADC_CONV PIN_D7
#define ADC_DO PIN_C4 // SDI
#define ADC_DI PIN_C5 // SDO
#define ADC_CLK PIN_C3

#bit CKP_BIT=0x14.4
#bit CKE_BIT=0x94.6
#bit SMP_BIT=0x94.7

void init_ext_adc();
int16 read_ext_adc(void);

void main() {

int16 dummydata;
output_high(PIN_C0);

init_ext_adc();

while(1)
{
dummydata= read_ext_adc();
printf("ADC=%lu\r\n", dummydata);
delay_us(50);
}
}

void init_ext_adc() {
output_low(ADC_CONV);

setup_spi(SPI_MASTER|SPI_H_TO_L|SPI_CLK_DIV_16|SPI_SS_DISABLED);
CKP_BIT=1; //SCK IDLE=HIGH
CKE_BIT=0;
}

int16 read_ext_adc(void) {
BYTE data_hi,data_lo,dummy;
int16 data;

output_high(ADC_CONV);
delay_us(10);
output_low(ADC_CONV);
data_hi=spi_read(0);
data_lo=spi_read(0);
dummy=spi_read(0);
data=make16(data_hi,data_lo);
return(data);
}
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