Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics 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.

[PIC] Float Number not showing Properly

Status
Not open for further replies.

M.Rehan

Full Member level 2
Joined
Feb 10, 2016
Messages
129
Helped
2
Reputation
66
Reaction score
33
Trophy points
28
Activity points
972
VOLTAGE_FACTOR not showing proper value

when V is greater than 512



Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
sbit LCD_RS at RD4_bit;
sbit LCD_EN at RC7_bit;
sbit LCD_D4 at RC6_bit;
sbit LCD_D5 at RC5_bit;
sbit LCD_D6 at RC4_bit;
sbit LCD_D7 at RD3_bit;
sbit LCD_RS_Direction at TRISD4_bit;
sbit LCD_EN_Direction at TRISC7_bit;
sbit LCD_D4_Direction at TRISC6_bit;
sbit LCD_D5_Direction at TRISC5_bit;
sbit LCD_D6_Direction at TRISC4_bit;
sbit LCD_D7_Direction at TRISD3_bit;
 
char V_TEXT[8];
char FACTOR_TEXT[11];
char TEMP_TEXT[11];
 
float VOLTAGE_FACTOR = 1.00;
float TEMP_FACTOR_1 = 0;
float TEMP_FACTOR = 0;
 
unsigned int V = 0;
 
void main()
{
 
 TRISA = 0x2F;
 PORTA = 0x00;
 LATA = 0x00;
 ANSELA = 0x2F;
 
 TRISB = 0xFF;
 PORTB = 0x00;
 LATB = 0x00;
 ANSELB = 0x00;
 
 TRISC = 0x00;
 PORTC = 0x00;
 LATC = 0x00;
 ANSELC = 0x00;
 
 TRISD = 0x00;
 PORTD = 0x00;
 LATD = 0x00;
 ANSELD = 0x00;
 
 Lcd_Init();
 Lcd_Cmd(_LCD_CLEAR);
 Lcd_Cmd(_LCD_CURSOR_OFF);
 
  while(1)
   {
    Delay_ms(1000);
    V = ADC_Read(3);
    TEMP_FACTOR_1 = 512-V;
    TEMP_FACTOR_1 = TEMP_FACTOR_1/512;
    VOLTAGE_FACTOR =   TEMP_FACTOR_1 + VOLTAGE_FACTOR;
 
    Delay_ms(100);
    IntToStr(V,V_TEXT);
    lcd_out(1,1,V_TEXT );
    Delay_ms(100);
    
    floatToStr(VOLTAGE_FACTOR,FACTOR_TEXT);
    lcd_out(2,1,FACTOR_TEXT );
    Delay_ms(100);
 
   }
}



MIKROC Pro For PIC
PIC 18F46k22

Capture.PNG

Capture1.PNG

- - - Updated - - -

Formula for Voltage factor is

Code:
Voltage Factor = ((512-V)/512) + Voltage Factor

where voltage factor is 1.00 initially

- - - Updated - - -

I know it is problem with floating point mathematics
Something is missing doing maths in MikroC compiler
 

It's no mikroC problem.

I think you should learn more about C language and it's automatic type conversion. The below (partial) expression with unsigned variable V in the range 0 to1023 is calculated in pure integer arithmetic, result is either +1 or 0.
Code:
(512-V)/512

You possibly want this expression to be calculated
Code:
(float)(512-V)/512
 
  • Like
Reactions: M.Rehan

    M.Rehan

    Points: 2
    Helpful Answer Positive Rating
The variable 'V' declared on line 22 as unsigned, in operation performed on line 55
Code:
TEMP_FACTOR_1 = 512-V;
That somehow seems don't implicitly casting the result of subtraction to signed.
Anyway, once the above variable is declared as float, the following line 56 keep it with this type
Code:
TEMP_FACTOR_1 = TEMP_FACTOR_1/512;

Perhaps just declaring the variable 'V' as int could solve that, I guess.
 

Code:
(float)(512-V)/512


Yeah it gives right answer
Thanks


Perhaps just declaring the variable 'V' as int could solve that, I guess.

I don't know its problem of MikroC or C that "For arithmetic operations between FLOAT and INT or other different data types all variables should be converter to highest size datatype before any arithmetic operation "

I,e For int and Char ---> char variable should be converter to int before arithmetic operation

for float and int ---> int should be converted to float
 
Last edited:
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top