multiplication and division of variable
xEaro
Posts: 3
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
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
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
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.
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?
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.