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

What is faster? ADC() conversion and multiplication + divis.

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



Joined: 10 May 2005
Posts: 323
Location: Belgium

View user's profile Send private message

What is faster? ADC() conversion and multiplication + divis.
PostPosted: Tue May 23, 2006 2:59 am     Reply with quote

Hi,

here is my routine to measure analog Battery Voltage:

Code:
//***********************Meten Batterijspanning*******************************//
//Vbat = (150+120)/120*waarde*3/1023
//digitale waarde = 10 bits
void Meet_Vbat()
{
   setup_adc_ports(AN0_AN1_AN3);
   setup_adc(ADC_CLOCK_DIV_32);
   set_adc_channel( 0 );
   delay_us(20);
   Vbat = read_adc() * (270.0 / 120.0) * (3.0 / 1023.0);
   setup_adc(ADC_OFF);
   setup_adc_ports(NO_ANALOGS);
}


Now my Q is about the line:

Code:
Vbat = read_adc() * (270.0 / 120.0) * (3.0 / 1023.0);


wouldn't it be faster to do the arithmetic at first and stating this:

Code:
Vbat = read_adc() * 0.006598240469;
Ttelmah
Guest







PostPosted: Tue May 23, 2006 4:01 am     Reply with quote

If you have 'constants' like this, the compiler works them out at compile time, _provided_ the arrangement is such that this part of the equation can obviously be solved first.
So:

Vbat = read_adc() * ((270.0 / 120.0) * (3.0 / 1023.0));

(notice the extra pair of brackets), will result in exactly the same code as using your 'presolved' version.:-)

As written, the compiler is not quite smart enough to realise that the whole block can be solved.
As a seperate comment, consider using integers instead. If you use integer 1/100th volts, then you can use integer arithmetic. Much faster and smaller, and also quicker for tests/printouts too. The compiler does have the ability to print an integer as a 'scaled' value. Might be worth considering.
As a final comment, the value at the end of the equation, should be 1024, not 1023. A ten bit ADC, gives 1024 'steps', with the full scale voltage (3v), never actually being reached. If it was, it'd be a count of 1024, not 1023, with the transition exactly 'at' the full scale voltage.

Best Wishes
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