|
|
View previous topic :: View next topic |
Author |
Message |
bandofcs
Joined: 24 Jul 2014 Posts: 6
|
How to printf a VERY long string |
Posted: Thu Jul 24, 2014 2:01 am |
|
|
dear all,
I am using PIC16F876A. When I try to define a char array variable more than size 90, they told me that data item is too big. My aim is to send a 136 bytes string to TX port in a single printf.
I have tried defining 2 smaller arrays and joining them together using pointer before sending. However, it doesnt work. No print out when view in HEX mode. Below is my code:
All help is welcomed.
Code: |
CHAR FIRST[69]={0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,
0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,
0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,
0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,
0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,
0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,
0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x00};
CHAR SECOND[69]={0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,
0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,
0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,
0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,
0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,
0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,
0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x00};
CHAR* st=&FIRST[0];
CHAR* nd=&SECOND[0];
st=strcat(st,nd);
printf("%s", st);
delay_ms (1000); |
|
|
|
asmallri
Joined: 12 Aug 2004 Posts: 1636 Location: Perth, Australia
|
Re: How to printf a VERY long string |
Posted: Thu Jul 24, 2014 4:07 am |
|
|
bandofcs wrote: | dear all,
I am using PIC16F876A. When I try to define a char array variable more than size 90, they told me that data item is too big. My aim is to send a 136 bytes string to TX port in a single printf.
I have tried defining 2 smaller arrays and joining them together using pointer before sending. However, it doesnt work. No print out when view in HEX mode. Below is my code:
All help is welcomed.
Code: |
CHAR FIRST[69]={0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,
0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,
0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,
0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,
0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,
0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,
0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x00};
CHAR SECOND[69]={0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,
0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,
0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,
0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,
0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,
0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,
0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x00};
CHAR* st=&FIRST[0];
CHAR* nd=&SECOND[0];
st=strcat(st,nd);
printf("%s", st);
delay_ms (1000); |
|
You are using a legacy PIC with significant architectural limitations, forcing the PIC to consume a large amount of RAM within it's very limited (early 1980s style) RAM banking architecture.
If you do not actually intent to use any of the special formatting capabilities of printf then don't use printf at all.
Code: |
CHAR* st=FIRST;
CHAR* nt=SECOND;
while (*st)
putc(*st++);
while (*nt)
putc(*nt++);
|
_________________ Regards, Andrew
http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!! |
|
|
bandofcs
Joined: 24 Jul 2014 Posts: 6
|
Re: How to printf a VERY long string |
Posted: Thu Jul 24, 2014 6:05 am |
|
|
Quote: |
You are using a legacy PIC with significant architectural limitations, forcing the PIC to consume a large amount of RAM within it's very limited (early 1980s style) RAM banking architecture.
If you do not actually intent to use any of the special formatting capabilities of printf then don't use printf at all.
Code: |
CHAR* st=FIRST;
CHAR* nt=SECOND;
while (*st)
putc(*st++);
while (*nt)
putc(*nt++);
|
|
Thanks for your reply andrew. I would like to clarify 1 point though. When I send an entire string via printf, I am assuming that there would only be 1 start bit and 2 stop bits to mark the start and end of the transmitted string.
Based on UART communication
http://en.wikipedia.org/wiki/Universal_asynchronous_receiver/transmitter
If I would to send individual char as in the code above, does it mean that each byte shall now have its own start and stop bits?
Is there a way to send the entire string at the same time such that i only get 1 start and 2 stop bits for the entire transmission? |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Thu Jul 24, 2014 6:14 am |
|
|
Quote: |
Is there a way to send the entire string at the same time such that i only get 1 start and 2 stop bits for the entire transmission? |
YES but NOT using printf....
RS-232 frames,by definition, each character with a start and stop bit.
and the UART hardware capability of the pic is oriented to this standard.
what you ask for is a more than a bit crazy as you will need to soft code both the TX and RX to make it happen.
WHY do you want to violate standard framing protocol in this way ? |
|
|
bandofcs
Joined: 24 Jul 2014 Posts: 6
|
|
Posted: Thu Jul 24, 2014 6:23 am |
|
|
asmboy wrote: |
YES but NOT using printf....
RS-232 frames,by definition, each character with a start and stop bit.
and the UART hardware capability of the pic is oriented to this standard.
what you ask for is a more than a bit crazy as you will need to soft code both the TX and RX to make it happen.
WHY do you want to violate standard framing protocol in this way ? |
Perhaps i misrepresented what im actually trying to say.
Will there be a difference between sending via putc() vs printf()? Will there be additional pauses/delay/bits between each character sent by use these 2 different functions? |
|
|
RF_Developer
Joined: 07 Feb 2011 Posts: 839
|
|
Posted: Thu Jul 24, 2014 7:24 am |
|
|
Quote: | Is there a way to send the entire string at the same time such that i only get 1 start and 2 stop bits for the entire transmission? |
No. Every character, whether sent by putc or printf, has the start and stop bits, normally only one of each - two stop bits is archaic and was needed for mechanical decoding, such as by a Teletype ASR33, though some systems retain it for legacy reasons, or because they have a fixed parity bit capable of being interpreted as a stop bit. That's how asynchronous transmission works.
There is no significant difference between sending character by character, e.g. using putc(), and using printf(), which simply calls putc() for each and every character anyway. |
|
|
asmallri
Joined: 12 Aug 2004 Posts: 1636 Location: Perth, Australia
|
|
Posted: Thu Jul 24, 2014 7:40 am |
|
|
RF_Developer wrote: |
There is no significant difference between sending character by character, e.g. using putc(), and using printf(), which simply calls putc() for each and every character anyway. |
This is not correct - there is a significant difference. If you avoid using printf then the code size will be significantly reduced. _________________ Regards, Andrew
http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!! |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Thu Jul 24, 2014 8:53 am |
|
|
Why cant you just do something like:
Code: |
int i=0;
Array[136]={1,2,3,...n};
while(i<n)
{
putc(Array[i]);
i++;
} |
include:
so that you can have a larger array.
G. _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
bandofcs
Joined: 24 Jul 2014 Posts: 6
|
|
Posted: Thu Jul 24, 2014 9:20 am |
|
|
Gabriel wrote: | Why cant you just do something like:
Code: |
int i=0;
Array[136]={1,2,3,...n};
while(i<n)
{
putc(Array[i]);
i++;
} |
include:
so that you can have a larger array.
G. |
Dear Gab, I have tried #device *=16 as mentioned in some posts in the forum. It doesnt work unfortunately. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
bandofcs
Joined: 24 Jul 2014 Posts: 6
|
|
Posted: Thu Jul 24, 2014 9:54 pm |
|
|
[quote="bandofcs"] asmboy wrote: |
Will there be a difference between sending via putc() vs printf()? Will there be additional pauses/delay/bits between each character sent by use these 2 different functions? |
for example
Code: |
CHAR FIRST[10]={0x22,0x22,0x22,0x22,0x11,0x11,0x11,0x11,0x11,0x00}
CHAR* st=FIRST;
while (*st)
putc(*st++);
|
vs
Code: |
CHAR FIRST[4]={0x22,0x22,0x22,0x22};
CHAR SECOND[6]{0x11,0x11,0x11,0x11,0x11,0x00};
CHAR* st=FIRST;
CHAR* nt=SECOND;
while (*st)
putc(*st++);
while (*nt)
putc(*nt++);
|
vs
Code: |
CHAR FIRST[10]={0x22,0x22,0x22,0x22,0x11,0x11,0x11,0x11,0x11,0x00}
printf("%s",FIRST);
|
vs
Code: |
CHAR FIRST[5]={0x22,0x22,0x22,0x22,0x00};
CHAR SECOND[6]{0x11,0x11,0x11,0x11,0x11,0x00};
printf("%s", FIRST);
printf("%s", SECOND);
|
etc.
Will i see a difference if i were to observe the output waveform in an oscilloscope? |
|
|
asmallri
Joined: 12 Aug 2004 Posts: 1636 Location: Perth, Australia
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19578
|
|
Posted: Fri Jul 25, 2014 1:17 am |
|
|
and as a very obvious comment, if any of these strings have even short sections that are 'constant', store these parts as constants.
Your chip is old, and has very limited RAM. If you declare a string as a normal variable with values, the whole string is stored in ROM, and then copied to RAM at boot time (so uses both ROM and RAM). If ten of the characters are constant, then store these in ROM only.
Understand that printf, _uses_ putc.
Printf, is a 'formatter'. It formats things the way you specify to send to putc.
So it allows you (for instance), to combine a constant string, and a variable, in one output:
Code: |
int fred=16;
printf("fred is %d\n",fred);
|
It sends "fred is ", then "16" then the line feed character.
It's output all goes 'via' putc.
As such, it adds complexity, which is not needed to just send a string. |
|
|
RF_Developer
Joined: 07 Feb 2011 Posts: 839
|
|
Posted: Fri Jul 25, 2014 1:48 am |
|
|
Which is what the poster was asking about - timing, not code size. :-| |
|
|
asmallri
Joined: 12 Aug 2004 Posts: 1636 Location: Perth, Australia
|
|
Posted: Fri Jul 25, 2014 8:37 am |
|
|
RF_Developer wrote: |
Which is what the poster was asking about - timing, not code size. :-| |
Rereading my post, I apologise if it came off as slightly offensive - it was not my intent. I agree - in the context of the specific serial framing you answer was correct. However, because the original poster did not understand the nature of serial comms formatting, I assumed it was likely he also did not understand the overhead associated with what he was trying to do given the limitations of the PIC16 family. _________________ Regards, Andrew
http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!! |
|
|
|
|
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
|