This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CCS/TMS320F28335: I am using a function provided at string.h that it dosn't work like I would like

Genius 9880 points

Part Number: TMS320F28335

Tool/software: Code Composer Studio

Hi Team, 

We received inquiry from customer asking for support regarding CCS and TMS320F28335 device. For me to not miss any information, I'll be copying the query below.

"

When I tried to convert data and send them for the serial, it's seems that I am using a function provided at string.h that it dosn't work like I would like. The code is like that:
interrupt void Task_2()
{
DefaultType fZOH36, fBlock13, fZOH34, fZOH37, fZOH38, fZOH39;

PS_MaskIntr(M__INT14);

fZOH39 = fGblADC1_10;
fZOH38 = fGblADC1_11;
fZOH37 = fGblADC1_8;
fZOH34 = fGblADC1_9;
{
static int contador=0;
static char mensaje_1[17]="00000000000000000";
static char Vin[7]="0000000";
static char Vout[7]="0000000";
static char Iin[7]="0000000";
static char Iout[7]="0000000";
fBlock13=1;
if (contador>=4){
contador=0;
}
else{
contador++;
}
switch(contador) {
case 0:
sprintf( Iin,"%f",fZOH39);
sprintf( Iout,"%f",fZOH38);
sprintf( Vin,"%f",fZOH37);
sprintf( Vout,"%f",fZOH34);

strcpy(mensaje_1, "ADC:CH1:");
strcat(mensaje_1, Vin);
mensaje_1[15]='\n';
mensaje_1[16]='\0';

The conflict I thinks it's the way I use the sprintf and the concatenates, due when I remove the mensaje_1[15]='\n';
mensaje_1[16]='\0'; and use only a number like 1 or 2, the variable wich stores the scia missatges don't become crazy.
the memory browser also revels:

mensaje_1
A D C : C H 1 : 0 . 7 6 4 6 4 . .
mensaje_2
A D C : C H 2 : 0 . 7 6 5 3 8 1 2

"

Thank you and looking forward for your kind response.

Regards,

Maynard

  • First, your initializations do not allow for the '\0' at the end of the string, but that should be OK.

    Second, there is a good chance that

    sprintf( Vin,"%f",fZOH37);

    stores more than 7 characters in Vin, and clobbers the variable next to it.

    If Vin is more than 7 characters, your strcat() is longer than you think and is clobbering memory, too.

    Always allow a generous amount of space for your strings, and don't forget the room for the trailing nul.

    I suggest you check out snprintf() which helps to prevent these overruns, though making all your strings 32 characters might fix this particular problem. Also check out using fieldwidths, too, for example, using %7.5f would have also prevented this.