View previous topic :: View next topic |
Author |
Message |
pilar
Joined: 30 Jan 2008 Posts: 197
|
Problem with Sprintf function |
Posted: Wed Apr 25, 2018 5:27 pm |
|
|
Hi someone can help me with my code:
Code: | #include <18F4620.h>
#fuses HS,NOWDT,WDT32768,PROTECT,NOLVP,NOBROWNOUT
#use delay(clock=20MHz)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)// RS232 Estándar
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int j,i;
char Cabecera1[]={0x00, 0x11, 0x00, 0x0D, 0x81};
char Buffer[10];
void main(){
j=0;
for (i=0; i<5; i++){
sprintf(Buffer[j+i],"%02X",Cabecera1[i]);
}
while(TRUE);
} |
The Sprintf function is not working... What is my error? |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9241 Location: Greensville,Ontario
|
|
Posted: Wed Apr 25, 2018 5:38 pm |
|
|
You need to post what you expected it to do as well as what it actually did.
I don't know what it should do...however from a 'general observation'...
1) you should NOT use the 'protect' fuse until the final version of the code is 100%
2) you should ALWAYS add 'ERRORS' to the Use RS232(...options...)
3) the variable 'j' is always 0, so not needed. |
|
|
pilar
Joined: 30 Jan 2008 Posts: 197
|
|
Posted: Wed Apr 25, 2018 5:53 pm |
|
|
Ok Temtronic, but because the function do not work? |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1354
|
|
Posted: Wed Apr 25, 2018 6:07 pm |
|
|
pilar wrote: | Ok Temtronic, but because the function do not work? |
Again, what value do you expect in "Buffer" and what value do you actually see?
Just a cursory glance indicates that you are not using the variable j at all, so I would expect only the first 6 characters of buffer to be filled, with the loss of 4 characters. Also, if you intend it to be a string of 10 characters, you did not leave room for a null character, so you would not get what you expect anyways. |
|
|
pilar
Joined: 30 Jan 2008 Posts: 197
|
|
Posted: Wed Apr 25, 2018 6:23 pm |
|
|
I hope to find in Buffer the values of Cabecera1, but when I run the program the Buffer remains empty without data.... |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9241 Location: Greensville,Ontario
|
|
Posted: Wed Apr 25, 2018 6:38 pm |
|
|
OK, I have to ask how you're looking at the contents of the 'buffer' ?
Jay |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Wed Apr 25, 2018 6:40 pm |
|
|
Why not just STRCPY ()?
you have a Index on the target array which to me seems odd.
And an index on the source array so you would be only printing that location
Quote: | sprintf(Buffer[j+i],"%02X",Cabecera1[i]);
|
Change to:
Code: |
sprintf(Buffer,"%s",Cabecera1); |
_________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19538
|
|
Posted: Thu Apr 26, 2018 12:38 am |
|
|
The big problem is buffer[j+i]. This is _not_ a RAM address.....
In C, an address is generated by using '&'. There is a shortcut, in that the name of an array, is treated as it's address, but here he is selecting an element in the array. The result will be that the code will actually write to an address dependant on what happens to be in element 'i' of the array. Result garbage.....
Now the printf outputs two characters each time, but he moves forwards by one character. Better syntax to put the hex into the buffer:
Code: |
char Buffer[11]; //enough space for the five pairs of characters & null
void main(){
j=0;
for (i=0; i<5; i++){
sprintf(Buffer + j +(i*2),"%02X",Cabecera1[i]);
//Will give an address of the start of buffer, moving forwards
//2 characters each time.
//Or
sprintf(& Buffer[j +(i*2)],"%02X",Cabecera1[i]);
//again gets the address
}
|
|
|
|
pilar
Joined: 30 Jan 2008 Posts: 197
|
|
Posted: Thu Apr 26, 2018 2:17 pm |
|
|
Hi Ttelmah,
Thank for clarifying my error ... this work |
|
|
|