Hi all,
I'm converting a big led project from the Mega to the Teensy 4.1. Everything has gone smooth so far except for a fundamental piece of code that handles the timing. Basically I have a lot of leds to sync to a (long) video so I was using the 32K output of a DS3231 RTC to generate interrupts every 1/25 of a second to run a frame timer. The problem is that the interrupts registers have (of course) different names on the Mega and I'm having trouble identifying what to change them to.
The original code obviously uses the ATmega2560's registers. For the Teensy I only found this page but it doesn't contain enough information. Could you help me porting the following code?
Here's a test sketch with the code I was running on the Mega:
I've added those first two lines (as indicated in the page I linked) and connected the RTC as follows:
Thanks
I'm converting a big led project from the Mega to the Teensy 4.1. Everything has gone smooth so far except for a fundamental piece of code that handles the timing. Basically I have a lot of leds to sync to a (long) video so I was using the 32K output of a DS3231 RTC to generate interrupts every 1/25 of a second to run a frame timer. The problem is that the interrupts registers have (of course) different names on the Mega and I'm having trouble identifying what to change them to.
The original code obviously uses the ATmega2560's registers. For the Teensy I only found this page but it doesn't contain enough information. Could you help me porting the following code?
Here's a test sketch with the code I was running on the Mega:
Code:
#include <FastLED.h>
#include "RTClib.h"
RTC_DS3231 rtc;
/* connections:
32K --> 47
SCL --> 21SCL
SDA --> 20SDA
VCC --> 5V
GND --> GND
*/
volatile uint32_t frameCounter = 0;
uint32_t frameCount;
uint32_t frameCountPrev;
void setup() {
Serial.begin(115200);
Serial.println("startup");
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (!rtc.isEnabled32K()) { //verify 32K output is enabled
rtc.enable32K();
}
TCCR5A = 0; //clear control register A
TCCR5B = 0; //clear control register B
TCNT5 = 0; //clear counter
OCR5A = 1309; //set value for output compare register A (32768Hz * 1/25 second) - 1 = 1309
TCCR5B |= (1 << WGM52); //Set CTC mode (WGM5 = 0100);
TCCR5B |= (1 << CS52) | (1 << CS51) | (1 << CS50) ; //External Clock mode using D47 as input
TIMSK5 |= (1 << OCIE5A); //Set the interrupt request
interrupts(); //Enable interrupt
}
void loop() {
//temporarily disable interrupts while making a copy of frameCounter
noInterrupts();
frameCount = frameCounter;
interrupts();
if (frameCount != frameCountPrev) { //only execute once per frame
if ((frameCount - frameCountPrev) != 1) {
Serial.println("missed frame");
while (1) {};
}
frameCountPrev = frameCount;
//do stuff, update leds
Serial.print(frameCount);
//print millis every 25 frames to test discrepancy
if ((frameCount % 25) == 0) {
Serial.print('\t');
Serial.print(millis());
}
Serial.println();
}
}
ISR(TIMER5_COMPA_vect) { //This is the interrupt request
static byte cycleCount = 24;
//adjustment to compensate for 32768 not being evenly divisible by 25
//counter will count ( (7 * 1310) + (18 * 1311) ) = 32768 pulses over 25 frames
if ((cycleCount & 0x03) == 0) {
OCR5A = 1309; //1310 clock pulses
} else {
OCR5A = 1310; //1311 clock pulses
}
if (cycleCount == 0) {
cycleCount = 25;
}
cycleCount--;
frameCounter++; //actual frame counter
}
I've added those first two lines (as indicated in the page I linked) and connected the RTC as follows:
Code:
#include <avr/io.h>
#include <avr/interrupt.h>
#include "RTClib.h"
RTC_DS3231 rtc;
/* connections:
32K --> 18 //can be anything else if needed
SCL --> 19SCL
SDA --> 18SDA
VCC --> 3.3V
GND --> GND
*/
Thanks