Shop OBEX P1 Docs P2 Docs Learn Events
multiplication and division of variable — Parallax Forums

multiplication and division of variable

xEaroxEaro Posts: 3
edited 2008-04-10 10:43 in Propeller 1
Hello all,
I'm new here and I fail to a simple thing.
My problem is the multiplication and division of variable (calculate).

I want to calculate 3 variable for my PWM

PUB PWM(Pin, PWM_FREQ, DutyCycle)|count, PWM_time, CNT_us

PWM_time  := 1/PWM_FREQ           '(E.g. 1/25_000 = 40µs) 
CNT_us    := CLKFREQ/1_000_000    '(E.g. 80_000_000/1_000_000 = 80 counts pro µs) 
DutyCycle *= 1/10                 '(1/10 equals 10% -  1/1000 would be the best)
 
DIRA[noparse][[/noparse]7] := 1  
count := cnt

CTRA := %00100 << 26 + 7 
FRQA := 1 
REPEAT 
 PHSA := -(PWM_time * CNT_us * DutyCycle)     'Must be rounded 
 WaitCnt(count += (PWM_time*CNT_us)) 

How does it work, to calculate right? With FloatMath?

I hope you can help me. Thanks and sorry for my bad English!
xEaro

Post Edited (xEaro) : 4/10/2008 7:44:39 AM GMT

Comments

  • stevenmess2004stevenmess2004 Posts: 1,102
    edited 2008-04-10 08:30
    All the maths on the propeller is done with integers. So when you do 1 / some big number you will end up with 0. Your options are to use fixed point numbers, change your algorithm so that you don't need non-integer numbers or use one of the floating point objects (there are three).

    If you change things a little than you won't need floating point for this. It seems like you just want to find the number of clock cycles for the high period and the total number for the entire cycle.

    Instead of using PWM_time and PWM_FREQ, change it to the number of clock cycles. You can do the same thing for the duty cycle. It simply means that some of your other code will have to change.
  • xEaroxEaro Posts: 3
    edited 2008-04-10 09:19
    Ah ok, thanks.
    In this example it works, but if I need a comma number e.g. 0.5 (1 / some big number) - how should I calculate this?
    now way?
  • stevenmess2004stevenmess2004 Posts: 1,102
    edited 2008-04-10 09:43
    Depending on exactly you can do a couple of things.
    1. You can use the floating point objects. For example you can do F.FDiv(num1,num2)
    2. You can use a fixed point scheme. In this case you just say that you have a certain number of decimal places. You can use addition and subtraction as normal but multiplication and division will need some extra shifts.
    3. You can use fractions. Not used very often but it is an option.
  • xEaroxEaro Posts: 3
    edited 2008-04-10 10:43
    Thanks, I think I understand it
Sign In or Register to comment.