![]() | ![]() | ![]() |
| |||||||
| Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc. |
![]() |
| | Tools |
| | #1 |
|
im using pic basic and it wont let me go less than one eg 0.1 say i have this pw = 150 then pw = pw + 1 or pw = pw - 1 how can i make the value less than 1? i would like to make it 0.1 is there abit of trickery that will alow me to do that? like this pw = (pw + (1/10)) would that work? thanks | |
| |
| | #2 |
|
PICs and most embedded processors only have integer aritmetic units. That means that they don't natively support any fractional numbers. There are a number of ways to get around this limitation but you are probably better off trying to find another way to solve your problem. If you describe what you're doing we can suggest alternatives. | |
| |
| | #3 |
|
Using real numbers is extremely slow, and takes LOT's of code, plus it's also inaccurate - so PIC BASIC's generally don't support them. It's actually very rarely needed, you can scale your calculations and do them as integers - so to get 0.1 resolution simply multily both values by 10, then manually insert the decimal point before you display the value. This is commonly done on all processors, including PC's, particularly for dealing with money values - you would usually convert all figures to pennies, and use integer maths. Again, because floating point maths is too inaccurate. | |
| |
| | #4 |
|
You can use fixed point arithmetic. One byte variable can represent an integer part and the another variable can represent the fractional part. Incrementing the fractional part by 1 increments the overall value by 1/256.
__________________ "Having to do with Motion Control" | |
| |
| | #5 |
|
its actually to move a servo from left to right but i would like to do it in smaller steps. the pw = 150 = 1500uS so i am going up and down 1uS but i would like to go in lower steps there for the servo will move slower i suppose i could put a delay in so it seams the servo is moving more slowly | |
| |
| | #6 | |
| Quote:
| ||
| |
| | #7 | |
| Quote:
PW = 1500 = 1500uS However, PW would have to be 16-bits. I don't know the kind of basic you're using but there are ways around it if you're limited to use 8-bit variables. This is a case where using assembly language will address this issue unambiguously. You didn't mention what PIC you're using but some PICs have a 10-bit hardware PWM generator which would have enough resolution.
__________________ "Having to do with Motion Control" | ||
| |
|
| Tags |
| decrementing, incrementing |
| Thread Tools | |
| Display Modes | |
| |