/*
Uno with Scope https://github.com/Dlloydev/Wokwi-Chip-Scope
and https://github.com/Dlloydev/Wokwi-Chip-PWM
and https://github.com/drf5n/Wokwi-Chip-FrequencyCounter
Wokwi Uno https://wokwi.com/projects/390819301187622913
Wokwi Nano https://wokwi.com/projects/426440872904255489
Wokwi Mega: https://wokwi.com/projects/390819455604080641
Wokwi ESP32: https://wokwi.com/projects/408508221519641601
Wokwi ESP32S3 https://wokwi.com/projects/404720144387009537
See also https://wokwi.com/projects/359331973918199809
This sketch feeds a pulse train into T1, divides it by (OCR1A+1)
and sums those up into nanos, subtracting a billion each second.
*/
#include "TimerHelpers.h"
/* ---------------------------------------------------------------
Test sketch
--------------------------------------------------------------- */
const byte CLOCK = 5; // this is T1 (timer 1 clock input)
const uint32_t NanosPerTick = 7142857;
const uint32_t NanosPerSec = 1000000000;
uint32_t nanos = 0;
uint32_t secs = 0;
void setup() {
pinMode(4, OUTPUT);
pinMode(9, OUTPUT);
pinMode (CLOCK, INPUT);
Serial.begin(115200);
TCCR1A = 0; // reset timer 1
TCCR1B = 0;
// set up Timer 1
TCNT1 = 0; // reset counter
OCR1A = 1; // compare A register value (1000 * clock speed)
// Mode 4: CTC, top = OCR1A
Timer1::setMode (4, Timer1::T1_FALLING, Timer1::TOGGLE_A_ON_COMPARE);
TIFR1 |= bit (OCF1A); // clear interrupt flag
TIMSK1 = bit (OCIE1A); // interrupt on Compare A Match
} // end of setup
ISR(TIMER1_COMPA_vect)
{
nanos += NanosPerTick;
if (nanos > NanosPerSec) {
nanos -= NanosPerSec;
secs++;
PIND = bit(4); // Toggle pin4
}
} // end of TIMER1_COMPA_vect
void loop() {
static int32_t lastSec = -1;
const uint32_t interval = 10;
static uint32_t last = -interval;
uint32_t now = millis();
if (now - last >= interval) {
last += interval;
noInterrupts();
uint32_t mysecs = secs;
interrupts();
while (lastSec != mysecs ) {
lastSec++;
Serial.println(lastSec);
}
}
} // end of loop
Wokwi UnoScope
https://wokwi.com/projects/390819301187622913
cnt/meas
en/!dis