Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
Tools
Old 27th September 2009, 06:56 AM   #16
Default

Your logic will work correctly up to 99 as this is the biggest BCD number than can be contained in a byte.

Mike.
Pommie is online now  
Old 27th September 2009, 01:06 PM   #17
Default

Quote:
Originally Posted by Pommie View Post
I was saying that making all variables floats was a bloated way to achieve the desired outcome. In the example given, j needs to be a float, the others could be chars. Also, casting to double makes it extra obvious what is being done.

Mike.
In that case, then respectfully, I agree.
__________________
A rectangular bear is just a polar bear after a coordinate transform. -- I dunno who.
A recent study shows that research causes cancer in rats. -- I dunno who said that one either.
Noggin is offline  
Old 29th September 2009, 06:32 AM   #18
Default

Quote:
Originally Posted by Johnbear View Post
I had to make all the variables in the calculation of type float for it to work.
-
You could have just done,
Code:
double j;
unsigned char i, Byte0, Byte1, Byte2, Byte3, x, y, z;
unsigned long FTW, bit32, Fo, Fref;

void main (void)
    { 
        y=5;
        z=2;
        j=(double)y/(double)z;
}
Mike.
Pommie is online now  
Old 2nd October 2009, 07:47 AM   #19
Default

Google for "Integer Promotion". This is an important term. Also search your compiler manual for this.

IP is a matter of C standards that all C compilers should either subscribe to, or list somewhere that they do something different. It comes down to several things:
1. It specifies where promotions to larger integers will occur. The left side of the equation never determines how the eq is interpreted during calculation. It's all the arguments on the right side. For equations which require multiple steps (add, mult, then divide), the type may even change multiple times.
2. Sometimes this automatic promotion will not be necessary, and consume excess resources to perform.
3. Some types of promotions, though potentially necessary for your application, are not done automatically. No one ever promotes an integer equation to a Float/Double.
4. Some types of promotion are unspecified by the standard and your compiler may or may not promote them.
5. You can often force the way integers are calculated by using a cast, but the compiler may overrule it in its actual implementation.

Also note that constants are evaluated and "folded" during the precompiler stage. As such, an integer may be assigned by a complicated calculation of float constants, either #defined as explicit constants or stated explicitly on the right hand side, and NO float code will be included in the program or executed. This is very handy at times for making clear code without making it any less efficient.

It is highly unlikely that you need a floating-point calc here. Everyone thinks they need FP early on, but rarely do. FP is quite slow to run, relatively speaking, and fixed-point type calculations can almost always do the job. In some cases they're more accurate, too.

You may find fixed-point math is easier and more efficient if you shift the value by considering it a mult/div of a power 2. You, as a programmer, will have to keep track of where the decimal is in the variable at any given time. Comment everywhere for documentation.
__________________
I thought what I'd do was I'd pretend I was one of those deaf-mutes.

Last edited by Oznog; 2nd October 2009 at 07:59 AM.
Oznog is offline  
Old 10th October 2009, 03:23 PM   #20
Default Pic 1937 timer problem

Hello..
I am using pic 1937 in my project. I am using timer0 as an interrupt. Now the problem i am facing is : when timer0 overflows, the corresponding flag bit gets set.. then it must go into its service routine..but it is not entering into its service routine..just it overflows and flag gets set.. and it restarts. not going into its routine.. can anybody suggest any solution or hint regarding this?

thank you so much in advance..
JAISHREE_KHATAR is offline  
Old 10th October 2009, 10:56 PM   #21
Default

This doesn't have anything to do with division. Make a new topic and people will happily try to help you out.
__________________
A rectangular bear is just a polar bear after a coordinate transform. -- I dunno who.
A recent study shows that research causes cancer in rats. -- I dunno who said that one either.
Noggin is offline  
Reply

Tags
dividing, numbers, pic

Thread Tools
Display Modes


Similar
Title Starter Forum Replies Latest
PIC ASM - Summation two 16 bit numbers (16f84) byrusber Micro Controllers 10 28th October 2007 09:41 PM
Random Numbers for PIC PDubya Micro Controllers 12 8th July 2006 11:01 PM
Voltage Dividing Networks Trevor Rymell Electronic Projects Design/Ideas/Reviews 19 12th April 2006 02:34 PM
Inputting 16 bit numbers into a 16 series pic bryan1 Micro Controllers 3 10th November 2005 08:31 AM
Delaying digital signal (frequency dividing) onder Electronic Projects Design/Ideas/Reviews 3 8th March 2004 12:45 AM



All times are GMT. The time now is 05:52 AM.


Electronic Circuits  |  Learning Electronics
eXTReMe Tracker