SPI Display works only without timer initialization... why?

Hello

I want to use a display and the timer1 interrupt together but it does not work.
Without any timer initialization the Oled display works fine an displays a "T" for test. With the timer initialization the display displays nothing. When the TIMSK1 is activated in code the display does not work anymore. When i comment the TIMSK1 out it works and shows a "T" for test on the display but the timer won't work without enabling TIMSK1.

Why does the display not work together with the initialization of the timer?

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels

// Declaration for SSD1306 display connected using software SPI (default case):
#define OLED_MOSI   9
#define OLED_CLK   10
#define OLED_DC    11
#define OLED_CS    12
#define OLED_RESET 13
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT,
  OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
  
void setup() 
{   
    //Timer interrupts
    cli();//stop interrupts
    //set timer1 interrupt at 1Hz
    TCCR1A = 0;// set entire TCCR1A register to 0
    TCCR1B = 0;// same for TCCR1B
    TCNT1  = 0;//initialize counter value to 0 --> counter Register
    // set compare match register for 50hz increments
    OCR1A = 40000;// bis hierhin zählt deer counter, dannach macht er einen Reset 16MHZ / prescaler / gewählte HZ
    // turn on CTC mode
    TCCR1B |= (1 << WGM12);
    // Set CS10 and CS12 bits for 8 prescaler
    TCCR1B |= (0 << CS12) | (1 << CS11) | (0 << CS10);  
    // enable timer compare interrupt
    TIMSK1 |= (1 << OCIE1A); // -->When i comment out this row the display works and with this not.<--
    sei();//allow interrupts
    
    Serial.begin(9600);
    // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
    if(!display.begin(SSD1306_SWITCHCAPVCC)) 
    {
        Serial.println(F("SSD1306 allocation failed"));
        for(;;); // Don't proceed, loop forever
    } 
    
    Test();//Gives out a "T" on display!   
}

void loop() 
{  
  
}

void Test(void)
{
        display.clearDisplay();
        display.setTextSize(1); // Draw 2X-scale text
        display.setTextColor(SSD1306_WHITE);
        display.setCursor(0, 0);
        display.println("T");
        display.display();
}

I controlled the data connection of the hardware. The arduino sends data to the display when TIMSK1 is comment out. And the arduino doesn't send data to the display when Timsk1 is active.

Someone an idea?

Done... the timercounter was overflow and resetet the arduino all the time

You need to have the ISR if you are enabling it in the TIMSK1 register. In your case, you are enabling

TIMSK1 |= (1 << OCIE1A)

So you need to have this in your code

ISR(TIMER1_COMPA_vect)
{
// do nothing or something
}

This topic was automatically closed after 120 days. New replies are no longer allowed.