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

printf and atof

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



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Nov 11, 2006 12:16 am     Reply with quote

Use the sprintf() function to convert a number to an ASCII string
and put it in an array. Example:

The following program displays:
Quote:
4.94


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()
{
float value = 4.940000; 
char array[20];

sprintf(array, "%3.2f", value);

printf(array);


while(1);
}
Ttelmah
Guest







PostPosted: Sat Nov 11, 2006 4:56 am     Reply with quote

As a few other comments:

You are reading the ADC twice. Why?.
value=read_adc();

Performs a start, waits for the conversion, and then reads it. The next pair of lines, basically do the same again....

Your printf format is faulty, which is probably why it does not work. In printf, the first number, is the _total field width_ (not the digits in front of the DP). Then the number after the DP, is how many trailing digits are required. Most C's, are smart enough, that if the first number is smaller than the second, they increase the first. CCS does not. You need to make the field big enough to hold the whole result.
Also, remember that on the ADC, the 'full' voltage on the Vref, is actually one count _above_ the maximum value read. So you wuld need to use /0x400, not 0x3FF, to get correct scaling.

Then, consider not using a float. Floating point arithmetic, is slow. You don't actually need a float to do what you want. If you only want 2DP, depending on the accuracy you really 'need', you might consider:
Code:


int32 value;
value=read_adc();
value*=247L;
value/=512;

printf("%4.2w",value);


This generates in integer arithmetic, adc*247/512, which is then displayed as x.xx. In int32, this can be made really accurate, and is a lot quicker than float (especially if the division is a binary value, which is then done by simple rotation).

Best Wishes
new2ccs



Joined: 29 Oct 2006
Posts: 10

View user's profile Send private message

printf to an array
PostPosted: Sun Nov 12, 2006 10:57 am     Reply with quote

Hello PCM PROGRAMMER and Ttelmah,

thank you for your responses, but i used both of your code and it did not work for me. Some how generating a value and assign it to an array does not work. It only works when i assign it directly ex: char array[] ={'12','a'};. something like this.

Ttelmah, I used your int32 stuff to replace the float and it gave me the wrong result of the voltage conversion.

so i guess i have to figure out a way to format it myself instead of using the built in function then.

thank you very much guys.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Nov 12, 2006 3:54 pm     Reply with quote

Quote:

I am using PCH 3.163 and MPLAB 7.31 to debug(ICD2)
I used both of your code and it did not work for me.

I tested the program that I posted above with PCM vs. 3.163.
I used an ICD2 to program it into an 16F877 on a PicDem2-Plus board.
It worked OK. It displayed this in the terminal window:
Quote:
4.94
new2ccs



Joined: 29 Oct 2006
Posts: 10

View user's profile Send private message

PostPosted: Mon Nov 13, 2006 10:12 am     Reply with quote

sorry, it is my display function that has problems, but i don't know why. I can display everything else ok, but when i used my display_volts function then my lcd won't work. With the code that you showed me, it worked i know now cuz when debugging if i hit the run button then i couldn't see anything, but when i stop it, then i sometimes see my NUMBERS.
Ttelmah
Guest







PostPosted: Mon Nov 13, 2006 10:22 am     Reply with quote

You did read the bit about using sprintf, not printf?. Printf, outputs to the currently selected output device. It does not return anything, so the contents of 'answer', will always be garbage.
I think 3.193, may be too old for the %w format to work properly. However you can simulate this by just sprintf ing the value to a string, and adding the decimal point yourself.

Best Wishes
new2ccs



Joined: 29 Oct 2006
Posts: 10

View user's profile Send private message

PostPosted: Mon Nov 13, 2006 11:14 am     Reply with quote

with the code that PCM PROGRAMMER showed me. it works ok.(sprintf). all i need to do now is to figure out why when using display_volts function with the array size of less than 16 characters , it messes up my lcd display(won't work).

Code:
char text1[]= {' ',' ','B','u','z','z','e','r',
              ' ','=',' ','S','3',' ',' ',' '};



this function is to display data to LCD.
Code:
void display(char arr[])
{

   int i;
   
   for(i = 0; i < 16; i++)
   {
                               if( arr[i] == '\0') // for different arr sizes (check for NULL
                                   i = 16;
                               else
      lcd_write_data(1,arr[i]);
   }
}


so in my MAIN function i do this

display_volts(value). this function then calls the display function
above to display my voltage array that is created my the sprintf(arr,"%3.2f, value);
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Nov 13, 2006 11:59 am     Reply with quote

Here's a demo program for the PicDem2-Plus that works.

Use the "Flex_lcd.c" driver that you can get from here. It's already
configured to work with a PicDem2-Plus.
http://www.ccsinfo.com/forum/viewtopic.php?t=24661

This program reads the value of the trimpot, converts it to volts,
and displays it on the LCD on the PicDem2-Plus board. I tested
this with PCM vs. 3.249 and it works OK.

I also installed your version, 3.163, and it works with that one too.
For your version, the "AN0" constant doesn't exist. Use the RA0_ANALOG
constant instead. (They're the same thing).

Change this line
Code:
setup_adc_ports(AN0);

to this:
Code:
setup_adc_ports(RA0_ANALOG);

Then it will compile with vs. 3.163.

Code:

#include <16F877.H>
#device adc=10
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
//#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

#include "flex_lcd.c"

//============================
void main()
{
int16 adc_value;
float volts;
 
lcd_init();

setup_adc_ports(AN0);
setup_adc(ADC_CLOCK_DIV_8);
set_adc_channel(0);

while(1)
  {
   adc_value = read_adc();
   volts = (float)(adc_value * 5)/1023.0;   
   printf(lcd_putc, "\f%3.2f", volts);
   delay_ms(500);
  }
}
Ttelmah
Guest







PostPosted: Mon Nov 13, 2006 3:29 pm     Reply with quote

The answer to the array size, is simple. A _string_, always needs one more character than the number in the text, for the '\0' terminator character.

Best Wishes
new2ccs



Joined: 29 Oct 2006
Posts: 10

View user's profile Send private message

PostPosted: Tue Nov 14, 2006 9:58 am     Reply with quote

thanks for the code, but suddenly my program works now. Maybe I changed the line in my loop from i =16 after the if statement to BREAK;
Code:
void display(char arr[])
{
   int i;
   
   for( i = 0; i <16; i++ )
   {
      if( arr[i] == '\0')   
         break;
      else
        lcd_write_data(1,arr[i]);
      
   }


}
new2ccs



Joined: 29 Oct 2006
Posts: 10

View user's profile Send private message

address lines picdem 2+
PostPosted: Wed Nov 15, 2006 3:39 pm     Reply with quote

I have a question about the address lines of the LCD. In the data sheet of hd44780U says line 2 = 0x40 and line 1 = 0X00, but when debugging the flex.c code. i notice that line 1 = 0xff and line 2 =0xbf. Why not line 1 = 00 and line 2 =40.

HD44780U
00 01 02 03 04 05 06 07 08 09 0A 0B0C0D0E 27
40 41 42 43 44 45 46 47 48 49 4A 4B4C4D4E 4F
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Nov 15, 2006 3:47 pm     Reply with quote

Quote:
when debugging the flex.c code. i notice that line 1 = 0xff and line 2 =0xbf

In the lcd_gotoxy() function, it sets the address to 0x00 for line 1
and to 0x40 for line 2.
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