|
|
View previous topic :: View next topic |
Author |
Message |
Neckruin
Joined: 17 Jan 2006 Posts: 66
|
How to send constant array of bytes using printf |
Posted: Thu Nov 09, 2006 11:43 am |
|
|
Hello all,
I guess this should be a question simple to answer, but at this time of the day my brain is completely OFF...
I have a very long array of bytes and wish to send it via the UART of my PIC16F876. As the array is very long, I allocate it as a constant. According to this, is this code correct?:
Code: | const int8 ARRAY[] = {0xC9, 0x04, ... , 0x00};
printf("%s", ARRAY); |
Simulation shows that only the first byte is sent... is this code correct??
Thank in advance.
Regards,
Juanma. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
Neckruin
Joined: 17 Jan 2006 Posts: 66
|
|
Posted: Fri Nov 10, 2006 5:12 am |
|
|
In the thread you suggest there is no mention to the specific case of constant data.
Does this means that there is no diference?
Can I do this?:
Code: | const int8 ARRAY[] = {0xC9, 0x04, ... , 0x00};
int i;
for(i=0; i<sizeof(ARRAY); i++)
putc(ARRAY[i]); |
|
|
|
Ttelmah Guest
|
|
Posted: Fri Nov 10, 2006 6:01 am |
|
|
Neckruin wrote: | In the thread you suggest there is no mention to the specific case of constant data.
Does this means that there is no diference?
Can I do this?:
Code: | const int8 ARRAY[] = {0xC9, 0x04, ... , 0x00};
int i;
for(i=0; i<sizeof(ARRAY); i++)
putc(ARRAY[i]); |
|
Yes.
However:
putc(ARRAY);
Does this automatically for you.
This is exactly what this shortcut does. :-)
The only difference, is that as you show, the 'null' at the end would be output (remember the length of the array, includes the zero at the end), and the array could contain zeros elsewhere without stopping. The shortcut, stops when it finds the null, and won't output this character.
Best Wishes |
|
|
Neckruin
Joined: 17 Jan 2006 Posts: 66
|
|
Posted: Fri Nov 10, 2006 10:00 am |
|
|
If I try this code:
Code: | const int8 ARRAY[] = {0xC9, 0x04, ... , 0x00};
putc(ARRAY); |
I get a compiler error: Error 51: A numeric expression must appear here
But if I try this one:
Code: | const int8 ARRAY[] = {0xC9, 0x04, ... , 0x00};
int i;
for(i=0; i<sizeof(ARRAY)-1; i++)
putc(ARRAY[i]); |
Only a few bytes are sent, not all (well, at least I only see some in the simulator).
What am I doing bad??
My compiler version: 3.242 |
|
|
Ttelmah Guest
|
|
Posted: Fri Nov 10, 2006 10:07 am |
|
|
The key to the error message, is compiler version, and whether 'putc' is coded to accept an integer, or a char.
If you code putc into a wrapper function, as:
Code: |
void const_putc(int8 c) {
putc(c);
}
|
Then use 'const_putc(ARRAY);'.
It'll work. Some compiler releases don't need this, but others do.
Once done, this can (of course) be used for any number of constant strings.
You should get all your characters with the loop.
Best Wishes |
|
|
Neckruin
Joined: 17 Jan 2006 Posts: 66
|
|
Posted: Fri Nov 10, 2006 10:19 am |
|
|
So, the final solution you suggest is this?:
Code: | const int8 ARRAY = {0x01, 0x02, ... , 0xFF};
int i;
void const_putc(int8 c) {
putc(c);
}
for(i=0; i<sizeof(ARRAY)-1; i++)
const_putc(ARRAY[i]); |
or
Code: | const int8 ARRAY = {0x01, 0x02, ... , 0xFF};
void const_putc(int8 c) {
putc(c);
}
const_putc(ARRAY); |
I want to apologize for my... lack of knowledge |
|
|
Ttelmah Guest
|
|
Posted: Fri Nov 10, 2006 11:00 am |
|
|
Neckruin wrote: | So, the final solution you suggest is this?:
Code: | const int8 ARRAY = {0x01, 0x02, ... , 0xFF};
int i;
void const_putc(int8 c) {
putc(c);
}
for(i=0; i<sizeof(ARRAY)-1; i++)
const_putc(ARRAY[i]); |
or
Code: | const int8 ARRAY = {0x01, 0x02, ... , 0xFF};
void const_putc(int8 c) {
putc(c);
}
const_putc(ARRAY); |
I want to apologize for my... lack of knowledge |
The second. The whole 'point' of the shortcut, is it removes the need for your to fiddle around with a loop.
Best Wishes |
|
|
Neckruin
Joined: 17 Jan 2006 Posts: 66
|
|
Posted: Fri Nov 10, 2006 11:02 am |
|
|
Well, I'm afraid that in the simulator the second option doesn't work
Anyway I hope it's a problem of the simulator itself, not of the code.
Thank you anyway for your help. I'll go on investigating.
Regards,
Juanma |
|
|
Ttelmah Guest
|
|
Posted: Fri Nov 10, 2006 11:27 am |
|
|
It should work.
The only time the 'loop' is needed, is if you want to send a '0' (which is what is shown in the thread referred to higher up here). However the loop was also not giving the right results, so I'd definately suspect a simulator problem...
Best Wishes |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Nov 10, 2006 12:29 pm |
|
|
I took the program from the link that I posted, and I dropped it into
MPLAB vs. 7.20. I installed PCM vs. 3.242 and compiled it. I then
setup UART1 to receive the output from MPLAB Simulator, and write
it to a "TEST.DAT" file on my desktop. I ran the program and then
closed MPLAB. I then used WinHex to view Test.dat. Here is contents
of Test.dat:
Quote: | 0D 18 01 74 00 20 01 |
Conclusion: It works.
Code: |
#include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
void main()
{
int8 i;
int8 data[7] = {0x0D, 0x18, 0x01, 0x74, 0x00, 0x20, 0x01};
for(i = 0; i < sizeof(data); i++)
putc(data[i]);
while(1);
} |
|
|
|
|
|
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
|