16x32 RGB led matrix with interrupt routine

For other supported Arduino products from Adafruit: Shields, accessories, etc.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
Todts
 
Posts: 4
Joined: Sat Mar 22, 2014 5:16 am

16x32 RGB led matrix with interrupt routine

Post by Todts »

Hi,

Im testing if i can use my 16x32 RGB ledMatrix from adafruit with an interrupt routine.
In void loop() I'm setting a pixel on position 0,0 to red and in my interrupt routine I am blinking a led every second. I'm using Timer1 (16bit) bc my compare match register for 1Hz is higher then 256.

My promblem is that the interrupt routine and the loop routine work individually but when i put them together then only the red pixel is been set.

Does somebody know why they can't go together?

grtz

Code: Select all

#include <avr/io.h>
#include <avr/interrupt.h>
#include <Adafruit_GFX.h>   // Core graphics library
#include <RGBmatrixPanel.h> // Hardware-specific library

int led =13;

#define CLK 8  // MUST be on PORTB! (Use pin 11 on Mega)
#define LAT A3
#define OE  9
#define A   A0
#define B   A1
#define C   A2
RGBmatrixPanel matrix(A, B, C, CLK, LAT, OE, false);

void setup()
{
  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

  // set compare match register for 1hz increments
  OCR1A = 15624;// = (16*10^6) / (1*1024) - 1 (must be <65536)
  // turn on CTC mode
  TCCR1B |= (1 << WGM12);

  // Set CS12 and CS10 bits for 1024 prescaler
  TCCR1B |= (1 << CS12) | (1 << CS10);  

  // enable timer compare interrupt
  TIMSK1 |= (1 << OCIE1A);
  
  sei();//allow interrupts
  
  pinMode(led,OUTPUT);
  matrix.begin();
}

  ISR(TIMER1_COMPA_vect)//interrupt routine
  {
    digitalWrite(led,!digitalRead(led));
  }

void loop()
{
  matrix.drawPixel(0, 0, matrix.Color333(7, 0, 0));
}

User avatar
pburgess
 
Posts: 4161
Joined: Sun Oct 26, 2008 2:29 am

Re: 16x32 RGB led matrix with interrupt routine

Post by pburgess »

The RGBmatrixPanel library uses Timer1 for refreshing the display. You'll probably want to set up a separate interrupt on Timer2.

Todts
 
Posts: 4
Joined: Sat Mar 22, 2014 5:16 am

Re: 16x32 RGB led matrix with interrupt routine

Post by Todts »

the number in the compare match register for Timer1 must be smaller then 65536 and for Timer0 and Timer2 it must be smaller then 256.(datasheet)

Calculating the number in the compare match register goes as fallow:

compare match register = [ 16,000,000Hz/ (prescaler * desired interrupt frequency) ] - 1

For 1sec blinking my desired interrupt frequency is 1Hz

16,000,000Hz/(1024*1)-1 = 15624

256 < 15624 < 65536

this tels me that i need to use Timer1...

luckily for me project i don't need to use 1Hz. This is just a sketch to learn with interrupt.

But do you have an idea how to evade it or is it just not possible while using 1hz?

User avatar
pburgess
 
Posts: 4161
Joined: Sun Oct 26, 2008 2:29 am

Re: 16x32 RGB led matrix with interrupt routine

Post by pburgess »

In that case, you'd want to set up your interrupt for a known interval (e.g. 100 Hz) and have a counter in your code that rolls over (and does some action) every 100th invocation.

Kinda stinks, but yeah, there are only so many timers to go around. The core Arduino library uses Timer0 for the delay() and millis() functions, etc., RGBmatrixPanel uses Timer1, so Timer2 is the last option.

Locked
Please be positive and constructive with your questions and comments.

Return to “Other Arduino products from Adafruit”