/** \brief Disables OVF interrupts on the given timer. * * As this library is interrupt driven, this effectively stops the motor. When * stopped, the stepper motor will go into its holding torque phase, unless the * motor driver is disabled. * * \param[in] timer the timer to stop. */ static void SM_timer_stop(SM_timer_t timer) { if (timer == SM_TIMER_NEEDLE) { TC0_SetOverflowIntLevel(&TIMER_NEEDLE, TC_OVFINTLVL_OFF_gc); } else { TC1_SetOverflowIntLevel(&TIMER_RING, TC_OVFINTLVL_OFF_gc); } }
/** \brief Enables OVF interrupts on the given timer. * * As this library is interrupt driven, enabling these interrupts essentially * enables the given SM_move() command to be carried out by the motor. * * \param[in] timer the timer to start */ static void SM_timer_start(SM_timer_t timer) { if (timer == SM_TIMER_NEEDLE) { TC0_SetOverflowIntLevel(&TIMER_NEEDLE, TC_OVFINTLVL_HI_gc); } else { TC1_SetOverflowIntLevel(&TIMER_RING, TC_OVFINTLVL_HI_gc); } /* enable hi level interrupts */ PMIC.CTRL |= PMIC_HILVLEN_bm; }
/* * Setup timer 1 compare match A to generate a tick interrupt. */ static void prvSetupTimerInterrupt(void) { //Use TCC0 as a tick counter. If this is to be changed, change ISR as well TC0_t * tickTimer = &TCC0; //select the clock source and pre-scale by 64 TC0_ConfigClockSource(tickTimer, TC_CLKSEL_DIV64_gc); //set period of counter tickTimer->PER = configCPU_CLOCK_HZ / configTICK_RATE_HZ / 64 - 1; //enable interrupt and set low level TC0_SetOverflowIntLevel(tickTimer, TC_OVFINTLVL_LO_gc); //enable low level interrupts PMIC_EnableLowLevel(); }
// 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 system tick void Init_SysTick( void ) { TC_SetPeriod(&TCF0, 124); // 125 ticks at 31.25KHz = 4ms periods TC0_ConfigClockSource(&TCF0, TC_CLKSEL_DIV1024_gc); // 32MHz/1024=31.25KHz TC0_SetOverflowIntLevel(&TCF0, TC_OVFINTLVL_LO_gc); // Enable interrupts for overflow }