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.
ATtiny424 ADC Reading from a single Pin
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...
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 ?
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){ // } }
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...
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 ?
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)
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.
Note that the tiny2 series ADCs are very different from those of the traditional series.
Thank you for all for help, ADC is now working.