volatile int toggleCount = 0; // Counter to track the number of toggles
void setup() {
// Initialize Serial communication at 9600 baud rate
Serial.begin(9600);
// Setup PORTF0 as output
volatile char *dirf = (volatile char *) 0x30; // DDRF register for direction
volatile char *outf = (volatile char *) 0x31; // PORTF register for output
*dirf |= 0x01; // Set F0 as output (bit 0 of PORTF)
// Configure Timer1
TCCR1A = 0; // Normal operation mode
TCCR1B = (1 << WGM12); // CTC mode (Clear Timer on Compare Match)
TCCR1B |= (1 << CS12);
// Set prescaler to 256 (16 MHz / 256)
OCR1A = 65000- 1; // Set compare match value for 1 Hz toggle (with 256 prescaler)
TIMSK1 = (1 << OCIE1A); // Enable Timer1 compare interrupt
sei(); // Enable global interrupts
}
ISR(TIMER1_COMPA_vect) {
volatile char *outf = (volatile char *) 0x31; // PORTF register
*outf ^= 0x01; // Toggle F0 (connected LED)
toggleCount++; // Increment toggle count
Serial.print("Toggle Count: ");
Serial.println(toggleCount); // Print count to serial
// Stop the timer after 10 toggles
if (toggleCount >= 10) {
TCCR1B &= ~(1 << CS12); // Stop Timer1 (clear prescaler bits)
Serial.println("Timer stopped after 10 toggles.");
}
}
void loop() {
// Nothing needed here; everything is handled in the interrupt
}