MPASM Float Variables

Status
Not open for further replies.

Lord Loh.

Full Member level 4
Joined
Jun 19, 2004
Messages
196
Helped
1
Reputation
2
Reaction score
0
Trophy points
1,296
Location
Texas
Activity points
1,951
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.
 

Hi,
I just want to tell u that PICs do not allow divisions and multiplication! u want to get some code to handle that.
second, in ur problem, i think u should better change ur code to use integer values rather thna float ones.
if u want tell me what u're trying to do with this 'istructionperiod' and i 'll tell u how to change ur approach.
note: this is useful because avoiding divisions and mult in PIC programmation: minimizes the real time constraints; avoid lost of data; minimizes the code size...
 

I am not trying to divide and multiplty using a PIC. I am trying to divide and multipy using MPASM.

I do not care if the code assembly time increases by a few milliseconds. I want to make my code crystal independant. All the programmer will have to do is set
Code:
Xtal=x(MHz
and MPASM would do a conditional assembly to generate the required code for delays counts etc.
 

Hi,
if u re using MPASM how did u implement :
instructionperiod=4/XTAL
in asm, have u did it right?
the above expression isn't a valid asm instruction!
more details.
 

Hi,

I do this sort of thing alot in MPASM, however, I think you miss understand about the float.

MPASM only uses integer type values of 24, 16, or 8 bits that I'm aware of. You have to be careful, because it will truncate.

instructionPeriod equ 4/8

is the same as

instructionPeriod equ 0

This is a better way of doing things. Say you want to delay by 100 uS and the XTAL was 8 MHz. The number of instruction cycles you would delay is:

100*8/4 = 200

instead do it this way

Code:
XTAL equ 8
variable delay, delayCycle
delay = 100
delayCycle = (delay * XTAL) / 4

But say you wanted a delay of something messy like 5.5us.

Then you need to think about possibly defining in ns or 100ns blocks.

I would do

Code:
XTAL equ 8
variable delay, delayCycle
delay = 55 ; 5.5uS
delayCycle = (delay*XTAL) / 40 ;11 cycles

Then you have to think about rounding. Suppose you wanted a delay of 5.6 us. The closest you could get is to delay 5.5us or delay 6us. Obviously 5.5 us is closer. If you want to round then I suggest using

Code:
XTAL equ 8
variable delay, delayCycle
delay = 56
delayCycle = (((delay + 1) * XTAL) / 20 + 1) / 2

This is 57*8 = 456. 456/20 = 22.8 => 22. 22+1 = 23. 23/2 = 11.5 => 11 (delay of 5.5 uS).

-jonathan
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…