Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Register Log in

[PIC] How do I use Float in xc8 compilor ?

Status
Not open for further replies.

Ivan-Holm

Member level 5
Joined
Jun 3, 2010
Messages
84
Helped
3
Reputation
6
Reaction score
2
Trophy points
1,288
Location
Denmark
Activity points
1,894
the code works fine with out float to show the int number of the ADC, but when I want to use float it works for a little bit time, and then Crash the Program and the micro P stop.
the code print the voltage i Volt, with to decimal after. on a 4 x 20 ascii Display. and have hit the wall :bang: to come op what to do. even #include<float.h> the Pic is an Pic18f242
define
Code:
//*************Analog*****************
 #define analog1 ADCON0 = 0x85;
 #define analog2 ADCON0 = 0x8d;
 #define analog3 ADCON0 = 0x95;
 #define analog4 ADCON0 = 0x9d;
 //#define analogon ADCON0bits.GO_DONE
//*******************Global variabler***********
 char lcdtext[16];
 int line,L;
 float volt;
 int An;
 float vdd10bit = 4.75/1023;
Code:
 //************ analog read ***
     int AnalogRead(int ch)
     {
         if(ch==1)
         {
              analog1;
         }
         else if(ch==2)
         {
              analog2;
         }
         else if(ch==3)
         {
              analog3;
         }
         else if(ch==4)
         { 
             analog4;
         }
         ADCON0bits.GO_DONE = 1;
        // int An;
         
//         for (An=0; An<1; An++)
//         {
          while(ADCON0bits.GO_DONE != 0)
          {
            blaa_on;
          }
  
        volt=(vdd10bit*(float)ADRES);
        // volt=ADRES;

return (float)volt;
     }
 

xenos

Full Member level 4
Joined
May 9, 2015
Messages
212
Helped
82
Reputation
164
Reaction score
81
Trophy points
28
Location
127.0.0.1
Activity points
1,182
There are no much to tell you from these code fragments here.
1) The vdd10bit should be declared with const modifier (probably optimizer will handle multiplication better).
2) function calculates a float and then converts it to int and return int

Add more code...

- - - Updated - - -


a little modification on the function without using global variables and
optimization on channel selection

Code:
float AnalogRead(short int ch){
	const float vdd10bit = 4.75/1023;
	float value = 0;
	if(ch>0 && ch<=4){
		ADCON0 = 0x85 + ((ch-1) << 3);
		ADCON0bits.GO_DONE = 1;
		while(ADCON0bits.GO_DONE != 0)
		{
			?????;
		}
  
		value=(vdd10bit*(float)ADRES);
	}
	return value;
}
 

horace1

Advanced Member level 5
Joined
Nov 18, 2008
Messages
2,123
Helped
596
Reputation
1,188
Reaction score
573
Trophy points
1,393
Location
Norwich, UK
Activity points
13,071
the float data type is supported by the xc8 compiler you just declare a variable a float and the compiler will allocate storage for it etc
no need to #include <float.h> unless you want to determine the precision, maximum/minimum values, etc, see
http://www.cplusplus.com/reference/cfloat/
 

Ivan-Holm

Member level 5
Joined
Jun 3, 2010
Messages
84
Helped
3
Reputation
6
Reaction score
2
Trophy points
1,288
Location
Denmark
Activity points
1,894
what does the const do ? xenos

horace1: yes the float works also in practice, but after some time it stops completely and the display just just blink with the cursor, after some time.
If I do not use float , it works fine, but then I can not show the volt with decimal, so something happens, maybe some buffer that the the Pic use got can't be emty.

I will paste the code, but it has nothing whit to do with the conversion. and some comment is en danish
 

horace1

Advanced Member level 5
Joined
Nov 18, 2008
Messages
2,123
Helped
596
Reputation
1,188
Reaction score
573
Trophy points
1,393
Location
Norwich, UK
Activity points
13,071
horace1: yes the float works also in practice, but after some time it stops completely and the display just just blink with the cursor, after some time.
If I do not use float , it works fine, but then I can not show the volt with decimal, so something happens, maybe some buffer that the the Pic use got can't be emty.
danish
float calculations take far longer than integer - in particular on devices without floating point coprocessors such as the PIC18 it will be orders of magnitude greater
you are probably getting some problems with the delays due to the longer calculation time
 

xenos

Full Member level 4
Joined
May 9, 2015
Messages
212
Helped
82
Reputation
164
Reaction score
81
Trophy points
28
Location
127.0.0.1
Activity points
1,182
The "const" means that the value of the variable does not change. If the value is visible during compilation, the compiler will use it directly in stead of loading it from some memory.

I asked for the code, because I had a suspicion that you call this function from within a tight time limited section (like an interrupt service routine) and that's why the processor time (after some point of execution) is not enough to handle all the requests and stops responding.

Althow you can use float arithmetic with no problem (except the significant processing time increment), you can use decimals to print, without float arithmentic.
e.g. use as a unity value the 1/100 of a volt. Then if the ADC is 202, it means that you have 2,02 volts.
 

Ivan-Holm

Member level 5
Joined
Jun 3, 2010
Messages
84
Helped
3
Reputation
6
Reaction score
2
Trophy points
1,288
Location
Denmark
Activity points
1,894
the Const helped it does not stop now. :D Thanks for the advice
 

Kurenai_ryu

Advanced Member level 2
Joined
Jun 10, 2006
Messages
671
Helped
159
Reputation
316
Reaction score
99
Trophy points
1,308
Location
Bolivia
Activity points
5,913
also check that the printf (I think the implementation of ftoa) is buggy in the XC8 standard library (at least on some versions of XC), so try to implement your own float printing function...
 

Ivan-Holm

Member level 5
Joined
Jun 3, 2010
Messages
84
Helped
3
Reputation
6
Reaction score
2
Trophy points
1,288
Location
Denmark
Activity points
1,894
I use the sprintf funktion not printf, I like to add it to an string array to chop the Ascii in to the display write loop.
 

Aussie Susan

Advanced Member level 4
Joined
Jan 5, 2015
Messages
1,190
Helped
365
Reputation
730
Reaction score
362
Trophy points
83
Activity points
12,893
It makes no real difference whether you use sprintf or printf or any other of the 'printf' family of functions as they all use the same core functions.
If it is working then that is good, but I will almost bet that you have used a constant format string with whichever function you are using . (In my experience I have only ever seen a couple of instances where people have used generated format string on the printf family of functions.) My issue with these functions is that they are big (in terms of their RAM and ROM memory footprint) and slow (at run-time the format string must be parsed each time) and that microcontrollers are generally memory limited and have a slow(ish) processor.
The printf family of functions all parse the format string and then select an appropriate function from a suite (that must all be in ROM so it can handle any valid format instruction) that will do the actual conversion to a string and then concatenating that with the other parts to generate the final string. As the format string is almost always constant, you can save a lot of time and memory by simply calling the appropriate function yourself and do the string concatenation yourself as well.
Just me on my soap-box!
Susan
 

Status
Not open for further replies.
Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top