Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

OLED on Interrupt
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

OLED on Interrupt

by AnderEpos on Tue Oct 13, 2020 3:28 am

Good morning everyone,

I am trying to get a simple stopwatch application running. But I am running into something I don't understand.

I have an OLED 128x32 hooked up to an Arduino. Running the Adafruit OLED library. This is my code:

Code: Select all | TOGGLE FULL SIZE
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_SSD1306.h>

// Settings OLED
#define oled_width 128
#define oled_height 32
#define oled_reset -1
Adafruit_SSD1306 display(oled_width, oled_height, &Wire, oled_reset);

// Settings Interrupt counter
int time_counter = 0;
int minuten = 0;
int sekondes = 0;

//=======================================================================================//

void setup (){
 
  // Setup UART
  Serial.begin(9600);
  while (!Serial);   

  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }

  // Clear the oled buffer
  display.clearDisplay();

  display.setTextSize(2);           
  display.setTextColor(SSD1306_WHITE);
  display.println(F("Welcome"));
  display.display();
  delay(2000);
 
  display.clearDisplay();

  cli();
  TCCR1A = 0;
  TCCR1B = 0;
  TCNT1  = 0;
  OCR1A = 15624;
  TCCR1B |= (1 << WGM12);
  TCCR1B |= (1<<CS12) | (1<<CS10);
  TIMSK1 |= (1 << OCIE1A);
  sei();
}

//===========================================================================//

void loop(){
while (true){
  display.clearDisplay();
     
     display.setTextSize(1);             // Normal 1:1 pixel scale
     display.setTextColor(SSD1306_WHITE);        // Draw white text
     display.setCursor(0,0);             // Start at top-left corner
     display.println(F("Loop"));
     delay(500);
    display.display();
    Serial.println("Loop");
}
}// end loop

ISR (TIMER1_COMPA_vect){
     time_counter += 1;
     sekondes = time_counter%60;
     minuten = time_counter/60;
     
     display.clearDisplay();
     
     display.setTextSize(1);             // Normal 1:1 pixel scale
     display.setTextColor(SSD1306_WHITE);        // Draw white text
     display.setCursor(0,0);             // Start at top-left corner
     display.println("Interrupt");
     display.print(minuten);
     display.print(" : ");
     display.println(sekondes);
     //delay(100);
     Serial.print("Interrupt");
     display.display();
     Serial.println("Debug");
 }


What I find, is that the
Code: Select all | TOGGLE FULL SIZE
display.display()
line in the interrupt crashes or at least stops my program. I thought that the display needed a bit time so I added the
Code: Select all | TOGGLE FULL SIZE
delay(100)
, that didn't help.

What I did notice is that when there is no display.display() in the interrupt, and only in loop() then everything seems to be working. Although it doesn't feel right to call the display.display() in the loop() when it should happen in the interrupt.

Any idea what I am missing, or don't understand and how I can fix this?

Thanks

AnderEpos
 
Posts: 4
Joined: Tue Oct 13, 2020 3:19 am

Re: OLED on Interrupt

by mikeysklar on Wed Oct 14, 2020 7:00 pm

@AnderEpos,

I'm not sure if you can call a function within an interrupt without complications.

Why not set a flag within the interrupt?

Code: Select all | TOGGLE FULL SIZE
DisplayInterrupt = HIGH;


Then have a conditional statement in the loop() area that is always checking the flag.

Code: Select all | TOGGLE FULL SIZE
 if ( DisplayInterrupt == HIGH ) {
  display.display();
}

mikeysklar
 
Posts: 1958
Joined: Mon Aug 01, 2016 8:10 pm

Re: OLED on Interrupt

by XRAD on Sun Oct 18, 2020 8:00 pm

even Serial.print can jam an ISR.....interrupting an interrupt = doublerrupting... :P

XRAD
 
Posts: 591
Joined: Sat Nov 19, 2016 3:28 pm

Re: OLED on Interrupt

by AnderEpos on Mon Oct 19, 2020 9:05 am

Thanks @mikeysklar,

I will implement your suggestion. It does seem like the most logical. I just thought I might be missing something.

AnderEpos
 
Posts: 4
Joined: Tue Oct 13, 2020 3:19 am

Please be positive and constructive with your questions and comments.