Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

incrementing and decrementing less than "1"

Status
Not open for further replies.

lompa

New Member
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
 
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.
 
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.
 
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.
 
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 said:
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.
 
lompa said:
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.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top