Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
Tools
Old 14th January 2005, 12:41 AM   #1
Default incrementing and decrementing less than "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
lompa is offline  
Old 14th January 2005, 02:18 AM   #2
Default

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.
bmcculla is offline  
Old 14th January 2005, 06:47 AM   #3
Default

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.
__________________
PIC programmer software, and PIC Tutorials at:
http://www.winpicprog.co.uk
Nigel Goodwin is offline  
Old 14th January 2005, 09:25 AM   #4
Default

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"
motion is offline  
Old 14th January 2005, 05:35 PM   #5
Default

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
lompa is offline  
Old 14th January 2005, 05:47 PM   #6
Default

Quote:
Originally Posted by lompa
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
You will have to put a delay in it to make it move more slowly, really small changes won't move it at all - there's a certain amount of 'dead band' to prevent the servo hunting.
__________________
PIC programmer software, and PIC Tutorials at:
http://www.winpicprog.co.uk
Nigel Goodwin is offline  
Old 15th January 2005, 12:26 AM   #7
Default

Quote:
Originally Posted by lompa
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
You only have to scale the values correctly. Let the value of 1 in PW represent 1 uS. Therefore:

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"
motion is offline  
Reply

Tags
decrementing, incrementing

Thread Tools
Display Modes




All times are GMT. The time now is 02:31 AM.


Electronic Circuits  |  Learning Electronics
eXTReMe Tracker