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

Help with LTC2480/81 (16-bit ADC)
Goto page Previous  1, 2, 3, 4  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Aug 07, 2011 5:35 pm     Reply with quote

Yes, that's the way spi_xfer() is supposed to be used.

If you don't see the SCK clock on the PIC pin, then do this test:

1. Disconnect the SCK wire between the PIC and the LTC2480.

2. Make a very simple test program with a while loop. In it, you should
do continuous spi_xfer() operations. (Just one line inside the loop. Or
maybe one line, and a short delay of a few usec, to provide separation
so you can see the groups of 8 clocks on your scope.)
And don't use structures in the test program. Just use a simple variable
for the return value from spi_xfer().

3. Run it and look at the SCK pin on the PIC with your scope.
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

PostPosted: Mon Aug 08, 2011 1:39 am     Reply with quote

OK,

Thanks PCM,
I'll give it a go

Carl
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

PostPosted: Mon Aug 08, 2011 4:10 am     Reply with quote

OK,

The clk signal is present 3 x 8bit pulses.
And I am getting some numbers out of the read adc. (but they don't make much sense at the moment 255xx - and it keeps gradually increasing with time upto 26xxx - which is worrying...)

Code:

x = read_LTC2480();                     // Read ADC
printf(lcd_putc, "\f %Ld\n" x);
delay_ms(500);
if(x!=0)



So apart from the above I now need to play with the maths a bit - understand how to read and extract the 16 bit number from the 32 bits (I tried RF_Developer's code - but this returned a smaller number (8 bit)). The returned result should be 65535 with 5V on the +IN input. I do not undersatnd the 2's complement connection either so will read up on it - From what I have seen it modifys the code to include negative numbers - but I don't really need this at the moment.

Thanks
Carl
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

PostPosted: Mon Aug 08, 2011 4:26 am     Reply with quote

I tried this but it returns with a smaller number. I don't quite understand because I am only trying to return the 16 bits of code that I require - by disregarding the first four bits by the right shift and then 'bitwise &' to only include the bits I require. So the output does not seem to be behaving correctly.

Code:


return (unsigned int)((adc_code.bits32 >> 4) & 0xFFFF);
}

//*******************************************************************************/
void main(){
unsigned int x;                                 // Integer result from LTC2480
float voltage;                         // Variable for floating point math
int timeout;
initialize();                          // Hardware initialization
while(1){         
delay_ms(1);                          // Pace the main loop to something more than 1 ms
                                 // This is a basic error detection scheme. The LTC2485 will never take more than
                                 // 163.5ms, 149.9ms, or 136.5ms to complete a conversion in the 50Hz, 55Hz, and 60Hz
                                 // rejection modes, respectively.
                                 // If read_LTC2485() does not return non-zero within this time period, something
                                 // is wrong.

x = read_LTC2480();                     // Read ADC
printf(lcd_putc, "\f %u\n" x);
delay_ms(500);


The output displayed 110 and then stated reducing.
Any thoughts on how I can start fault finding to read the 16 bits.

Thanks
Carl
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

PostPosted: Mon Aug 08, 2011 4:59 am     Reply with quote

So this is my code at the moment
Code:

#include <16F628A.h>           // Device       
#include <flex_lcd.c>          // Rohs Flex LCD
#fuses INTRC_IO, NOWDT, NOPROTECT, NOPUT, NOBROWNOUT, NOLVP, DEBUG,
#use delay(clock=4000000)
#use standard_io(a)     
#use standard_io(b)
#use spi(MASTER, MODE=0, CLK=PIN_A0, DO=PIN_A1, DI=PIN_A7, BITS=8, MSB_FIRST)
#define SPI_MODE_0  (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_1  (SPI_L_TO_H)
#define SPI_MODE_2  (SPI_H_TO_L)
#define SPI_MODE_3  (SPI_H_TO_L | SPI_XMIT_L_TO_H)
#define CS PIN_A6              // Chip Select
// char config = 0b11110000;   // External Input, Gain = 256, Autocalibration, Simultaneous 50Hz/60Hz Rejection, Slow Speed
char config = 0b10000000;      // External Input, Gain = 1, Autocalibration, Simultaneous 50Hz/60Hz Rejection, Slow Speed


void initialize(void){      // General initialization stuff.   
output_low(LCD_RW);       // Set R/W pin on LCD to a low level                   
lcd_init();                 // Initialize LCD
lcd_putc("\fHello\n");      // Obligatory hello message
delay_ms(5000);             // for half a second
}                            // End of initialize()

//*******************************************************************************/

unsigned int read_LTC2480()
{
   int32 adc_code;                        // Raw adc data
   int8 te0, te1, te2;   
   output_low(CS);                        // Enable LTC2480 SPI interface
   
   while(input(PIN_A7)) {}                // Wait for end of conversion. The longest
                                          // you will ever wait is one whole conversion period
                                          // Now is the time to switch any multiplexers because the conversion is finished
                                          // and you have the whole data output time for things to settle.                                               
   te2 = spi_xfer(config);                // Read first byte, send config byte
   te1 = spi_xfer(0);                     // Read 2nd byte, send speed bit
   te0 = spi_xfer(0);                     // Read 3rd byte. ‘0’ argument is necessary
                                          // to act as SPI master!! (compiler
                                          // and processor specific.)
                                                   
   output_high(CS);                       // Disable LTC2480 SPI interfac
                   
   adc_code = make32(0, te2, te1, te0);   // Reassemble the full code word.
// adc_code = ((int32)te2 << 16) | ((int32)te1 << 8) | te0;  // Alternative version.
   
   // Deal with errors, e.g. overflow, in here by interpreting status bits.
   
   return (unsigned int)((adc_code >> 4) & 0xFFFF); // Return 16 bit data portion of adc code.
}                                     // End of read_LTC2480()

//*******************************************************************************/
void main(){

unsigned int x;                      // Integer result from LTC2480
initialize();                          // Hardware initialization
while(1){         
delay_ms(1);                          // Pace the main loop to something more than 1 ms
                                 // This is a basic error detection scheme. The LTC2485 will never take more than
                                 // 163.5ms, 149.9ms, or 136.5ms to complete a conversion in the 50Hz, 55Hz, and 60Hz
                                 // rejection modes, respectively.
                                 // If read_LTC2485() does not return non-zero within this time period, something
                                 // is wrong.
x = read_LTC2480();                     // Read ADC
printf(lcd_putc, "\f %u\n" x);
delay_ms(500);
}
}


And the output displays thress digits 115 - which fluctuautes but generally is decreasing in value.

Quote:
A slight change in the code - but it didn't make any difference. DO is now on pin A1 not A7.
Code:


while(input(PIN_A1)) {}               
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

PostPosted: Mon Aug 08, 2011 9:36 am     Reply with quote

Hi PCM,

It looks as though something is seriously wrong.
I disconnected the +IN and -IN from the ADC inputs and the output was still more or less the same varying around '110' on the display. So basically it makes no difference if the inputs are disconnected or at max 0V and 5V - the ouputs are the same - as though it is just floating.

I must have it configured incorrectly. Below is a link to the waveforms.

www.encoderonline.com/UK/clkanddo.gif
www.encoderonline.com/UK/clkandi.gif

Channel A is 'CLK' and B is 'SDI and SDO respectively'. One thing that looks strange is the SDO waveform (pin 20, A1 from PIC - which is connected to pin1, SDI on ADC) - just high for one pulse - is this how it's supposed to be?

I can't see how either of these pins can be used for this part of the code
Code:

while(input(PIN_A7)) {}

Because this wouldn't make sense when looking at the waveforms...
Even if it was
Code:

while(input(PIN_A1)) {}             

Any thoughts?
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

PostPosted: Mon Aug 08, 2011 3:41 pm     Reply with quote

Hi PCM,

Sorry to keep bothering you, but can you help any further please.

Thanks
Carl
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Aug 08, 2011 3:58 pm     Reply with quote

The code you posted is not simple enough. For example, you still have
the hardware SPI #define statements in there, and they are of no use
because the 16F628A doesn't have hardware SPI. Those 4 statements
just clutter up the program.

Options for a more simple test program:
The LTC2480 data sheet tells how to configure the chip for a simple
interface that doesn't even involve sending data to the chip at all.
You only read from it.
Or, as another option, it can be configured as a temperature sensor.
That might be an easier thing to test, since you don't have to worry
about setting the input voltage level with an external circuit.

I will try to make a very simple program and post it for you to try.
It might be several hours from now (on the high side of that).
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

PostPosted: Mon Aug 08, 2011 4:19 pm     Reply with quote

Thats fine, thanks for all your help.
The only code I can see on the datasheets is very similar to my earlier postings - but it does also write (with the config) variable.

In regards to the links though - what do you think of the waveforms?
Also why, when I try both ways of writing the read program do I get very big differences?

I mean if it is written like the Suggested code by RF Developer I posted last
Code:

return (unsigned int)((adc_code >> 4) & 0xFFFF)

then I get a three digit number on the display.

But if I use the original code below then I get something very different - a five digit number.
Code:
adc_code.by.te0 = adc_code.by.te0 & 0xF0;
adc_code.bits32 = adc_code.bits32 - 0x00200000;
return adc_code.bits32;


Regardless though, both attempts are not stable, so I do not think it is the code (even though both of them should do the same thing - I think).

Thanks for your help.
Carl
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Mon Aug 08, 2011 4:27 pm     Reply with quote

re the !CS toggle

i'm kind of fond of the data access plan shown in fig 7
of the part data sheet,
and second PCM's suggestion to do a
bit of KISS work and just try to read the temperature first.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Aug 08, 2011 4:54 pm     Reply with quote

Regarding the first scope image:
Because you had a floating input when you read that data, there's no way
to correlate it to any input voltage level. So there's no point in analyzing it.

Re the 2nd scope image:
With regard to the PIC's output data having only the first bit high, that's
exactly what you're sending to the LTC2480. You first send the 'config'
variable, and it's set to 0x80. Then you do two spi_read() operations
with a parameter of 0x00. That's 0x80 0x00 0x00, which is exactly what
you see on the scope.
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

PostPosted: Tue Aug 09, 2011 1:40 am     Reply with quote

I see - its the config variable, understood now.
I will continue looking at it today, but if you could possibly have a look at a simpler test program that would be excellent.

Thanks
Carl
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

PostPosted: Tue Aug 09, 2011 9:31 am     Reply with quote

Hi PCM,

Mixed outcome today - but I still need your help.
The information below may be completely 'muddying the waters' but it is still interesting and I think worth considering.

I thought before going any further to just try and tieing the input of the ADC to the true application - a load cell (bridge type), and it actually gave out something that resembles a linear output. The load cell has previously been overloaded - so is not exactly linear, but it is good enough to 'get an indication' for coding purposes.

at 'zero' weight it gave out '320' on the display, at 'max' weight (10KG) it gave out 6624 - and at half weight (5KG) it gave 3488. So the output is relatively linear.

The load cell is 1.5mV/V with a 5V supply - which gives a FS of +/- 3.75mV. The common voltage is 2.5V. So when at minimum, the voltage difference between the two inputs is more or less 0V. And at FS we get a 7.5mV difference.

So taking the 'faulty offset' into account it is actually reading and outputting something that resembles what I need.

I used the original code for this (with union), and not the simplified code (make 32). This is because if I use the make 32 code - it gives out a much smaller number.

I don't know if the above observations today categorically show that the hardware is correct, so I will still need a tester code from you still if possible.

But continuing with my progress, I am trying to change the 'config' command from x1 to x256 (so to scale the output nearer to FS on the datasheet for 'x256 = +/-9.76mV page16'). But it doesn't matter if I type 0b10000000 or 0b11110000 - it makes no difference to the output.

So why is the config command not altering anything? When I check it with the scope the signal does change and correctly corresponds to what I set it to.

Carl
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Tue Aug 09, 2011 11:47 am     Reply with quote

so in KISS land - there is a simple way to read just temperature but it requires sending IN SDI data .
if this was MY project - an early step for ME would have been to heed this bit of inf:

Quote:

The LTC2480 powers up in a default mode commonly
used for most measurements. The device will remain in
this mode as long as the serial data input (SDI) is low.
In this default mode, the measured input is external, the
GAIN is 1, the digital filter simultaneously rejects 50Hz
and 60Hz line frequency noise, and the speed mode is 1x


and per Fig 7 of the datasheet coupled with the above -
you are only using CLOCK and SDO to get data in a known
configuration. i would go no further until i was confident that
'babystep' mode was a-OK

me thinks a lot could be learned with a lot less code than you are chasing
iust now.
just another 2 c worth
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

PostPosted: Tue Aug 09, 2011 1:56 pm     Reply with quote

Hi ASM,

I did look at both your and PCM Programmer replies and tried the temperature sensor settings - 1b11111000 in the 'config' command, but I couldn't work out two things:

1) How the value is read (it does not clearly state this in the dadatsheet) - I assumed it would be the same format - a 24 bit code manipulated into 16 bits.

2) How to make the code into a corrsponding readable temperature - it looks complicated (Pages 19 & 20).
Quote:
When using the internal temperature sensor, if the output
code is normalized to RSDO = VPTAT/VREF , the temperature
is calculated using the following formula:
T
R V
SLOPE
T
R V
SLOPE
K
SDO REF
C
SDO REF
=
= °
•
•
–
in Kelvin
and
273 in C
where SLOPE is nominally 1.4mV/°C.


Carl
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2, 3, 4  Next
Page 2 of 4

 
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