Cypress Perform

Home > Design Support > Cypress Developer CommunityTM > Cypress Forums > PSoC® 5 > sprintf function

Bookmark and Share
Cypress Developer CommunityTM
Forums | Videos | Blogs | Training | Rewards Program | Community Components



sprintf function
Moderator:
ANCY

Post Reply
Follow this topic



sprintf function

vgy posted on 29 Apr 2013 6:24 AM PST
Top Contributor
133 Forum Posts

hello ,

          I have converted hexadecimal to decimal and i am able to print on GLCD, now i have to do suitable mathematical operation on decimal value and to print on GLCD, but i could not able to suceed. here is my sample  code

#define x=0.0762939453125;

void main()

{

                unsigned char    Pstring[ 16 ]     = "                ";
                unsigned char   y, z, voltage;

  {

  ___________

}

                  for ( i = 0; i < 16; Pstring[ i ] = '\0', i++ );   
                  
                   sprintf( Pstring, "%04d", ( uint16 ) ADC_Sample_Max );

                  GLCD_Text(40, 20, FONT_FIFTEEN_DOT,  Pstring);

// in the above line i am able to print decimal value , to do operations am doing below operations

                   voltage=Pstring*x;

                  voltage=voltage/20;

                  y=voltage+10;

                 z=y/0.19;

             GLCD_Text(40, 20, FONT_FIFTEEN_DOT,  z);  // here z value should print.

}

                  

                  

        




Re: sprintf function

danaaknight posted on 29 Apr 2013 06:43 AM PST
Top Contributor
1773 Forum Posts

Here is sprintf() formating string examples that may help, attached.

 

Regards, Dana.



Re: sprintf function

danaaknight posted on 29 Apr 2013 06:46 AM PST
Top Contributor
1773 Forum Posts

You have to repeat the same sprintf() operation on the second value

to use the GLCD Text function. Just as you did on the first value.

 

Regards,. Dana.



Re: sprintf function

vgy posted on 29 Apr 2013 06:47 AM PST
Top Contributor
133 Forum Posts

but i am getting error at

voltage=Pstring*x;

 and the following error is

.\main.c:140: error: expected expression before '=' token



Re: sprintf function

Bob Marlowe posted on 29 Apr 2013 07:16 AM PST
Top Contributor
1768 Forum Posts

Pstring is a pointer to an array of characters, you cannot multiply that with something. You will have to do a calculation and then convert the result to a string again.

 

Bob



Re: sprintf function

vgy posted on 29 Apr 2013 07:29 AM PST
Top Contributor
133 Forum Posts

Can you explain a bit further



Re: sprintf function

Bob Marlowe posted on 29 Apr 2013 10:09 AM PST
Top Contributor
1768 Forum Posts

Well, you probably are not used to C-Language, but it is not too difficult to learn...

x is defined as a float constant, y and z as an integer in the range 0 to 255, that does not look right, define y and z as float.

sprintf() converts given values into a humal readable form  (character string), there is no need to fill the string with '\0'.

You cannot do a multiplication operation with a string (what is "Hello World" * 3.14)

But you may use your ADC_Sample_Max again and multiply it with whatever you want.

 

Here is a link to my favorite C-manual: http://publications.gbdirect.co.uk/c_book/ 

 

Bob



Re: sprintf function

danaaknight posted on 29 Apr 2013 12:24 PM PST
Top Contributor
1773 Forum Posts

Something like this

 

#define x = 0.0762939453125;

void main()

    unsigned char    Pstring[ 16 ]     = "                ";
    float            x, y, z, voltage;
    uint16            ADC_Sample_Max;       

    for ( i = 0; i < 16; Pstring[ i ] = '\0', i++ );               // Like Bob says, the sprintf() will null terminate the string for you
                                                                                      // when it is invoked, eg. you do not need this line of code
    sprintf( Pstring, "%04d", ( uint16 ) ADC_Sample_Max );
    GLCD_Text(40, 20, FONT_FIFTEEN_DOT,  Pstring);

    voltage = ( (float) ADC_Sample_Max * x ) / 20;
    y = voltage + 10;                                            // Not sure why you need y and z......
    z = y / 0.19;
    GLCD_Text(40, 20, FONT_FIFTEEN_DOT,  z);                      // here z value should print.

}



Re: sprintf function

Bob Marlowe posted on 29 Apr 2013 01:23 PM PST
Top Contributor
1768 Forum Posts

Last lines should read

 

sprintf( Pstring, "%04d", ( uint16 ) z );
    GLCD_Text(40, 20, FONT_FIFTEEN_DOT,  Pstring); // here z value should print.
 

 

Bob



Re: sprintf function

danaaknight posted on 29 Apr 2013 06:16 PM PST
Top Contributor
1773 Forum Posts

Thanks Bob for picking up on that, when I looked at it

I kept thinking I had to dupe the conversion and GLCD

lines, completely forgot to do that.

 

Senior momentus.



Re: sprintf function

vgy posted on 30 Apr 2013 05:46 AM PST
Top Contributor
133 Forum Posts

i did it in other way , to convert to hex to dec i used shift operation and then multiplied by suitable constant

                            temp=ADC_Sample_Max;
                            no1=temp & 0x001;
                            no1=no1*1;
                            temp=ADC_Sample_Max;

                            temp=temp>>1;
                            no2=temp & 0x001;
                            no2=no2*2;
                            temp=ADC_Sample_Max;

 

 






ALL CONTENT AND MATERIALS ON THIS SITE ARE PROVIDED "AS IS". CYPRESS SEMICONDUCTOR AND ITS RESPECTIVE SUPPLIERS MAKE NO REPRESENTATIONS ABOUT THE SUITABILITY OF THESE MATERIALS FOR ANY PURPOSE AND DISCLAIM ALL WARRANTIES AND CONDITIONS WITH REGARD TO THESE MATERIALS, INCLUDING BUT NOT LIMITED TO, ALL IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT OF ANY THIRD PARTY INTELLECTUAL PROPERTY RIGHT. NO LICENSE, EITHER EXPRESS OR IMPLIED, BY ESTOPPEL OR OTHERWISE, IS GRANTED BY CYPRESS SEMICONDUCTOR. USE OF THE INFORMATION ON THIS SITE MAY REQUIRE A LICENSE FROM A THIRD PARTY, OR A LICENSE FROM CYPRESS SEMICONDUCTOR.

Content on this site may contain or be subject to specific guidelines or limitations on use. All postings and use of the content on this site are subject to the Terms and Conditions of the site; third parties using this content agree to abide by any limitations or guidelines and to comply with the Terms and Conditions of this site. Cypress Semiconductor and its suppliers reserve the right to make corrections, deletions, modifications, enhancements, improvements and other changes to the content and materials, its products, programs and services at any time or to move or discontinue any content, products, programs, or services without notice.

Spec No: None; Sunset Owner: GRAA; Secondary Owner: RAIK; Sunset Date: 01/01/20