Hi. I have ATtiny461a and wanted to generate a square waveform, but using timer1 in CTC mode. Unfortunately, it only seems to work when I set OCR1A=255. The lower value I set OCR1A to, the shorter lower part of the signal gets, but the part where the signal is high seems unaffected. Here's my code
#include <Arduino.h> volatile bool state = false; ISR(TIMER1_COMPA_vect) { state = !state; if(state) PORTB |= (1 << PORTB0); //sets PB0 to high else PORTB &= ~(1 << PORTB0); //sets PB0 to low } //------------------------------ void setup() { pinMode(PIN_B0,OUTPUT); TCCR1A = 0; TCCR1B = 0; TCNT1 = 0; TCCR1A |= (1 << COM1A1); // CTC mode TCCR1B |= (1 << CS10) | (1 << CS11) | (1 << CS12); // 1:64 prescaler OCR1A = 51; TIMSK |= (1 << OCIE1A); // enable timer compare interrupt //----------------------------- } void loop() { }
Here's what it looks like on the oscilloscope when I set OCR1A to 255
And here's what it looks like if I set it to 51 for example:
Am I doing something wrong in the code? I can't find my mistake.
Also, I know Timer1 has builtin square-wave generation - but I want to use CTC mode, just for educational purposes :)