Created
February 5, 2020 16:46
-
-
Save dwblair/f4c47260feb9f7026e18b2b99bf819a7 to your computer and use it in GitHub Desktop.
basic_conductivity_measure / samdX1
This file contains 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 = 20000.0; // the actual frequency seems to be half of this | |
// timer tester | |
Adafruit_ZeroTimer zerotimer = Adafruit_ZeroTimer(3); | |
void TC3_Handler() { | |
Adafruit_ZeroTimer::timerHandler(3); | |
} | |
// the timer callback | |
volatile bool togglepin = false; | |
void TimerCallback0(void) | |
{ | |
digitalWrite(10, togglepin); | |
togglepin = !togglepin; | |
} | |
void setup() { | |
pinMode(10, 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.setCallback(true, TC_CALLBACK_CC_CHANNEL0, TimerCallback0); | |
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