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.

10 bit ADC to 16bit

Status
Not open for further replies.

fatburger92

New Member
Hey I have a quick question regarding ADC output (newbie). After doing my AD conversion, I have my value stored in ADCH and ADCL. I have defined h as a unsigned char of 16 bits (uint_16t h), but how do I get the value stored in h (as shown below) to convert from 10bits to 16bits?

h= (ADCH << 2) | (ADCL >> 6);
printf("03X", h);
 
Are you absolute sure that your C compiler does not allow you to read the 10-bit ADC result into an unsigned int in one go?

Many C compilers are able to do just that. User can, but does not need to read both the low byte and high byte and merge them together.

What MCU and compiler are you using?

Added: If you are using C18 Microchip compiler, you just need to include the header file adc.h and then get the ADC result simply by writing h=ReadADC();
 

Attachments

  • C18_read_ADC.gif
    C18_read_ADC.gif
    14.2 KB · Views: 309
Last edited:
I am using the Atmega32. So, instead of

h=(ADCH<<2)|(ADCL>>6);

I would just have to go

#include <adc.h>
h=ReadADC() ;
printf("03X", h);

However, I am not sure that my compiler allows for this function. Would I then have to create a subprogram which converts my 10 bit number to 16 bits?
 
You still have not told us which C compiler you're using so I will assume you are using the free GCC WINAVR compiler.

In the include header file "io.h", which then includes "iom32.h", ADC is defined as 16-bit and with address points to the ADCH+ADCL pair.

Therefore one can simply use the following code constuction to get the 10-bit value into variable h. The "#include <adc.h>" is for PIC only and will not be needed here.

Code:
  h = ADC;

The following is something I read from the net which confirms what I wrote above.

ADC value is read via direct access. Even though the variable is 16 bit and ADC refers to two registers that must be read individually, AVR-GCC graciously handles these low level register reading and assignment operations.
 
Thanks a lot for the help! Yes, you're right, I have the GCC AVRstudio compiler. I tried that, but on my screen (after I put my code through), I only get a bunch of 0's outputting?

#include "defines.h"

#include <ctype.h>
#include <stdint.h>
#include <stdio.h>

#include <avr/io.h>
#include <avr/pgmspace.h>

#include <util/delay.h>

#include "uart_lib.h"

/* Global variable to establish the stream information for the UART. */
FILE uart_str = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);

/**********************************************************************/
int main(void) {
uint8_t c, i, delaycount, doneflag, abortflag;
uint16_t h;

/* Initialize the UART. */
uart_init();

/* Establish the default streams to use the uart. */
stdout = &uart_str;
stdin = &uart_str;
stderr = &uart_str;

/* Initialize the ADC (using pin ADC0) as an input pin. */
DDRA = 0x00;

PORTA = 0x00;

ADCSRA |= (1 << ADPS1) | (1 << ADPS0); /* Setting the prescalar to 8 so that the clock is between 50kHz and 250kHz (using a 1MHz default system frequency */


ADMUX |= (1 << REFS0);

/* An infinite loop to continuously display the hex value*/

while (1)

ADCSRA |= (1 << ADEN);
ADCSRA |= (1 << ADSC);


h= ADC;

printf("03X", h);
return (0); }
 
A few things come to mind.

Is your system clock 1MHz or else you have to select the proper ADC clock.

Have you placed a DC voltage between 0~5V into the ADC channel 0 pin for test?

Is you USART routine working? Can you print a constant value like "1023" using it?

Your infinite While loop at the end is wrong. It is missing the braces and it will not do what you have intended.

I would suggest you follow the tutorial at the AVRFreaknet which provide step by step instruction on how to make the ADC channel 0 works using a LED and a port pin for ADC result indication. Once you have got that working, it will be a simple matter to come back to your own.

Tutorial: Guide to the AVR ADC
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top