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

adding values to a text string

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



Joined: 29 Jul 2009
Posts: 154
Location: at work

View user's profile Send private message MSN Messenger

adding values to a text string
PostPosted: Wed Aug 11, 2010 10:06 pm     Reply with quote

I have a basic question,

I'm trying to read the inputs of 3 ADC pins on my PIC16F684, and save them all to one text string that will then be sent to an LCD using RS232 communication. My question is, it there a way to add values to a text string?

For example, this is what I want to do:

Code:

set_adc_channel(1);
VAR=read_adc(); //let's pretend ADC reads 103
//ADD VAR to text string, followed by a comma. Now text string should read "103,"

set_adc_channel(2);
VAR=read_adc(); //let's pretend ADC reads 244
//ADD VAR to text string, followed by a comma. Now text string should read "103,244,"

set_adc_channel(3);
VAR=read_adc(); //let's pretend ADC reads 77
//ADD VAR to text string, followed by a comma. Now text string should read "103,244,77,"

putc("text string"); //send text string to LCD via RS232


So the LCD should receive "103,244,77,". Is this possible? I want to use my PIC as a data logger, but I don't want to create an identifier for every value being recorded. I would prefer to save all recorded values in a CHAR array that can be sent to the LCD in one 'putc' command.

The code, when finished, will record data from the ADC inputs 5 times per second for 5 seconds, so it would take 25 identifiers to accomplish this, and that's just a sloppy way to do this.

Thanks!
_________________
Vinnie Ryan
Audi80



Joined: 07 Sep 2007
Posts: 41

View user's profile Send private message

PostPosted: Thu Aug 12, 2010 6:46 am     Reply with quote

Hi there, the simplest thing to do is use strcat

Ex:

Code:

char completeString[100];

void addValue(char * newAdcValue){
    strcat(completeString,newAdcValue);
    printf("Whole new String: %s\r\n",completeString);

}



Test it and see if it works, best regards...
ckielstra



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

View user's profile Send private message

PostPosted: Thu Aug 12, 2010 6:55 am     Reply with quote

I don't understand why you can't write each part to the LCD separately.

Instead of strcat you could use printf:
Code:
set_adc_channel(1);
var1=read_adc(); //let's pretend ADC reads 103

set_adc_channel(2);
var2=read_adc(); //let's pretend ADC reads 244

set_adc_channel(3);
var3=read_adc(); //let's pretend ADC reads 77

printf("%d,%d,%d,", var1, var2, var3);  //send text string to LCD via RS232
Ttelmah



Joined: 11 Mar 2010
Posts: 19550

View user's profile Send private message

PostPosted: Thu Aug 12, 2010 7:55 am     Reply with quote

A series of comments:
1) The ability to send a multi-character string with putc, _only_ applies to constant strings. The string will need to be printed out a character at a time, or sent with puts/printf.
2) As another poster has said, why not just printf the values. If you are worried about timing, consider using buffered interrupt driven transmit (ex_stisr.c), and print to this.
3) if you must have a string, you can generate it, using sprintf, in one go, or if you 'must' do it a section at a time, generate it in parts, then assemble using strcat.

Best Wishes
vinniewryan



Joined: 29 Jul 2009
Posts: 154
Location: at work

View user's profile Send private message MSN Messenger

PostPosted: Fri Aug 13, 2010 2:26 pm     Reply with quote

compiler version 4.0.3

After trying all mentioned methods, I decided to just pay $0.60 for a memory chip. I didn't realize they're so cheap! So now that problem is solved.

Which brings me to my new problem, which I'm currently researching. I can't seem to send an int16 via printf or puts. This is the context:
Code:

//un-important setup stuff here
#use RS232(BAUD=19200,INVERT,LONG_DATA,FORCE_SW,XMIT=pin_a0)
long X;

X=Read_ADC();
puts("%d", X);
printf("%d", X);


when I set X as a LONG (16 bit), the compiler gives me an error "Print format type is invalid". When I set X as 'char', the compiler returns no errors, but only outputs 8 bits.

I need 16 bit values to be sent to my LCD but the compiler won't let me do this with my current methods.

Any ideas?
_________________
Vinnie Ryan
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Aug 13, 2010 2:32 pm     Reply with quote

Quote:
I need 16 bit values to be sent to my LCD but the compiler won't let me do this.

It will, and it's in explained in the CCS manual.
http://www.ccsinfo.com/downloads/ccs_c_manual.pdf
Look in the printf section of the manual. There is a table of format
values to use, to display any data type (including 16-bit). In the June
2010 manual, this is on page 207 (page 219 in the acrobat reader).
vinniewryan



Joined: 29 Jul 2009
Posts: 154
Location: at work

View user's profile Send private message MSN Messenger

PostPosted: Fri Aug 13, 2010 2:43 pm     Reply with quote

Thank you PCM, you just solved my problem! I overlooked the table, thanks for the info!
_________________
Vinnie Ryan
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