CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

sprintf problem..........

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
prayami



Joined: 22 Dec 2004
Posts: 78

View user's profile Send private message

sprintf problem..........
PostPosted: Sun Apr 09, 2006 4:00 pm     Reply with quote

Hi.....I have CCS PCH compiler version 3.235.23.16 . And I am using
18F4525 microcontroller. I tried the following program but non of the
PIN_D4 to PIN_D7 goes high.

This code is to save float value into to char array. And I tried to check
the position of the dot in that array but there is no dot in that array.

Is it bug...??

As per I understand...the following code ...

sprintf(input_str,"%01.2f",0.71);

should result like this...

input_str[0] = '1'
input_str[1] = '7'
input_str[2] = '.'
input_str[3] = '0'
input_str[4] = '\0'

But it is not like that.....................

Code:


#include <18F4525.h>
#fuses H4,NOWDT,NOLVP,NOBROWNOUT,NOPBADEN,PUT,NOXINST,NOMCLR,NOLPT1OSC
#use delay(clock=40000000)   
      
char input_str[5];


void main() {

  set_tris_d(0x00);

  output_low(PIN_D4);
  output_low(PIN_D5);
  output_low(PIN_D6);
  output_low(PIN_D7);

  while(TRUE) {
   
         sprintf(input_str,"%01.2f",0.71);        

      if( input_str[0] == '.' )
         output_high(PIN_D4);
      else
         output_low(PIN_D4);


      if( input_str[1] == '.' )
         output_high(PIN_D5);
      else
         output_low(PIN_D5);

      if( input_str[2] == '.' )
         output_high(PIN_D6);
      else
         output_low(PIN_D6);

      if( input_str[3] == '.' )
         output_high(PIN_D7);
      else
         output_low(PIN_D7);

   
  }//while(TRUE)
 
}//main

PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Apr 09, 2006 4:31 pm     Reply with quote

In your version of the compiler, in the printf (or sprintf) format statement,
the "width" specifier must be greater than the "precision" specifier.
See the changes I've made in bold below. If you make that change
then you get the proper output:
Quote:
string = 0.71


Quote:
#include <18F4525.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()
{
char input_str[10];

sprintf(input_str,"%03.2f",0.71);

printf("string = %s\n\r", input_str);

while(1);
}

In PCH vs. 3.249, your original code as posted, will work.
But with your older version, you have to do it like I've shown above.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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