3.3.3 Code Example for ATtiny817

To quickly implement the method into a real project, generating an Atmel START Project based on the ATtiny817 is recommended.

  • Connect the ATtiny817 XPRO Mini board to the computer via a Mini-USB cable
  • Open Atmel Studio 7.0 and click File → New → Atmel START Example Project
  • Type "ATtiny817" then select the "ATtiny817 Xplained Mini", and click "CREATE NEW PROJECT" in the window
  • Select AVCC as ADC reference and 1.1V internal reference voltage as ADC input, and then click "GENERATE PROJECT"
  • Type "Battery Voltage Measurement without using I/O pin on ATtiny817" as the project name
  • Wait for the completion of the project generation to be finished and then locate the main.c file

The simplest way is to check or update three items based on the generated project.

  1. Let Vbg act as ADC input.
    ADC0.MUXPOS = ADC_MUXPOS_INTREF_gc  /* ADC internal reference, the Vbg*/;
  2. Let VCC act as ADC reference.
    ADC0.CTRLC = ADC_PRESC_DIV2_gc /* CLK_PER divided by 2 */
    			| ADC_REFSEL_VDDREF_gc /* Vdd (Vcc) be ADC reference */
    			| 0 << ADC_SAMPCAP_bp /* Sample Capacitance Selection: disabled */;
  3. Start the ADC and calculate the result.
    float Vcc_value = 0 /* measured Vcc value */;
    ADC0.CTRLA = 1 << ADC_ENABLE_bp /* ADC Enable: enabled */
    	    | 1 << ADC_FREERUN_bp /* ADC Free run mode: enabled */
    	    | ADC_RESSEL_10BIT_gc /* 10-bit mode */;
    ADC0.COMMAND |= 1; // start running ADC
    while(1) {
    		if (ADC0.INTFLAGS)
    		{
    			Vcc_value = ( 0x400 * 1.1 ) / ADC0.RES /* calculate the Vcc value */;
    		}
    	}