示例#1
0
/*
This is a slightly modified version of the timer setup found at:
https://github.com/maxbader/arduino_tools
 */
void startTimer(int frequencyHz) {
  REG_GCLK_CLKCTRL = (uint16_t) (GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK0 | GCLK_CLKCTRL_ID (GCM_TCC2_TC3)) ;
  while ( GCLK->STATUS.bit.SYNCBUSY == 1 );

  TcCount16* TC = (TcCount16*) TC3;

  TC->CTRLA.reg &= ~TC_CTRLA_ENABLE;

  // Use the 16-bit timer
  TC->CTRLA.reg |= TC_CTRLA_MODE_COUNT16;
  while (TC->STATUS.bit.SYNCBUSY == 1);

  // Use match mode so that the timer counter resets when the count matches the compare register
  TC->CTRLA.reg |= TC_CTRLA_WAVEGEN_MFRQ;
  while (TC->STATUS.bit.SYNCBUSY == 1);

  // Set prescaler to 1024
  TC->CTRLA.reg |= TC_CTRLA_PRESCALER_DIV1024;
  while (TC->STATUS.bit.SYNCBUSY == 1);

  setTimerFrequency(frequencyHz);

  // Enable the compare interrupt
  TC->INTENSET.reg = 0;
  TC->INTENSET.bit.MC0 = 1;

  NVIC_EnableIRQ(TC3_IRQn);

  TC->CTRLA.reg |= TC_CTRLA_ENABLE;
  while (TC->STATUS.bit.SYNCBUSY == 1);
}
示例#2
0
void loop() {
  int newPotValue = analogRead(POT_PIN);
  // We only want to update the frequency if there has been significant changes
  // to the potentiometer. We do this to prevent jitters but also to make this
  // loop more efficient.
  if (abs(newPotValue - potValue) > POT_JITTER_THRESHOLD) {
    potValue = newPotValue;
    setTimerFrequency(frequencyFromPotValue(potValue));
  }
}
示例#3
0
void setTimerPairFrequency(Timer timerA, Timer timerB, uint16_t herz) {
	// The timer A value dictates the frequency for both timer A and B.
    // Set a higher frequency for timer B so that interrupts are not fired the same time.
	setTimerFrequency(timerA, TIMER_PRESCALER, herz);
	setTimerFrequency(timerB, TIMER_PRESCALER, herz);
}