Open
Description
The code provided in the rs-blinker.ino
does not work on my Elegoo Mega 2560 R3. It did not switch the LED. After a bit of tweaking I got it working, code below.
Question. Do you see any faults on this code that may not yield the intended results?
#define LED_PIN 2 // Pin number for the onboard LED (Arduino Uno)
const unsigned long long freq = 50; // So I can see it
void setup() {
pinMode(LED_PIN, OUTPUT); // Set the LED pin as output
// Configure Timer1
cli(); // Disable global interrupts
TCCR1A = 0; // Set Timer1 to Normal mode (no PWM)
TCCR1B = 0; // Clear Timer1 control register B
// Set compare match register for 1Hz (1-second interval)
// OCR1A = 15624; // (16 MHz / (1024 * 1Hz)) - 1
OCR1A = (F_CPU) / (1024 * freq) - 1;
// Set prescaler to 1024 and enable CTC mode
TCCR1B |= (1 << WGM12); // Configure for CTC mode
TCCR1B |= (1 << CS12) | (1 << CS10); // Set prescaler to 1024
// Enable Timer1 compare interrupt
TIMSK1 |= (1 << OCIE1A);
sei(); // Enable global interrupts
}
// Interrupt Service Routine for Timer1 Compare Match A
ISR(TIMER1_COMPA_vect) {
digitalWrite(LED_PIN, !digitalRead(LED_PIN)); // Toggle LED
}
void loop() {}
Metadata
Metadata
Assignees
Labels
No labels