Hello,
I can't success to trigger the Timer1Overflow interrupt when reaching 511 value. ( mode 0010 WGM1x).
Is my configuration of Timer1 wrong ?
void setup()
{
Serial.begin(9600);
noInterrupts();
// Initialise timer/counter to known value
TCCR1A = 0;
TCCR1A = _BV(COM1A1) | _BV(COM1A0); // Enable D9 OUTPUT
TCCR1A = _BV(WGM11) /*| _BV(WGM10)*/; // Enable pwm to 511
TCCR1B = 0;
TCCR1B = //|
//_BV(WGM13)
//|
//_BV(WGM12)
//;
// Set prescaler (divide by) on the clock ticks for 16MHz clock
/* For Timer 1 the options for are:
* 111 divide by 1024 = 15.625kHz = 64uS
* 110 divide by 256 = 62.5kHz = 16uS
* 101 divide by 126 = 125kHz = 8
* 100 divide by 64 = 250kHZ = 4
* 011 divide by 32 = 500kHz = 2
* 010 divide by 8 = 2MHz = 0.5
* 001 divide by 1 = 16MHz = 0.0625 uS
* 000 timer disabled
*
* the CSnn are the Clock Source prescaler TTCRnB bits we are setting
*/
// TCCR1B = 0;
// TCCR1B = ((1 << CS22) | (1 << CS21) | (1 << CS20));
// or
// TCCR1B |= 0b00000111;
// or
TCCR1B = _BV(CS12) |
_BV(CS11)
//|
//_BV(CS10)
; // Prescaler 62.5khz
// Enable interrupts on overflow by adjusting pins 2:0
// Set the Timer 1 Interrupt Mask Register so that
// we enable to overflow interrupt TOIE1 (Timer 1 Overflow Interrupt Enable)
// AND when the values match two values (comparators OCIE1A & OCIE1B) we
// trigger two further, independent interrupts.
TIMSK1 = ((1 << TOIE1) /*| (1 << OCIE1A) */ /*| (1 << OCIE1B)*/);
OCR1A = 255; // Dutycycle of 50%
pinMode(9, OUTPUT);
interrupts();
}
void loop()
{
// put your main code here, to run repeatedly:
}
ISR(TIMER1_ovf_vect)
{
Serial.println("interrupt");
}
Thanks
The second line overrides the first and the third overrides the second, thus disabling inverted PWM on OC1A.
You can either use:
// Initialise timer/counter to known value
TCCR1A = _BV(COM1A1) | _BV(COM1A0);
TCCR1A |= _BV(WGM11);
or
// Initialise timer/counter to known value
TCCR1A = _BV(COM1A1) | _BV(COM1A0) | _BV(WGM11);
Tried your two solutions but neither works.
The ISR(TIMER1_OVF_vect) is not triggered.
It's ok! my bad,many thanks
I haven't used interrupts yet, but I have read here that printing in an interrupt routine is not a good idea.
It is not a good idea, yes. But for testing, it's a time saver. When the solution will be found, I'll remove all those Serial.print
Where did you get that table from? It is not correct.
This is the table from the datasheet:
Table 16-5. Clock Select Bit Description
CS12
CS11
CS10
Description
0
0
0
No clock source (Timer/Counter stopped).
0
0
1
clkI/O/1 (No prescaling)
0
1
0
clkI/O/8 (From prescaler)
0
1
1
clkI/O/64 (From prescaler)
1
0
0
clkI/O/256 (From prescaler)
1
0
1
clkI/O/1024 (From prescaler)
1
1
0
External clock source on T1 pin. Clock on falling edge.
1
1
1
External clock source on T1 pin. Clock on rising edge.
This means your
selects an external clock source. Your timer will never count up.
Didn't you get a warning message?
In file included from /Users/john/Library/Arduino15/packages/arduino/hardware/avr/1.8.4/cores/arduino/Arduino.h:30:0,
from sketch/sketch_jan25a.ino.cpp:1:
/Users/john/Documents/Arduino/sketch_jan25a/sketch_jan25a.ino: In function 'void TIMER1_ovf_vect()':
/Users/john/Documents/Arduino/sketch_jan25a/sketch_jan25a.ino:65:5: warning: 'TIMER1_ovf_vect' appears to be a misspelled 'signal' handler, missing '__vector' prefix [-Wmisspelled-isr]
ISR(TIMER1_ovf_vect)
^
In function 'TIMER1_ovf_vect':
/Users/john/Documents/Arduino/sketch_jan25a/sketch_jan25a.ino:65:1: warning: 'TIMER1_ovf_vect' appears to be a misspelled 'signal' handler, missing '__vector' prefix [-Wmisspelled-isr]
ISR(TIMER1_ovf_vect)
^
They are using the Timer2 table for Timer1.
Maybe that it will not work ?
Yeah I used the wrong timer table... shame on me
DaveX
January 31, 2022, 4:34pm
11
Did you want it toggling a pin?
You can tell if your configuration of a timer is wrong by printing the control registers and confirming they are configured as intended:
void reportTimer1(void) {
static unsigned long lastReport = 0;
if (millis() - lastReport < 1000) return;
lastReport = millis();
Serial.print("TCCR1A 0xb");
Serial.print(TCCR1A, BIN);
Serial.print(" TCCR1B 0xb");
Serial.print(TCCR1B, BIN);
Serial.print("TCNT1 :");
Serial.print(TCNT1);
Serial.print("OCRA1 :");
Serial.print(OCRA1);
Serial.print("TMSK1 :");
Serial.print(TMSK1);
Serial.println();
}
I'll add that function and put a reportTimer1(); in the setup() or loop().
You should switch your selected solution to #6 .
And where did you get that wrong table comment? It will intermittently mess people up in several different ways, depending on the prescaler they choose.