When you use the Arduino environment, you enable certain Timer preset values to support the standard PWM outputs controlled with analogWrite(). It is good practice when writing custom Timer1 code to remove all the presets with TCCR1A = 0 and TCCR1B = 0. Your prescalers were wrong for 1024 and were actually at 64.
void setup() {
// put your setup code here, to run once:
TCCR1A = 0;
TCCR1B = 0;
DDRD |= (1 << PD7);//DDR for PD7
TCCR1B |= (1 << WGM12); //Configure Timer1 for CTC
TIMSK1 |= (1 << OCIE1A); // Enable CTC interrupt
sei();
OCR1A = 15624; // set CTC compare value to 1 Hz at 16 MHz clock and prescalar of 1024
TCCR1B |= ((1 << CS10) | (1 << CS12));
}
ISR(TIMER1_COMPA_vect)
{
PORTD ^=(1<<PD7);// Toggle PD7
}
void loop() {
// put your main code here, to run repeatedly:
}
This is a hardware feature of AVR output pins and avoids a read/modify/write cycle. The PIN register is the register you use to read input pins. When you write to it, any bit written with a 1 gets toggled and the other bits are left unchanged.
Yes johnwasser,
I am just begining to get a hang of Bit Math, is the reason of experimenting with C type code on the Arduino platform, I use MCUDUDE's minicore.