![]() |
|
|||||||
| Proton Plus Compiler v3 Coding problems and general discussion related to the Development Suite |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
#1 |
|
Licensed User
![]() |
Need some quick help with a math issue. I haven't had to do this in a long long time and for some reason I cant wrap my brain around this...
I have an input of 1 through 100 steps. I need it to become 1000 through 2000 steps matching the inputs value. Anyone wanna help my headache?
|
|
|
|
|
|
#2 |
|
Licensed User
![]() Join Date: May 2008
Location: Sydney, Australia
Posts: 1,453
![]() |
Look at three things:
-What is the min. input? -What is the input range? -What is the output range? -What is the offset? I notice you specify 1-100, rather than 0-100. I will assume that was intended. Min input: 1 Input range: 99 Output range: 1000 Offset: 1000 Formula is therefore: out=(in-1)*(1000/99)+1000 Examples: In=100, Out=2000 In=1, Out=1000 In=50.5, Out=1500 (Remember 50.5 is between 1 and 100, not 50) |
|
|
|
|
|
#3 |
|
Licensed User
![]() |
Thanks wastrix! yes it was intended to be 1-100. if it was a 0 -100 would this be the change?
out=in*(1000/100)+1000 Thanks again. |
|
|
|
|
|
#4 |
|
Licensed User
![]() Join Date: May 2008
Location: Sydney, Australia
Posts: 1,453
![]() |
That's correct.
|
|
|
|
|
|
#5 |
|
Licensed User
![]() Join Date: Aug 2004
Location: Central Scotland
Posts: 1,599
![]() |
Or maybe not.
![]() By my calculation 1-100 inclusive is a range of 100 (0 is your reference point) Always confusing when you work with arrays and such which start with element x(0). Try counting on your fingers. If you get stuck, use your toes as well.
Last edited by bill9399; 8th February 2010 at 09:53. |
|
|
|
|
|
#6 |
|
Licensed User
![]() Join Date: Oct 2005
Location: Crime riddled ZA
Posts: 7,495
![]() |
Don't do the whole formula in one
out=in*(1000/100)+1000 Break it into it's individual steps, it's faster and you use less code. |
|
|
|
|
|
#7 |
|
Licensed User
![]() |
One more issue I keep banging my head with is interupts. I need to make one thats uses TMR1? to keep a contant 50 hertz pulse (servo) while allowing the main code to run. my main code is...
Code:
Device 16F88
Xtal 20
@CONFIG_REQ
@__CONFIG _CONFIG1, CP_OFF & CCP1_RB0 & DEBUG_OFF & WRT_PROTECT_OFF & CPD_OFF & LVP_OFF & BODEN_OFF & MCLR_OFF & PWRTE_ON & WDT_OFF & HS_OSC
@__CONFIG _CONFIG2, IESO_ON & FCMEN_ON
All_Digital = TRUE
CMCON=7
TRISA = %00000001
TRISB = %00000000
Symbol Pin = PORTA.1 ' Alias servo out pin
Symbol LED1 = PORTB.0
Symbol LED2 = PORTB.1
Symbol LED3 = PORTB.2
Symbol LED4 = PORTB.3
Symbol LED5 = PORTB.4
Symbol LED6 = PORTB.5
Symbol LED7 = PORTB.6
Symbol LED8 = PORTB.7
Symbol LED9 = PORTA.2
Symbol LED10 = PORTA.3
PORTA.1 = 0
PORTA.4 = 0
Dim p_one As Word
Dim p_two As Word
Dim duty As Dword
Dim out As Word
Dim Calc1 As Word
Dim Calc2 As Word
out = 1000
'main program loop here
main:
p_one = 0 ' reset counter values
p_two = 0
p_one = PulsIn PORTA.0, High
DelayMS 10
p_two = PulsIn PORTA.0, Low
duty = (p_one * 100)
duty = duty / (p_one + p_two)
Calc1= (1000/99)+1000
Calc2= duty-1
out= Calc2 * Calc1
'****** This needs to be updates at 50 hertz by interupt****
Servo Pin , out
'***********************************************
If duty >= 1 Then LED1 = 1 : Else : LED1 = 0
If duty >= 10 Then LED2 = 1 : Else : LED2 = 0
If duty >= 20 Then LED3 = 1 : Else : LED3 = 0
If duty >= 30 Then LED4 = 1 : Else : LED4 = 0
If duty >= 40 Then LED5 = 1 : Else : LED5 = 0
If duty >= 50 Then LED6 = 1 : Else : LED6 = 0
If duty >= 60 Then LED7 = 1 : Else : LED7 = 0
If duty >= 70 Then LED8 = 1 : Else : LED8 = 0
If duty >= 80 Then LED9 = 1 : Else : LED9 = 0
If duty >= 99 Then LED10 = 1 : Else : LED10 = 0
GoTo main
End
|
|
|
|
|
|
#8 |
|
Licensed User
![]() Join Date: May 2008
Location: Sydney, Australia
Posts: 1,453
![]() |
@Fanie: If you'd be doing the whole thing in PDS anyway, that's debatable.
@Bill: Maybe, but in statistics range is defined as xMax - xMin, not inclusive. @DaOne: You should probably start a new thread for this. |
|
|
|
|
|
#9 |
|
Licensed User
![]() Join Date: Oct 2005
Location: Crime riddled ZA
Posts: 7,495
![]() |
Easy if you use a PWM pic and a 4017 devide by 10.
If you run the PWM at 500Hz / 10 (4017) you have 50HZ. This is the easiest and most convenient way. It will free the pic up to do other things while the PWM is maintained in the backround. You can do it using interrupts if the 25c for the 4017 is too expensive
|
|
|
|
|
|
#10 | |
|
Licensed User
![]() |
Quote:
Will do... patiently waiting for your answer
Last edited by DaOne; 8th February 2010 at 10:46. |
|
|
|
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Scaling Math Confusion | thetrueman | Proton Plus Compiler v3 | 6 | 27th September 2009 17:55 |
| External I2C EEPROM table or float point math? | pinko | Proton Plus Compiler v3 | 0 | 24th July 2009 16:03 |
| Help with MATH needed.. | captainslarty | The Lounge | 85 | 21st February 2009 12:53 |
| Interrupts and math | spyder0069 | Proton Plus v2.1.5.3 General Discussion | 8 | 18th December 2004 08:31 |
| Floating point math? 32 bit Math? | AlienRelics | Wish List / Product Feedback | 0 | 4th July 2002 23:57 |