Go Down

Topic: ADC Conversion Time of ATmega328 of Arduino UNO (Read 5144 times) previous topic - next topic

GolamMostafa

May 31, 2017, 03:46 am Last Edit: Jun 03, 2017, 08:08 pm by GolamMostafa
It was (is) my question to myself. I could never had (have) satisfactory answer. Now, looking at the Forum members.

Assume that the ADC's Clock is 122 Hz 488 Hz. VREF is 5V. Input voltage at Ch-5 is 2.50V. The queries are:

(a) How many clock pulse would be required to finish conversion?
(b) How many data bits the ADC will produce (10 bit or less!)? What is the value of ADC?
(c) If the conversion process is changed into Continuous Conversion Mode, what is the
    sampling rate? By sampling rate, I have wanted to know: How many times the input signal
    is being sampled for conversion?

I had been playing and making applications of ADC for many many years; unfortunately, I could never make the above-mentioned points cleared neither reading Text Books nor Data Sheets and even the Cook Books!  

Robin2

Assume that the ADC's Clock is 122 Hz. VREF is 5V. Input voltage at Ch-5 is 2.50V. The queries are:
The datasheet for the Atmega328 says
Quote
By default, the successive approximation circuitry requires an input clock frequency between 50
kHz and 200 kHz to get maximum resolution.
so I don't know what you mean by 122Hz.

Is that the frequency at which you want to take samples rather than the frequency of the ADC clock?

And it also says clearly
Quote
A normal conversion takes 13 ADC clock cycles.
and
Quote
In Free Running mode, a new conversion will be started immediately after the conversion com-
pletes
...R
Two or three hours spent thinking and reading documentation solves most programming problems.

GolamMostafa

#2
May 31, 2017, 12:50 pm Last Edit: Jun 03, 2017, 08:33 pm by GolamMostafa
Quote
so I don't know what you mean by 122Hz.
The issue of 122 Hz 488 Hz (clkADC) has appeared in my mind from the achievable division factors as have been laid down in System Clock Prescaler and ADC Clock Prescaler.


Theoretically on paper and pencil, I think we can operate the ADC at this very low clocking speed ((16 MHz/1024256)/128 = 122 Hz488 Hz). If so, my query is to know the number of clock pulses that the ADC will take to finish conversion. I acknowledge the operating frequency range suggested by the manufacturer which I had always maintained at 125 KHz in my works.

I have also confusion over the 'Resolution' terminology. In the context of ADC, as far as I know for myself, the resolution refers to the voltage allocated for the LS-bit of the ADC's value. Therefore, for the 10-bit ADC, whether it is running at 122 Hz488 Hz or 125 KHz, it will keep pumping bits until the match occurs for the given input. The resolution will remain the same for any input voltage (0 to Full Scale).

So, whenever I hear people saying about 'maximum resolution of ADC' I get confused as I can not correlate the meaning with what I have in my collection.  


Robin2

Theoretically on paper and pencil, I think we can operate the ADC at this very low clocking speed ((16 MHz/1024)/128 = 122 Hz).
I have not investigated that. I can't understand why you would want such a slow conversion.

Quote
If so, my query is to know the number of clock pulses that the ADC will take to finish conversion.
I have answered that.

Quote
I have also confusion over the 'Resolution' terminology. In the context of ADC, as far as I know for myself, the resolution refers to the voltage allocated for the LS-bit of the ADC's value. Therefore, for the 10-bit ADC, whether it is running at 122 Hz or 125 KHz, it will keep pumping bits until the match occurs for the given input. The resolution will remain the same for any input voltage (0 to Full Scale).
Unless you want to do a very fast conversion it will always work at 10 bit resolution.


This all sounds to me like another XY problem. If you explain what you are trying to do it will be much easier to help.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

GolamMostafa

I have taken your all opinions with good respect in my account. This section of the Forum is a 'General Discussion' Section of free thought. Therefore, I would be collecting opinions if these are luckily available in response to my queries. The very spirit of this Section is such that it does not bring its discussants into collision. 

I will try to evaluate the received opinions with appropriate experimental setup using the Arduino rather than entering into debate with huge 'ignorance' of my own in the background. At suitable time, I would place the findings back in the Forum for kind analysis of the respondents.

The technical queries that we place in the Forum could be easily explained and answered by the people who were involved in the design and development of these devices; but, they are hardly seen. This is a great surprise to me. For example: I never had the answer to my query: Why does the 8086 contain a pre-fetch queue of only 6-byte when the length of an 8086 instruction could be as long as 7-byte?     

Robin2

#5
May 31, 2017, 09:07 pm Last Edit: May 31, 2017, 09:09 pm by Robin2
The technical queries that we place in the Forum could be easily explained and answered by the people who were involved in the design and development of these devices; but, they are hardly seen.
Atmel makes microprocessors by the million. I don't imagine they want their engineers wasting time on this Forum for hobbyists who use maybe a few thousand microprocessors between them.

They did get their engineers to write detailed and accurate datasheets that describe the products - including the 3 questions you asked in your Original Post and which I have answered.


...R
Two or three hours spent thinking and reading documentation solves most programming problems.

CrossRoads

Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Robin2

The purpose of this post is to understand few statements of the ATmega328 data sheet
I don't know the answers to these questions and frankly, I have no idea why the answers would make a difference, even if I did know.

They seem like questions that should be addressed to the folks at Atmel.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

GolamMostafa

#8
Jun 04, 2017, 06:46 pm Last Edit: Jun 04, 2017, 08:03 pm by GolamMostafa
I appreciate your answer and concern; these are in fact related to academic affairs, and they have nothing much to do with practical applications. I have got an opportunity through Arduino Forum to place my queries.

I attended, many years ago, at Schlumberger's Research Center at Houston and GEC Establishments in UK where I saw veteran Research Scientists to work like hobbyists. Hobbyists are free from all kinds of stresses; they work in ways they like; they carefully avoid all kinds of collisions with their fellows and others!    

Robin2

I have got an opportunity through Arduino Forum to place my queries.
The response from other members of the Forum has been a bit under-whelming :)

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

GolamMostafa

#10
Jun 05, 2017, 04:18 pm Last Edit: Oct 30, 2017, 06:07 am by GolamMostafa
The purpose of this post is to understand few statements of the ATmega328 data sheet in connection with ADC Module against Experimental Data

1. With regards to ADC Module of ATmega328, the data sheet says:



(a) The first conversion after the ADC is switched on (i.e., ADCSRA.ADEN is written to 1)
takes 25 ADC clock cycles in order to initialize the analog circuitry (Figure 28-4).

(b) Remark: In Figure 28-4, we observe that following the ADC Start
Command, 13.5 ADC Clock Cycles are spent to arrive up to the point of Sample-Hold. There
remains 25.0 - 13.5 = 11.5 cycles. The ADC is a SAR-type 10-bit converter. Assuming that the
input is of any part of Full Scale, the ADC will start from Vref/2 (1000 0000 00) and then manipulate
this number as needed until match occurs with the input analog voltage.

(c) Let us review the working principle of SAR-type A/D converter before we conclude on how many more clkADC pulses (10 or 11.5) would be required to finish conversion.

(d) In SAR method of 10-bit A/D conversion, there are 10 binary-weighted blocks 512, 256, 128, 64, 32, 16, 8, 4, 2, and 1. The MS-block of 512 is tried first. If it is too high than the input voltage, it is set aside and a 0 is recorded for bit-9 position. Then the next MS-block is tried. If adding a block does not make the intermediate result higher than the input, then a 1 is recorded for that bit. Each block is tried only for once. When all the blocks have been tried, the result is a 10-bit binary for the input voltage.

(e) Let us apply the above conversion principle to the 10-bit SAR-type ADC of the ATmega328. Assume input voltage is 3.78V with Vref = 5V. Expected binary is 11 0000 0101.




clkADC     Tried value             Remark              Partial Binary Result
1             1000 0000 00         Vdac < Vin          1000 0000 00
2             1100 0000 00         Vdac < Vin          1100 0000 00
3             1110 0000 00         Vdac > Vin          1100 0000 00
4             1101 0000 00         Vdac > Vin          1100 0000 00
5             1100 1000 00         Vdac > Vin          1100 0000 00
6             1100 0100 00         Vdac > Vin          1100 0000 00
7             1100 0010 00         Vdac > Vin          1100 0000 00
8             1100 0001 00         Vdac < Vin          1100 0001 00
9             1100 0001 10         Vdac > Vin          1100 0001 00
10           1100 0001 11         Vdac < Vin          1100 0001 01   = Final Binary Result  

We observe that 10 more clkADC pulses are enough to finish the conversion! As a result, the net conversion time is 10+1.5 = 11.5 clkADC. But, the data sheet (Figure 28-4, 28-5) and Experimental Data Sheet (Section-5) clearly show that the conversion has taken in 13 clkADC. We may naturally ask, for the shake of better understanding, where does the ATmega328 ADC use the extra 1.5 cycle time?    

(f) Normal conversion, after the conversion of Section-1(a), takes 13 ADC clock cycles (Figure 28-5).

(g)  By default, the successive approximation circuitry requires input clock frequency between 50 kHz
and 200 kHz to get maximum resolution. If a lower resolution than 10 bits is needed, the input
clock frequency to the ADC can be higher than 200 kHz to get higher sample rate. The Arduino setting for clkADC is 125 kHz.

(h)  13 - 260 us conversion time

(i) Remark:
Assuming clkADC 50 kHz, the normal conversion time: 1/50000 * 13 = 260 us

Assuming clkADC 200 kHz, the normal conversion time: 1/200000 * 13 = 65 us

Assuming standard clkADC 125 kHz, the normal conversion time: 104 us. We have
experimentally seen by reading the content of ADCSRA-register that the analogRead(AX)
function uses 125 kHz as clkADC.

How to explain the figure 13 us as mentioned in the data sheet? To achieve a normal conversion
time of 13 us, we need to use clkADC of 1 MHz (1/1000000 * 13 = 13 us) which Atmel does not
recommend!

2. We have done some measurements based on the following setup of Section-4. The experimental data establishes propositions of Section-1(a) and Section-1(b). We could not correlate experimental data with the statement of Section-1(c). Forum members may interpret the presented data and explain the meaning of the statement of Section-1(c).

3. Queries as to Section-1(g):
(a) Why is the restriction of 50 kHZ to 200 kHz. The experimental data of Section-5 shows that the
ADC is quite well converting data with clkADC from 1 MHz 500 kHz to 488 Hz!

(b) What is the meaning of maximum resolution? The experimental data of Section-6 shows that the
resolution (Volt for the LS-bit) remains the same for all permissible clkADC.

(c) What does it mean: Lower resolution than 10-bit. The experimental data of Section-6 shows that
the converted data remains in the domain of 10-bit for all permissible clkADC.


4. The Hardware and Software Flow Chart Setup





5. The Experimental Data Sheet (attachment: ADCConvert.doc)




6. The Arduino IDE Codes (Tested) engaged for the collection of data of Step-5 (attachment: P2410).




(a)  After ADC is started, the Timer-1 is made ON to count high resolution (16 MHz) pulses from the
internal oscillator and continues counting until the conversion is completed. The content of CNT1
is proportional to the very first conversion time of the input analog signal. The content is shown on
LCD TopLeft. Later on, it is converted to clkADC manually and is recorded in the Table of Section-5.

Similarly, the proportional count of conversion time for the next normal conversion is presented
on LCD TopRight.

(b) The ADC value for the first conversion is presented on LCD BottomLeft. The ADC value for
the next normal conversion is shown on LCD BottomRight.  

(c) The clkADC is changed by manipulating the value of CLKPR- and ADCSRA-registers.

(d) The following codes are one-to-one representation of the Flow Chart of Section-4.

(e) The conversion time of the ADC can not be measured using analogRead(AX) function as we
have no idea when this function is activating the 'ADC Start Bit, ADSC' and polling the
'End-of-Conversion Bit, ADIF'.  

Code: [Select]

#include <LiquidCrystal.h>
LiquidCrystal lcd(5, A0, A1, A2, A3, A4);


void  setup()
    {
      
       lcd.begin(16, 2);                    //16 charcaters 2 lines LCD
       //  pinMode(13, OUTPUT);

       CLKPR = 0x80;      // data to enable write into CLKPR register
       CLKPR = 0x00 ;     //data to select clock: clkSYS = 16 MHz
    
      //Timer-1 as an Internal Pulse Counter----
      TCCR1A = 0x00;   //Normal UpCounting Mode
      TCCR1B = 0x00;  //T1 is OFF
      TCNT1 = 0x00;   //Initial Value
    
      bitClear(PRR, 0);            //5V is connected to ADC module
      ADCSRA = 0x97;                //   125 KHz clkADC
      ADMUX = 0x45;                 //    Vref= 5V, Ch5


      bitWrite(ADCSRA, 6, HIGH);          // ADC is started bd ADSC
      TCCR1B = 0x01;                         // Timer-1 is made ON running at 16 MHz
      while (bitRead(ADCSRA, 4) != HIGH)  //Checking EOC by sensing ADIF
        ;
      TCCR1B = 0x00;          //Stop T1
      lcd.setCursor(0, 0);     //Cursor at TopLeft
      lcd.print(TCNT1, 10);   //LCD shows ADC conversion time as Count of 16 MHZ T1's clock
                                     // the count is converted to clkADC in Section-5  
  
      bitWrite(ADCSRA, 4, HIGH);          //ADIF-bit is cleared by putting LH

      int x1 = ADCL;                      // redaing ADC lower 8-bit first
      int x2 = ADCH;

      x2 = x2<<8;
      x2 = x2 | x1;

      lcd.setCursor(0, 1);              //ADC value during first conversion    
      lcd.print(x2, 16);
  
//---- Normal conversion----------    
       TCNT1 = 0x00;

        bitWrite(ADCSRA, 6, HIGH);                //ADC is started bd ADSC
        TCCR1B = 0x01;                              // running at 16 MHz
        while (bitRead(ADCSRA, 4) != HIGH)  //Checking EOC by sensing ADIF
           ;
        TCCR1B = 0x00;                              //Stop T1
        lcd.setCursor(12, 0);
        lcd.print(TCNT1, 10);                        //showing conversion of of normal conversion as counts  
        bitWrite(ADCSRA, 4, HIGH);              //ADIF-bit is cleared by putting LH

        x1 = ADCL;                      
        x2 = ADCH;

        x2 = x2<<8;
        x2 = x2|x1;
        lcd.setCursor(12, 1);
        lcd.print(x2, 16);               //shows ADC value of normal conversion
 
}  

void loop()
{
  
}
    



Go Up