# Using a/d converters

Discussion in 'AVR' started by akahrim, Sep 19, 2013.

1. ### akahrimMember

Joined:
Apr 4, 2013
Messages:
49
Likes:
0
Location:
USA
Hello All,

I am working on writing a microcontroller (Attiny24) program using C++ and programmer's notepad. When the ADC performs a conversion, the microcontroller stores the value in the ADCH and ADCL registers. These are both 8-bit registers. The ADC has 10 bits of resolution. Currently I'm taking the values from both registers and putting it into one 16-bit variable. Once I have the 16-bit binary value from the ADC (lets call this variable 16_bit_result), I would like to multiply it by .02 and store this value in another variable (Result_of_16_bit_multiplied_by_pointzerotwo). Can I just perform the following?

Create new variable - double Result_of_16_bit_multiplied_by_pointzerotwo

then perform the following calculation

Result_of_16_bit_multiplied_by_pointzerotwo = 16_bit_result*.02

Or do I need to do any typecasting?

2. ### misterTWell-Known MemberMost Helpful Member

Joined:
Apr 19, 2010
Messages:
2,697
Likes:
368
Location:
Finland
You can do that.. But the result will be truncated to the nearest integer. If your ADC result is 49 and you multiply that by 0.02 (which is same as dividing by 50), the result will be zero. 99 * 0.02 will result one etc.

Edit: Sorry my mistake. I did not spot the fact that your result variable is a double. In that case the result should be accurate decimal number. The compiler should take care of the typecast.

You could do a typecast just to be sure:
Code (C):

double Result_of_16_bit_multiplied_by_pointzerotwo;
Result_of_16_bit_multiplied_by_pointzerotwo = ((double)16_bit_result)*.02;

I would use "float" instead of double. And even then the calculation will take a relatively long time, but that is a problem only if you are sampling signals at high rate.

Edit2: If you are using AVR-GCC compiler then: "double is only 32 bits wide and implemented in the same way as float". http://gcc.gnu.org/wiki/avr-gcc

Last edited: Sep 19, 2013
3. ### akahrimMember

Joined:
Apr 4, 2013
Messages:
49
Likes:
0
Location:
USA
Thanks alot for the response. I will be working on trying this out this week and seeing how the different variable types will affect the calculation time. Thanks again for your feedback.