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 CCS Technical Support

get_timer0 output in float

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



Joined: 07 Nov 2013
Posts: 6

View user's profile Send private message

get_timer0 output in float
PostPosted: Tue Aug 22, 2017 12:30 am     Reply with quote

Hello,

Can I get float value of get_timer0 function ?
Is there any way to edit this function?

Code:

void main() {     
    lcd_init();
    delay_ms(5);      //
    lcd_putc("\f");   
    lcd_cursor_on(0);
   
    set_timer0(0);
    setup_timer_0(T0_EXT_L_TO_H|T0_DIV_256);
           
    while(1) {
        set_timer0(0);
        delay_ms(1000);
        f=get_timer0();
        //f= f * 256 ;
        lcd_putc("\f");
        printf(lcd_putc,"%f",f);       
    }
}


Code:

Out put is
 1000 /256 = 3.00

but i want

1000 / 256 = 3.90625


Is there any way to get correct float output !
alan



Joined: 12 Nov 2012
Posts: 357
Location: South Africa

View user's profile Send private message

PostPosted: Tue Aug 22, 2017 12:45 am     Reply with quote

PIC defaults to integer operations if both var are integer irrespective of the type it is assigned to.

So you have to force one of the variables to float to get the compiler to do float operations.
Eg.
Code:
  f = (float)get_timer0() / 257;

or
Code:
  f = get_timer0() / 257.0;
Ttelmah



Joined: 11 Mar 2010
Posts: 19496

View user's profile Send private message

PostPosted: Tue Aug 22, 2017 2:39 am     Reply with quote

The timer value _is_ an integer. So printing it as a float, won't change anything.....
Now as prisim says you can do some maths to generate a float result, by either casting the value (this is the technical name for the (float) declaration he shows), or by using a float value for the other variable in the maths.
However 'caveat'. This involves a huge amount of time. Floats are something in general that should be treated as if they carried a horrible infection, when dealing with a micro-controller like the PIC. You can generate a sensible output, much faster _and more accurately_ by using integers.
So:
Code:

void main(void)
{   
    int32 f; 
    lcd_init();
    delay_ms(5);      //
    lcd_putc("\f");   
    lcd_cursor_on(0);
   
    set_timer0(0);
    setup_timer_0(T0_EXT_L_TO_H|T0_DIV_256);
           
    while(1) {
        set_timer0(0);
        delay_ms(1000);
        f=get_timer0()*100000;
        lcd_putc("\f");
        printf(lcd_putc,"%7.5W",f/256);       
    }
}


This generates an integer count of 'hundredthousandths', that is then displayed with a decimal point as if it was a float value.
prisim



Joined: 07 Nov 2013
Posts: 6

View user's profile Send private message

PostPosted: Tue Aug 22, 2017 6:12 am     Reply with quote

Hello dear,
Thanks for the suggestion, but having the same issue !

Getting the output as

1000/256 = 3.00
Ttelmah



Joined: 11 Mar 2010
Posts: 19496

View user's profile Send private message

PostPosted: Tue Aug 22, 2017 7:24 am     Reply with quote

Are you sure you have got 'f' declared as an int32 as I show?. And the %7.5W?. You can't get 3.00 with the 7.5 it should be giving you 5 decimals.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Aug 22, 2017 9:58 am     Reply with quote

Quote:
thanks for the suggestion , but having the same issue !

Getting the output as

1000/256 = 3.00

Alan told you exactly what to do. Just re-read his post and do it !
Ttelmah



Joined: 11 Mar 2010
Posts: 19496

View user's profile Send private message

PostPosted: Tue Aug 22, 2017 10:47 am     Reply with quote

He has also not shown the actual code he is using. He talks about a division, but no sign of it in the code.
Makes it very hard to second guess what he is doing....

What I posted will run, and does display 3.90625 for a signal giving 1000 pulses in the 1 second sampling time.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Aug 22, 2017 11:13 am     Reply with quote

I know yours will run, but I thought if he just writes 300.0 instead of 300
then he will get what he wants. I was thinking of the easiest way.
Ttelmah



Joined: 11 Mar 2010
Posts: 19496

View user's profile Send private message

PostPosted: Tue Aug 22, 2017 12:45 pm     Reply with quote

Yes. The point I was making, is he doesn't show the actual maths he is using, or the data declaration for the variable. Now the compiler should complain if 'f' is not a float with the printf specifier he is giving, but if 'f' is float the cast should not be necessary. As it stands the number is an integer, and will only print the integer, since there are no decimals. Need to see what he is actually doing.
prisim



Joined: 07 Nov 2013
Posts: 6

View user's profile Send private message

PostPosted: Wed Aug 23, 2017 8:14 am     Reply with quote

Ttelmah wrote:
He has also not shown the actual code he is using. He talks about a division, but no sign of it in the code.
Makes it very hard to second guess what he is doing....

What I posted will run, and does display 3.90625 for a signal giving 1000 pulses in the 1 second sampling time.


Hello dear its my full main code, i just want to make a frequency counter, this code is fetched from web.

I just pasted the code as it is, but the result is invalid, if you don't mind can you share your whole c file, that you used and getting the results correctly ?
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