Created
December 18, 2019 22:05
-
-
Save dwblair/23cfb1af2ce21c2466726fb1021bcb7a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Arduino.h> | |
#include "Adafruit_ZeroTimer.h" | |
// This example can have just about any frequency for the callback | |
// automatically calculated! | |
float freq = 10.0; // 1 KHz | |
// timer tester | |
Adafruit_ZeroTimer zerotimer = Adafruit_ZeroTimer(3); | |
void TC3_Handler() { | |
Adafruit_ZeroTimer::timerHandler(3); | |
} | |
// the timer callback | |
volatile bool togglepin = false; | |
volatile bool togglepin2 = false; | |
void TimerCallback0(void) | |
{ | |
digitalWrite(LED_BUILTIN, togglepin); | |
togglepin = !togglepin; | |
} | |
void TimerCallback1(void) | |
{ | |
digitalWrite(12, togglepin2); | |
togglepin2 = !togglepin2; | |
} | |
void setup() { | |
pinMode(LED_BUILTIN, OUTPUT); | |
pinMode(12, OUTPUT); | |
Serial.begin(9600); | |
while (!Serial) { | |
delay(10); | |
} | |
Serial.println("Timer callback tester"); | |
Serial.print("Desired freq (Hz):"); | |
Serial.println(freq); | |
// Set up the flexible divider/compare | |
uint8_t divider = 1; | |
uint16_t compare = 0; | |
tc_clock_prescaler prescaler = TC_CLOCK_PRESCALER_DIV1; | |
if ((freq < 24000000) && (freq > 800)) { | |
divider = 1; | |
prescaler = TC_CLOCK_PRESCALER_DIV1; | |
compare = 48000000/freq; | |
} else if (freq > 400) { | |
divider = 2; | |
prescaler = TC_CLOCK_PRESCALER_DIV2; | |
compare = (48000000/2)/freq; | |
} else if (freq > 200) { | |
divider = 4; | |
prescaler = TC_CLOCK_PRESCALER_DIV4; | |
compare = (48000000/4)/freq; | |
} else if (freq > 100) { | |
divider = 8; | |
prescaler = TC_CLOCK_PRESCALER_DIV8; | |
compare = (48000000/8)/freq; | |
} else if (freq > 50) { | |
divider = 16; | |
prescaler = TC_CLOCK_PRESCALER_DIV16; | |
compare = (48000000/16)/freq; | |
} else if (freq > 12) { | |
divider = 64; | |
prescaler = TC_CLOCK_PRESCALER_DIV64; | |
compare = (48000000/64)/freq; | |
} else if (freq > 3) { | |
divider = 256; | |
prescaler = TC_CLOCK_PRESCALER_DIV256; | |
compare = (48000000/256)/freq; | |
} else if (freq >= 0.75) { | |
divider = 1024; | |
prescaler = TC_CLOCK_PRESCALER_DIV1024; | |
compare = (48000000/1024)/freq; | |
} else { | |
Serial.println("Invalid frequency"); | |
while (1) delay(10); | |
} | |
Serial.print("Divider:"); Serial.println(divider); | |
Serial.print("Compare:"); Serial.println(compare); | |
Serial.print("Final freq:"); Serial.println((int)(48000000/compare)); | |
zerotimer.enable(false); | |
zerotimer.configure(prescaler, // prescaler | |
TC_COUNTER_SIZE_16BIT, // bit width of timer/counter | |
TC_WAVE_GENERATION_MATCH_PWM // frequency or PWM mode | |
); | |
zerotimer.setCompare(0, compare); | |
zerotimer.setCompare(1, compare/2); | |
zerotimer.setCallback(true, TC_CALLBACK_CC_CHANNEL0, TimerCallback0); | |
zerotimer.setCallback(true, TC_CALLBACK_CC_CHANNEL1, TimerCallback1); | |
zerotimer.enable(true); | |
} | |
void loop() { | |
Serial.println("tick"); | |
delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment