CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Resolution of a speed controller

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
incubi



Joined: 08 Apr 2008
Posts: 8

View user's profile Send private message

Resolution of a speed controller
PostPosted: Thu Apr 24, 2008 1:20 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Apr 24, 2008 1:51 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Apr 24, 2008 2:08 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Apr 24, 2008 2:13 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Apr 24, 2008 2:58 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Apr 24, 2008 6:21 pm     Reply with quote

Thanks for the input that gives me some ideas to work with.

incubi
incubi



Joined: 08 Apr 2008
Posts: 8

View user's profile Send private message

Follow up
PostPosted: Sun Apr 27, 2008 4:31 pm     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group