TIMER2 ISR and Wire.h

Hello

I use TIMER2 (TIMER2_OVF_vect) and Wire.h; when I call a function (which uses Wire.h) in
TIMER2_OVF_vect ISR, then the programme freezes and for the life of me I don't know why.

My configuration:
Arduino IDE 1.8.10, WIN 7 Pro 32Bit

ATMega168, 16MHz
PortExpander MCP23017 for the GLCD, hooked up address 20
PortExpander PCA9555A for button & leds, hooked up address 24
PORTD PD4 - PD7 LEDS

TWI 400kHz
TIMER2 1ms

problem:
power on, the 3 leds (TIMER2 ISR) start flashing, indication on GLCD
after 5 seconds (see "init_prg") the programme stops, because the function
"cont_prg_pe_pca()" is called.
=> "stops" means:

  • indication on GLCD is "Zeit" and nothing else happens (no flashing leds, no
    other indication on GLCD)

Question: how/why does Wire.h have an influence on TIMER2?

if "cont_prg_pe_pca()" is called in the "main" - " for" loop then there's no problem,
the leds and button hooked up to PCA9555A flash/work as desired.

The important part is the TIMER2 ISR and the function"cont_prg_pe_pca()".

Would be great if someone could help me.

Thanks in advance

I need to call that function in TIMER2 ISR because later on there'll be 6 7segment indications hooked up and controlled by that ISR

Hero_123

Klaus_032.ino (9.78 KB)

Don't call functions from an ISR. Just set a flag (a global variable), exit the ISR and let the main program do the work.

Interrupts are turned off when the ISR is called, which can play havoc with just about everything.

Don't forget to declare variables "volatile" if shared with an ISR.

I use TIMER2 (TIMER2_OVF_vect) and Wire.h; when I call a function (which uses Wire.h) in
TIMER2_OVF_vect ISR, then the programme freezes and for the life of me I don't know why.

I do know why: you must not use any functionality of the Wire library inside interrupt context (aka inside an ISR) because in interrupt context all interrupts are disabled and the Wire library depends heavily on interrupts to happen.

I don't see why you use the timer 2 at all as you already have that functionality available for free in the Arduino IDE, just use the millis() function.