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.

While Loop problem using PIC12F683

Status
Not open for further replies.

ahmedabosriaa

Newbie level 5
Newbie level 5
Joined
Nov 26, 2012
Messages
10
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,337
Hi guys :)

I have a problem, hope to find help here :)

This is a simple program to let (B1= High) if (set = 8.1)
Unfortunately it doesn't work BUT it works only if we replaced the condition to (set<5.4 OR set<2.7).
So, could anyone tell me what is the problem.




double set;

void main() {

ANSEL =0b00100001;
GPIO=0;
TRISIO=0b000001; // All pins are output except pin one


while(1){
set=0;
while(set<8.1){
set+=2.7;
}
if(set == 8.1){
GPIO.B1 = 1;
}
}
}
 

I presume your code works according to C language specification. The problem is solely about understanding the nature of floating point numbers. Other than integer and fixed point numbers, they involve a rounding error. In the present case, matching 8.1 is missed by a very small difference. You can visualize by printing the numbers with full resolution.

But there's a simple solution: Never test for equality with float numbers, because it possibly never happens.

By the way, it seem pretty stupid to use float or even double (not actually supported by your compiler and casted to real, I guess) with a tiny PIC12F.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top