Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

MPASM Float variables

Status
Not open for further replies.

lord loh.

Member
I am trying to write a code with macros that is independent of the crystal frequency. I am trying to write delay routines such that by once specifying the crystal frequency like
Code:
Xtal EQU 8

I get MPASM do conditional assembly to select the right type of subroutine that would give me the desired delay. I would like to get MPASM do the calculation of the value that should go into the timer register.

However once I try to
Code:
instructionperiod=4/Xtal

the value shall become of a floating point type. How do I proceed now. I then tried to
Code:
movlw instructionperiod
the WREG was now 0x00.

Has anyone tried any such assembly?

Could anyone please guide me here....

Thanks in advance.
 
The following message from the assembler help file means you can't do what you are trying to do with floating point numbers.

Code:
Note: Intermediate values in constant expressions are treated as 32-bit unsigned
integers.  Whenever an attempt is made to place a constant in a field for which
it is too large, a truncation warning will be issued.

To avoid this problem consider representing the crystal frequency as an integer. So 4 Mhz. would be 4000000. Now the effects of truncation are not as severe. Remember that integer division will throw away(truncate) any remainder, so do the multiplications first and the divisions last.

To avoid the warning you can use an "and" operator in the constant expression to restrict its value to a byte or a word.
 
Last edited:
lord loh. said:
So getting milliseconds and microseconds out of MHz is out of quesion. :(
Not at all. As an example, suppose you want to compute the required number of instructions to execute for a 1 millisecond delay. Assume that the crystal frequency is 4 Mhz. Assume that your delay routine takes seven instruction cycles.

Code:
Floating Point Calculation
Instruction_Cycles = Delay / (7 * (4 / (Frequency))
142.8571429 = .001 / (7 * ( 4 / 4e6 ))

Now with just a little bit of algebra we can rewrite the equation as
Code:
Instruction_Cycles = (Delay * Frequency) / ( 7 * 4 )
142.8571429 = (.001 * 4e6) / 28
We are not done yet because the delay of .001 is not an integer. How do we make it into an integer? How about if we express it as an integer number of microseconds? It is 1000 microseconds so we multiply Delay by 1e6. If we scale Delay by 1e6 we have to divide frequency by 1e6. We end up with
Code:
Instruction_Cycles = (Delay(usec) * Frequency(Mhz.)) / 28
142 = (1000 * 4) / 28
We loose the .8571429 because we are not rounding we are truncating. How can we round? by adding 14 (why 14?) to the numerator before we do the divide. Then the result is 143.

For every floating point expression you can construct there is an integer expression which approximates the floating point expression to any number of significant figures you desire.
 
Not insight mon ami...experience! I've done this a few times before.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top