ATtiny424 ADC Reading from a single Pin

Go To Last Post
10 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hii all,
I'm using ATtiny424  and want to read ADC value from a defined pin. Can anyone help me out with an example code.

Thanks & Regard
Rajendrasinh Chhasatiya

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Have you studied the control registers associated with the ADC???---they really contain the info you need

 

This will give you plenty of ideas.  Start out using single-conversion mode (you tell the ADC to do a conversion & then it gives you a result) 

 

http://ww1.microchip.com/downloa...   (see code examples at the end)

 

https://ww1.microchip.com/downlo...

 

 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


My brief scanning of the datasheet suggests that this:

 

 

combined with this:

 

 

should tell you all you need to know. Which pin were you hoping to use ?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

As an example, here is an excerpt of the code I am using on tiny824.
The voltage divided by the thermistor is read with reference to VDD.

You can see what you're doing by looking at the datasheet, so I won't go into further detail.
I'm not good at English.

 

int main(void){
    wdt_reset();
    sys_init();

    // ADC_init(MAIN_CLK = 2MHz, ADC_CLK = 333kHz)
    ADC0.INTCTRL = ADC_RESRDY_bm;
    ADC0.MUXPOS = ADC_MUXPOS_AIN5_gc;
    ADC0.CTRLF = ADC_FREERUN_bm | ADC_SAMPNUM_ACC512_gc;
    ADC0.CTRLC = ADC_REFSEL_VDD_gc;
    ADC0.CTRLB = ADC_PRESC_DIV6_gc;
    ADC0.CTRLA = ADC_LOWLAT_bm | ADC_ENABLE_bm;
    ADC0.COMMAND = ADC_MODE_BURST_SCALING_gc | ADC_START_IMMEDIATE_gc;
    sei();

    // MAINLOOP ------------------------------------------------------
    while (1){
        wdt_reset();
    }
}

////////////////////////////////////////////////////////////////
// ADC INT
////////////////////////////////////////////////////////////////
ISR(ADC0_RESRDY_vect){
    uint16_t res = ADC0.RESULT;

//  if ((res > foo){

//  }
}

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

avrcandies wrote:

Have you studied the control registers associated with the ADC???---they really contain the info you need

 

This will give you plenty of ideas.  Start out using single-conversion mode (you tell the ADC to do a conversion & then it gives you a result) 

 

http://ww1.microchip.com/downloa...   (see code examples at the end)

 

https://ww1.microchip.com/downlo...

 

 

Thanks i will go through it. 

Thanks & Regard
Rajendrasinh Chhasatiya

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:

My brief scanning of the datasheet suggests that this:

 

 

combined with this:

 

 

should tell you all you need to know. Which pin were you hoping to use ?

I am going to used PA7 (AIN7) pin of ATtiny424.

Thanks & Regard
Rajendrasinh Chhasatiya

Last Edited: Wed. Jul 6, 2022 - 10:29 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It's not so dificult to set it up:

 

void initAdc() {
    // setup references for the ADC
    VREF.CTRLA = VREF_ADC0REFSEL_4V34_gc;  // ADC reference 4.34V
    VREF.CTRLB = VREF_ADC0REFEN_bm;        // enable reference
    
    // ADC settings: enable, run in standby mode
    ADC0.CTRLB = ADC_SAMPNUM_ACC4_gc;                       // accumulate 4 samples per measurement
    ADC0.CTRLC = ADC_REFSEL_INTREF_gc | ADC_PRESC_DIV2_gc;  // internal reference, prescaler :2 (low freq)
}

uint16_t readAdcCh(uint8_t channel) {
    ADC0.MUXPOS = channel;                            // select channel
    ADC0.COMMAND = ADC_STCONV_bm;                     // start conversion
    while (bit_is_set(ADC0.COMMAND, ADC_STCONV_bp));  // wait until the flag gets cleared (uses bit position - *_bp)
    return ADC0.RES;                                  // return the result (reading from it also clears the ADC_RESRDY flag)
}

 

Internal 4.3V reference is used (I used also DAC with 4.3V ref, but I removed it here) as I needed something more precise than Vcc, and I also used sample accumulation for better accuracy (it does 4 samples in one go)

 

Computers don't make errors - What they do they do on purpose.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

A lot of the "complexity" stems from them giving you way too many options (more and more and now even more) to consider---it's option overload!

So look at the datasheet and scribble away anything that isn't needed for a basic conversion...do you need 20 different clocking options and 10 power saving modes?  Probably not, ignore such things (at least for now).

This will rapidly whittle down to a few choices that you can keep in your head to understand what needs done.

 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Note that the tiny2 series ADCs are very different from those of the traditional series.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you for all for help, ADC is now working.yes

Thanks & Regard
Rajendrasinh Chhasatiya