2.3 Data Visualizer and Power Debugging Demo

This section shows a demo using the Data Visualizer, including Power Debugging.

Getting Started Topics

Video: Data Visualizer and Power Debugging Demo

/*
* Power_Demo_ADC_SleepWalking.c
* Device/board: ATtiny817 Xplained Pro
* Created: 8/6/2017 3:15:21 PM
*/ 

#include <avr/io.h>
#include <avr/interrupt.h> 
#include <avr/sleep.h>

#define F_CPU (20E6/2)

void sys_init(void)
{
       _PROTECTED_WRITE(CLKCTRL.MCLKCTRLB, CLKCTRL_PEN_bm | CLKCTRL_PDIV_2X_gc);
}

void rtc_pit_init(void)
{
       RTC.CLKSEL = RTC_CLKSEL_INT1K_gc;
       RTC.PITCTRLA = RTC_PITEN_bm | RTC_PERIOD_CYC256_gc;
}

//picoPower 4: Event system vs. IRQ. Compare to not using IRQ 
void evsys_init(void)
{
       EVSYS.ASYNCCH3 = EVSYS_ASYNCCH3_PIT_DIV128_gc;
       EVSYS.ASYNCUSER1 = EVSYS_ASYNCUSER1_ASYNCCH3_gc;
}

//picoPower 3: Evaluate own sample, e.g. window mode. 
//             Significantly reduce awake time. 
   
void adc_init(void)
{
       ADC0.CTRLC  = ADC_PRESC_DIV8_gc | ADC_REFSEL_VDDREF_gc;  
       ADC0.CTRLA  = ADC_ENABLE_bm | ADC_RESSEL_8BIT_gc;
       ADC0.MUXPOS = ADC_MUXPOS_AIN6_gc;
       
	ADC0.CTRLA |= ADC_RUNSTBY_bm;       //picoPower 1: So can run in sleep. 
       ADC0.CTRLE = ADC_WINCM_OUTSIDE_gc;  //picoPower 3: So can evaluate own sample. 
       ADC0.INTCTRL = ADC_WCMP_bm;
       ADC0.WINHT = 200;
       ADC0.WINLT = 100;

       ADC0.EVCTRL = ADC_STARTEI_bm;       //picoPower 4: So event can trigger conversion
}

uint8_t adc_get_result(void)
{
       return ADC0.RESL;
}

//picoPower 5: Send quickly, then back to sleep: compare 9600, 115200, 1250000 baud rates
//note only sending 1 byte
#define BAUD_RATE 57600   
void usart_init()
{
       USART0.CTRLB = USART_TXEN_bm;
       USART0.BAUD = (F_CPU * 64.0) / (BAUD_RATE * 16.0);
}
void usart_put_c(uint8_t c)
{
       VPORTB.DIR |= PIN2_bm | PIN6_bm;  //picoPower 2b: see Disable Tx below
       USART0.STATUS = USART_TXCIF_bm;
	   
	VPORTB.OUT |= PIN6_bm;
       USART0.TXDATAL = c;
       while(!(USART0.STATUS & USART_TXCIF_bm));
	VPORTB.OUT &= ~PIN6_bm;
       VPORTB.DIR &= ~PIN2_bm | PIN6_bm;
                     //picoPower 2b: Disable Tx pin in-between transmissions 
}

//picoPower 2: Disable unused GPIO
//             compare: Nothing, PORT_ISC_INPUT_DISABLE_gc, PORT_PULLUPEN_bp

void io_init(void)
{
       for (uint8_t pin=0; pin < 8; pin++)
       {  
              (&PORTA.PIN0CTRL)[pin] = PORT_ISC_INPUT_DISABLE_gc;
              (&PORTB.PIN0CTRL)[pin] = PORT_ISC_INPUT_DISABLE_gc;
              (&PORTC.PIN0CTRL)[pin] = PORT_ISC_INPUT_DISABLE_gc;
       }
}

int main(void)
{
       sys_init();  
       rtc_pit_init();
       evsys_init();
       adc_init();
       io_init();
       usart_init();
	   
	VPORTB.DIR |= PIN6_bm;
	VPORTB.OUT &= ~PIN6_bm;
       sei();

       //picoPower 1: Go to sleep.  Compare with no sleep, IDLE and STANDBY
       set_sleep_mode(SLEEP_MODE_STANDBY);

	while (1)
	{
          sleep_mode();
	}
}

ISR(ADC0_WCOMP_vect)       //picoPower 3: Only called if relevant sample
{
       ADC0.INTFLAGS = ADC_WCMP_bm;
       usart_put_c(adc_get_result());
}