ADC attiny817

Go To Last Post
9 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
#include <stdio.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>

#include <stdlib.h>

uint16_t results;

int main(void)

{

	CPU_CCP= CCP_IOREG_gc; //Un-protect protected I/O registers
	CLKCTRL_MCLKCTRLB =!(CLKCTRL_PEN_bm);	//OFF Prescaler

	sei();		

	//*********************************ADC*******************************************************

	PORTA.DIRCLR = PIN7_bm;

	ADC0.CTRLA |= ADC_RESSEL_bm; //8 bit
	ADC0.CTRLA |= ADC_FREERUN_bm; // FREE RUN

	VREF.CTRLA |= VREF_ADC0REFSEL_1V1_gc;   // dont work :(

	//VREF.CTRLB |= VREF_ADC0REFEN_bm;
	//ADC0.CTRLC |= ADC_SAMPCAP_bm;
	ADC0.CTRLC = ADC_PRESC_DIV32_gc;  //32 PRESCALER
	ADC0.MUXPOS = ADC_MUXPOS_AIN7_gc; //MUX

	ADC0.CTRLA |= ADC_ENABLE_bm;  // ADC ON

	ADC0.COMMAND |= ADC_STCONV_bm; // START  

	//***************************D A C **************************************************************

	//***************************************************

    while (1)
    {

		results = ADC0.RES;
		_delay_ms(1500);

	}

}

//**********************************************************************************************************************************************

Attiny817

Anyone know how to turn internal reference source, above 0.55V? I can't change it :(

This topic has a solution.
Last Edited: Mon. Oct 9, 2017 - 05:42 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

AT_TRUCK wrote:
VREF.CTRLA |= VREF_ADC0REFSEL_1V1_gc; // dont work :(

 

What register is used to set VREF?

 

See 30.5.3 in the datasheet

 

Jim

 

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

I try with  ADC0.CTRLC = ADC_REFSEL_INTREF_gc; but the bits are automatically set to zero so there is an internal reference source.

 

Automatic reference source is internal 0.55V,  change   ""VREF.CTRLA |= VREF_ADC0REFSEL_1V1_gc;"" register does not change anything.

 

I use 18.1 in the datasheet. 

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

Those bits are not in CTRLA, the page I posted is for CTRLC!

 

Jim

 

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

My mistake, those bits select VDD or internal reference....  

 

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

It may be you have enable freerun mode, and the ADC will not accept a change in vref while running, so either do not enable freerun and use single start conversion mode,

or setup vref before enabling freerun.

 

Jim

 

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

I tried many options and nothing works :( I use xplained mini.

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

I found a solution - the voltage for the ADC came from the DAC
(earlier from the divisor and had similar problems)
In the DAC I have typed VREF.CTRLA =  VREF_DAC0REFSEL_1V1_gc;
should be                      VREF.CTRLA |= VREF_DAC0REFSEL_1V1_gc;

difference " |= "

This error resets the registry CTRLA.

I'm not understand the difference between "|="  and  "="

Last Edited: Mon. Oct 9, 2017 - 05:15 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Aw, that would do it! 

The "|=" is a read/modify/write which "or"'s in the 1 bits and writes the results back.

while the "=" just assigns the value of all bits to the register, possibly changing all bit values.

 

Glad you found it.

 

Jim