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

Problem with printf and Float

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



Joined: 25 Mar 2011
Posts: 7

View user's profile Send private message

Problem with printf and Float
PostPosted: Fri Mar 25, 2011 7:22 pm     Reply with quote

Hi, I'm having trouble with this code, when I simulated using the Proteus, everything works OK and I can see in the hyperterminal of Proteus, which prints the correct values​​, but when I progam the PIC with the code, I can only receive all-zero in the HyperTerminal of the PC, , anyone can tell me what my error?

Code:
#include <18F452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP,NOBROWNOUT,NOSTVREN
#use delay(clock=20000000)
#use rs232(baud=19200, xmit=PIN_C6, rcv=PIN_C7, ERRORS)// RS232 Estándar
#use standard_io(b)
#use standard_io(d)

#include <stdlib.h>
#include <input.c>

int32 pulsos_LH ;
float Mostrar;
int32 Indice;

#INT_TIMER0
void Tiempo_Demo() {
   set_timer0(22464);
   Indice++;
   
}


void main(){

    setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256);
    enable_interrupts(INT_TIMER0);
    enable_interrupts(GLOBAL);
    set_timer0(22464);
    pulsos_LH = 56;

while (TRUE){
   
     Mostrar = (pulsos_LH*3600*0.036)/((Indice)*2.25);
     printf("Consumo = %8.4f L/H\r\n",Mostrar);
    }
}
Jerson



Joined: 31 Jul 2009
Posts: 125
Location: Bombay, India

View user's profile Send private message Visit poster's website

PostPosted: Fri Mar 25, 2011 7:30 pm     Reply with quote

You can do this
Mostrar = ((float)pulsos_LH*3600*0.036)/((float)Indice*2.25);

OR

change the definitions for pulsos and Indice to float type. In your case, this may not be ideal considering what they mean.

If it starts to work, you can figure it out.
_________________
Regards
Jerson Fernandes
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Sat Mar 26, 2011 3:21 am     Reply with quote

Shouldn't actually need the cast. Since two of the constants are float values, and one is present on both sides of the equation, casting should be automatic.

However some little comments.
There potentially is a problem in using an int32 value, that is incremented in an interrupt, in a sum outside the interrupt. Problem is that the interrupt may occur half way 'through' the sum, and then the result would be invalid...
Though the compiler should automatically do it, 'pre-solve' things like 3600*0.036, to make sure that extra work is not involved. In fact pre-solve all the constant terms. So:
Code:

void main(void){
    int32 local_value;
    setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256);
    enable_interrupts(INT_TIMER0);
    enable_interrupts(GLOBAL);
    set_timer0(22464);
    pulsos_LH = 56;

    while (TRUE){
       disable_interrupts(GLOBAL);
       local_value=Indice;
       enable_interrupts(GLOBAL); //Make a local copy of the value with
       //Interrupt momentarily 'off'.
   
       Mostrar = (pulsos_LH*57.6)/(float)Local_value;

       //3600*0.036/2.25 = 57.6
       printf("Consumo = %8.4f L/H\r\n",Mostrar);
    }
}

The compiler should automatically cast pulsos_LH to a float, since it is being multiplied by a float value, and then the float result will be used for a float division. However casting the local value, makes sure.
As written, it is going to print about 200times, before the number will change. Perhaps consider waiting till Indice does change, before actually printing?.

Best Wishes
temtronic



Joined: 01 Jul 2010
Posts: 9225
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Mar 26, 2011 5:54 am     Reply with quote

Does the real hardware include a MAX232 chip between the PIC and the PC , with the correct wiring?
I ask since you say it works fine in Proteus( correct numbers).

Maybe have a simple print ' hello world 1 2 3 4 ' to verify the hardware is correct ?
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