/* This example shows how to configure TCC0 for basic timer operation. The timer will run at the main clock frequency (32MHz) so we need to do a bit of math to derive the number of clock cycles to arrive at the desired microsecond time out. */ void startTimer(uint16_t microseconds) { uint32_t cycles; // There are 31.25 nano seconds per clock cycle. // So we multiply the micro seconds by the clock period to determine // the number of clock cycles to achieve the microsecond timeout. // That number of cycles becomes our TOP value. // We will use 32 nano seconds to preclude floating point arithmetic. cycles = 32*microseconds; // Set period/TOP value. TCC0.CCA = cycles; // Set the CNT to 0. TC_SetCount(&TCC0, 0); /* Enable Input "Capture or Compare" channel A. */ TC0_EnableCCChannels( &TCC0, TC0_CCAEN_bm ); // Select clock source and start the timer. TC0_ConfigClockSource( &TCC0, TC_CLKSEL_DIV1_gc ); // Enable CCA interrupt. TC0_SetCCAIntLevel( &TCC0, TC_CCAINTLVL_LO_gc ); PMIC.CTRL |= PMIC_LOLVLEN_bm; }
void battery_init() { PORTD.DIRSET = PIN4_bm | PIN5_bm | PIN6_bm; // battery level indicator PORTD.OUT = 0x00; PORTC.DIRSET = PIN7_bm; // signal to stop the boost converters PORTC.OUTSET = PIN7_bm; // start with the converters stopped to avoid power // surges on the battery. When the voltage recovers // the converters will be enabled again PORTC.DIRCLR = PIN6_bm; // input V_USB_CHG PORTE.DIRCLR = PIN3_bm; // input CHG_STAT // update once for each second (1Hz) TC_SetPeriod(&TCE0, 31250); TC0_ConfigClockSource(&TCE0, TC_CLKSEL_DIV256_gc); TC0_ConfigWGM(&TCE0, TC_WGMODE_NORMAL_gc); TC0_EnableCCChannels(&TCE0, TC0_CCAEN_bm); TC0_SetCCAIntLevel(&TCE0, TC_CCAINTLVL_LO_gc); }