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

ltc1298.c
Goto page 1, 2, 3  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Guest_Me
Guest







ltc1298.c
PostPosted: Mon Jan 24, 2005 12:06 pm     Reply with quote

hi can anyone help me please, can you please commentate the following codes? everything works now. im now able to read the voltage and hex via rs232. but i need to understand what the codes exactly do. thank you very much.

Driver for LTC1298

Code:

//////////////////////////////////////////////////////////////////////////
////        (C) Copyright 1996,2003 Custom Computer Services           ////
//// This source code may only be used by licensed users of the CCS C  ////
//// compiler.  This source code may only be distributed to other      ////
//// licensed users of the CCS C compiler.  No other use, reproduction ////
//// or distribution is permitted without written permission.          ////
//// Derivative programs created using this software in object code    ////
//// form are not restricted in any way.                               ////
///////////////////////////////////////////////////////////////////////////

#ifndef ADC_CS

#define ADC_CLK  PIN_B0
#define ADC_DOUT PIN_B1
#define ADC_DIN  PIN_B2
#define ADC_CS   PIN_B3

#endif



void adc_init() {
   output_high(ADC_CS);
}

void write_adc_byte(BYTE data_byte, BYTE number_of_bits) {
   BYTE i;

   delay_us(2);
   for(i=0; i<number_of_bits; ++i) {
      if((data_byte & 1)==0)
        output_low(ADC_DIN);
      else
        output_high(ADC_DIN);
      data_byte=data_byte>>1;
      output_high(ADC_CLK);
      delay_us(50);
      output_low(ADC_CLK);
      delay_us(50);
   }
}


BYTE read_adc_byte(BYTE number_of_bits) {
   BYTE i,data;

   data=0;
   for(i=0;i<number_of_bits;++i) {
      output_high(ADC_CLK);
      delay_us(50);
      shift_left(&data,1,input(ADC_DOUT));
      output_low(ADC_CLK);
      delay_us(50);
   }
   return(data);
}


long int read_analog( BYTE channel ) {
   int l;
   long int h;

   delay_us(200);

   output_low(ADC_CLK);
   output_high(ADC_DIN);
   output_low(ADC_CS);

   if(channel==0)
     channel=0x1b;
   else
     channel=0x1f;
   write_adc_byte( channel, 5);

   h=read_adc_byte(8);
   l=read_adc_byte(4)<<4;
   output_high(ADC_CS);
   return((h<<8)|l);
}

void convert_to_volts( long int data, char volts[6]) {
   BYTE i;
   long int temp,div;

   div=0x3330;

   for(i=0;i<=4;i++) {
     temp=data/div;
     volts[i]=(BYTE)temp+'0';
     if(i==0) {
       volts[1]='.';
       i++;
     }
     temp=div*(BYTE)temp;
     data=data-temp;
     div=div/10;
   }
   volts[i]='\0';
}



the EX_AD12.c

Code:

#if defined(__PCB__)
#include <16c56.h>
#fuses HS,NOWDT,NOPROTECT
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_A3, rcv=PIN_A2)  // Jumpers: 11 to 17, 12 to 18

#elif defined(__PCM__)
#include <16F877.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

#elif defined(__PCH__)
#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
#endif

#include <ltc1298.c>


void display_data( long int data ) {
     char volt_string[6];

     convert_to_volts( data, volt_string );
     printf(volt_string);
     printf(" (%4lX)",data);
}


void main() {
   long int value;

   adc_init();

   printf("Sampling:\r\n");

   do {
      delay_ms(1000);

      value = read_analog(0);
      printf("\n\rCh0: ");
      display_data( value );

      value = read_analog(1);
      printf("   Ch1: ");
      display_data( value );

   } while (TRUE);

}


thanks...
rwyoung



Joined: 12 Nov 2003
Posts: 563
Location: Lawrence, KS USA

View user's profile Send private message Send e-mail

PostPosted: Mon Jan 24, 2005 1:51 pm     Reply with quote

Have you read the LTC1298 data sheet? Question

The PIC is selecting the LTC1298 via the ADC_CS chip select pin which is active low. The PIC must select the ADC prior to writing/reading from the SPI pins. Refer to the LTC1298 datasheet.

When the PIC needs data from the LTC1298 it first sends a control word (either 0x1B or 0x1F) depending on the desired channel to be sampled. Then data is clocked back out of the LTC1298, 16 bits worth. Refer to the LTC1298 data sheet for more about its control words and operating modes.

There are calls to "delay_us" interspersed in the code to allow sufficient time for the data line relative to either the clock or chip select signals. Refer to the LTC1298 data sheet for more information. You can alter those values depending on your PIC's clock speed.

The function "convert_to_volts" is taking the 16-bit word returned from the ADC and is scaling it to a string representation of a floating poitn number.

By the way, did you read the header on the file "ltc1298.c"? A strict interpretation suggest you should not have posted the file in its entirety to the forum without permission from CCS. Not every person reading the CCS forum has a licensed copy of the compiler. Probably not that big a deal for such a simple driver...
_________________
Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month!
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Mon Jan 24, 2005 2:20 pm     Reply with quote

I believe he would like for us to comment every line of code so that he can understand what is going on. As rwyoung suggests, read the datasheet for the LTC1298 and the PIC and then ask specific questions about the parts that you don't understand.
Guest_Me
Guest







PostPosted: Tue Jan 25, 2005 10:51 am     Reply with quote

hi 2 all, thanks for the answers.

Quote:
Have you read the LTC1298 data sheet?


I read the data sheet. i think thats the first thing i do before starting a project. so i already know how to activate the chip.

Quote:

The function "convert_to_volts" is taking the 16-bit word returned from the ADC and is scaling it to a string representation of a floating poitn number.


i think its a 12-bit conversion, isnt it?

Quote:

I believe he would like for us to comment every line of code so that he can understand what is going on. As rwyoung suggests, read the datasheet for the LTC1298 and the PIC and then ask specific questions about the parts that you don't understand.


thats exactly i want, is it possible to you guys. to explain or to commentate the code?


thanks
rwyoung



Joined: 12 Nov 2003
Posts: 563
Location: Lawrence, KS USA

View user's profile Send private message Send e-mail

PostPosted: Tue Jan 25, 2005 11:10 am     Reply with quote

Guest_Me wrote:

i think its a 12-bit conversion, isnt it?


Glad to hear you read the LT1298 datasheet. Not enough people read the data sheet first... Smile

Yes the converter is returning 12 bits but the function is returning it as a 16-bit integer, justified to the MSB of the integer.

If you are having trouble understanding C or are trying to learn C, go get yourself a copy of Kernigahn and Ritchie's "The C Language", 2nd edition. That book will ground you in the "pure" form of C. Then read the CCS manual (get the latest edition from the CCS website) and read that to understand the built-in functions. Also read the "readme.txt" that came with your compiler because it has the latest information, especially about new functions that have not made it into the CCS manual. CSS seems to be a bit slow about updating their manuals.

Another good way to understand code is to "play" computer and execute the code yourself on a piece of paper. Or if you have access to Microsoft Visual C or can download something like Dev-C++/Bloodshed and the Insight GDB debugger you can re-enter "convert_to_volts" and use the debugger on your PC to single-step through the code watching all the variables.
_________________
Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month!
Guest_Me
Guest







PostPosted: Tue Jan 25, 2005 11:37 am     Reply with quote

I just need to understand the code, because what i want to do is, to get the conversion in decimal, and now in HEX format.

thanks.
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Tue Jan 25, 2005 11:45 am     Reply with quote

Guest_Me wrote:
I just need to understand the code, because what i want to do is, to get the conversion in decimal, and now in HEX format.

thanks.


Research the format specifiers for this
Code:
     printf(" (%4lX)",data);
Guest_Me
Guest







PostPosted: Tue Jan 25, 2005 11:48 am     Reply with quote

i knew it has to something with that line =)
i already search in the internet, but ive found nothing..
rwyoung



Joined: 12 Nov 2003
Posts: 563
Location: Lawrence, KS USA

View user's profile Send private message Send e-mail

PostPosted: Tue Jan 25, 2005 1:00 pm     Reply with quote

Guest_Me wrote:
i knew it has to something with that line =)
i already search in the internet, but ive found nothing..


Didn't look hard enough... Cool

http://www.opengroup.org/onlinepubs/009695399/functions/printf.html

4th entry when I googled "printf". Also, go get the Kernigahn and Ritchie book. It will pay off in the long run.
http://www.amazon.com/exec/obidos/tg/detail/-/0131103628/qid=1106679568/sr=8-1/ref=sr_8_xs_ap_i1_xgl14/002-7182543-6496038?v=glance&s=books&n=507846
Depending on where you are in the world, as little as $US22 plus shipping will get you a copy.
_________________
Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jan 25, 2005 1:01 pm     Reply with quote

You need to use http://www.google.com to search for printf.
You'll find a page like this:
http://crasseux.com/books/ctutorial/Formatted-output-conversion-specifiers.html
Don't expect people on this board to teach you the C language.
The reason is because the people on this board don't really want to teach
the C language to beginners. We want to solve problems concerning
the CCS compiler and other embedded software or hardware issues.
Guest_Me
Guest







PostPosted: Tue Jan 25, 2005 2:34 pm     Reply with quote

well you people might think that im a computer illiterate, and im not able to use google. surely i google first before i ask in this forum...
i thought you could help me, by simply commentate the codes..
well thanks anyway.
gudnyt
rwyoung



Joined: 12 Nov 2003
Posts: 563
Location: Lawrence, KS USA

View user's profile Send private message Send e-mail

PostPosted: Tue Jan 25, 2005 3:21 pm     Reply with quote

Guest_Me wrote:
i knew it has to something with that line =)
i already search in the internet, but ive found nothing..


Also from the CCS help file (should be in the PDF manual too)

Code:
Format:
The format takes the generic form   %wt where w is optional and may be 1-9 to specify how many characters are to be outputted, or 01-09 to indicate leading zeros or 1.1 to 9.9 for floating point.  t is the type and may be one of the following:

C   Character
S   String or character
U   Unsigned int
x   Hex int (lower case output)
X   Hex int (upper case output)
D   Signed int
e   Float in exp format
f   Float
Lx   Hex long int (lower case)
LX   Hex long int (upper case)
lu   unsigned decimal long
ld   signed decimal long
%   Just a %


You ask us to comment on what is fairly straight forward code. If you had asked for specific information as in your later post where you asked about "printf" and the format qualifier, then you would have gotten a straight answer and not RTFM (which by the way I have restrained myself with great effort from saying until now) Surprised .

If you really did use Google why didn't you read the first 3 or 4 web pages it gave as links? The 4th one (the one I added to a post above) is from the IEEE and has CONSIDERABLE detail on "printf" and format qualifiers.

If you need help understanding what the various operators mean (<<, >>, +, -, ==, etc) or how to interpret standard C I will say it one more time, go get a copy of Kernighan and Ritchie's book. Borrow a copy or see if a library can find you a copy. Even a brand new copy is a worthy investment. Nearly every other C language book I've seen makes refernece to this one. It is a primary source. You cannot go wrong by reading it. It will be nearly as good for you as eating your vegatables! Wink
_________________
Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month!
Guest_Me
Guest







PostPosted: Tue Jan 25, 2005 3:33 pm     Reply with quote

im familiar with C++
and i know the basic programming...
i found it already on the datasheet (printf) myself... and now i know why is not going..because my ccs compiler is out of date i only have the version 3.178.. and there was a bug concerning the printf..
Code:
3.212  A %u and %d bug is fixed


thanks..
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Tue Jan 25, 2005 3:54 pm     Reply with quote

Quote:
im familiar with C++
and i know the basic programming...


Then printf should be a question to you. BTW, ints are 8 bit in CCS and the data is a long int so you should be using %lu or %ld but you probably already figured this out from reading the manual.
rwyoung



Joined: 12 Nov 2003
Posts: 563
Location: Lawrence, KS USA

View user's profile Send private message Send e-mail

PostPosted: Tue Jan 25, 2005 4:10 pm     Reply with quote

Guest_Me wrote:
because my ccs compiler is out of date i only have the version 3.178.. and there was a bug concerning the printf..
Code:
3.212  A %u and %d bug is fixed


thanks..


This brings up an important point. Something we should have asked you very early is "What compiler version are you using?".

For future posts, go ahead and make reference to your compiler version and the particular PIC chip you are using or planning to use when you need help. That makes a big difference in the solution to "odd" behaviour problems.

PCM Programmer in particular seems to have every version ever released and seems have memorized the entire history of bugs and fixes!
_________________
Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month!
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 1, 2, 3  Next
Page 1 of 3

 
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