/* 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; }
// Configures PWM output on compare a b and c for single slope pwm, with hires, and clk source as sys clk void configPWM (volatile TC0_t * tc, HIRES_t * hires, uint16_t period) { TC_SetPeriod (tc, period ); TC0_ConfigWGM (tc, TC_WGMODE_NORMAL_gc ); // set to single slope pwm generation mode TC0_EnableCCChannels (tc, TC0_CCAEN_bm); // enable compare A TC0_EnableCCChannels (tc, TC0_CCBEN_bm); // enable compare B TC0_EnableCCChannels (tc, TC0_CCCEN_bm); // enable compare C TC0_EnableCCChannels (tc, TC0_CCDEN_bm); // enable compare D //~ TC0_SetCCAIntLevel (tc, TC_CCAINTLVL_HI_gc); TC0_SetCCBIntLevel (tc, TC_CCBINTLVL_LO_gc); //~ TC0_SetCCCIntLevel (tc, TC_CCCINTLVL_HI_gc); //~ TC0_SetCCDIntLevel (tc, TC_CCDINTLVL_HI_gc); TC0_SetOverflowIntLevel (tc, TC_OVFINTLVL_LO_gc); PMIC.CTRL |= PMIC_HILVLEN_bm; TC0_ConfigClockSource (tc, TC_CLKSEL_DIV1_gc); HIRES_Enable (hires, HIRES_HREN_TC0_gc); }
// This function initializes the PWMs void Init_PWM( void ) { // Set PWM outputs MOTOR_OUTPUT.DIRSET = 0x03; // PD0/PD1 set as outputs // Configure timer TC0_ConfigWGM(&MOTOR_TIMER, TC_WGMODE_SS_gc); TC0_EnableCCChannels(&MOTOR_TIMER, TC0_CCAEN_bm|TC0_CCBEN_bm); TC_SetCompareA(&MOTOR_TIMER, 0); TC_SetCompareB(&MOTOR_TIMER, 0); TC_SetPeriod(&MOTOR_TIMER, MAX_PWM); TC0_ConfigClockSource(&MOTOR_TIMER, TC_CLKSEL_DIV1_gc); // 32MHz }
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); }
void TCE0_init() { /* Select a Waveform Genreation mode and the CC channels to use*/ TC0_ConfigWGM( &TCE0, TC_WGMODE_SS_gc ); TC0_EnableCCChannels( &TCE0, TC0_CCAEN_bm); // only CCA is used TC_SetPeriod( &TCE0, 60000); /* The corresponding port pins MUST be output for the Waveform to be visible */ PORTE.DIRSET = 0xFF; PORTE.OUTSET = 0xFF; //set port high to switch LEDs off, take INVEN into account /* To have inverted ouput on some pins we set the corresponding INVEN bit for the pin*/ TC0_ConfigClockSource(&TCE0, TC_CLKSEL_DIV1_gc); }