ADC attiny817

Go To Last Post
16 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

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

  • 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

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

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

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

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

  • 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

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

  • 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

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

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

By default VREF  is selected as Reference.

Thanks,

Jaivvignesh | Associate Firmware Developer

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

You could've waited til tomorrow - then celebrated the thread's 2nd birthday!

 

laugh

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

.

Last Edited: Wed. Oct 9, 2019 - 06:17 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I would suggest to  check/read the date on that post ...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

.

Last Edited: Wed. Oct 9, 2019 - 06:18 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

but it was already answered 2 years ago - in #9 - and that has already been marked as the solution.

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

my mistake, sorry...havent noticed.

 

EDIT: all deleted, so now you can be happy.

Last Edited: Wed. Oct 9, 2019 - 06:18 AM