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.

Using a/d converters

Status
Not open for further replies.

akahrim

Member
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?
 
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:
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". https://gcc.gnu.org/wiki/avr-gcc
 
Last edited:
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:
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". https://gcc.gnu.org/wiki/avr-gcc

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.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top