View previous topic :: View next topic |
Author |
Message |
incubi
Joined: 08 Apr 2008 Posts: 8
|
Resolution of a speed controller |
Posted: Thu Apr 24, 2008 1:20 pm |
|
|
Hi all,
I was wondering if someone has a good/simple way of doing this.
I have an AD running 0 - 255 range and I need to control a speed function with a range from 1 – 30 (no float number) so I need to scale down the AD range and still keep the full usage of the pot. I’m not sure how to do it.
incubi |
|
|
umka
Joined: 28 Aug 2007 Posts: 99 Location: New Zealand
|
|
Posted: Thu Apr 24, 2008 1:51 pm |
|
|
one can use something like
Code: | value = read_adc();
int8 value;
value = value / 8; // scales value between 0 and 31
if (value<1)
value=1;
if (value>30)
value=30; |
|
|
|
incubi
Joined: 08 Apr 2008 Posts: 8
|
|
Posted: Thu Apr 24, 2008 2:08 pm |
|
|
Thanks for the input
Would there be any issue with floats or because its an int will it round ok?
I know Hi-Tech has issues with that not sure about CCS.
incubi |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Apr 24, 2008 2:13 pm |
|
|
Divide the A/D result by a floating point scale factor, and then cast
the result to an int8. Test the results by making a loop that goes
through all the input values, and then display the result of the scaling
operation with a printf statement. |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Thu Apr 24, 2008 2:58 pm |
|
|
How about multiplying by 29, dividing by 256 (which the compiler will turn into a shift) and adding 1. These three int operations will be much faster than anything with floats, and with minuscule error. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
incubi
Joined: 08 Apr 2008 Posts: 8
|
|
Posted: Thu Apr 24, 2008 6:21 pm |
|
|
Thanks for the input that gives me some ideas to work with.
incubi |
|
|
incubi
Joined: 08 Apr 2008 Posts: 8
|
Follow up |
Posted: Sun Apr 27, 2008 4:31 pm |
|
|
Just to complete this, it turns out that umka's idea was both simple and worked with no side-effects that I have been able to find. The float math and divide by 29 caused a small delay in the scanning, not sure why.
Thanks for the help.
incubi |
|
|
|