4

Apologies if this is the wrong sub-forum, the involvement of ATTiny and arduino places this into a strange category. Please correct me if I should post elsewhere.

It is important for this to be a library as small kids will be using this program and hardware for their own projects. Its kind of a rip on the LOLshield but it involves a lot more steps to integrate more lessons all at once.

My whole program takes 8 bytes of information and converts it to data for a shift register driving two multiplexers all driving an 8x8 LED matrix. It can also use multiple of these packets to make a slide show. I have it in a working state right now and would like to turn it into a library for easy use. I am a bit lost in this step as I had to create an interrupt timer and this makes it more complicated than the tutorial on the arduino website covers.

Also if someone could point me to a library for ATTiny85 that makes calling a timer interrupt easy then the rest of this project will likewise become easier.

Any help would be great! I will be continuing to look on online resources and will update should I figure it out.

4
  • I would just implement a callback and tell the consumer "call this callback every so often, preferably with a timer interrupt". Commented Dec 24, 2015 at 1:17
  • Ok could you direct me to a timer interrupt library compatible with the ATTiny85 then? Programming an interrupt for the attiny85 is stupidly messy to a new programmer.
    – lukeb28
    Commented Dec 24, 2015 at 1:53
  • Nope. But it should be easy enough to port something like Timer1 to arduino-tiny. Commented Dec 24, 2015 at 1:58
  • Already looked into it, it's not. I'm not exactly a great coder in the first place, this is a learning experience for me.
    – lukeb28
    Commented Dec 24, 2015 at 2:16

1 Answer 1

3

I had to create an interrupt timer ...

Be warned that there are things you should not do in interrupts, like delays, serial printing, etc.


Code

In the interests of answering the question though, this works:

// ATMEL ATTINY 25/45/85 / ARDUINO
// Pin 1 is /RESET
//
//                  +-\/-+
// Ain0 (D 5) PB5  1|    |8  Vcc
// Ain3 (D 3) PB3  2|    |7  PB2 (D 2) Ain1 
// Ain2 (D 4) PB4  3|    |6  PB1 (D 1) pwm1
//            GND  4|    |5  PB0 (D 0) pwm0
//                  +----+

ISR (TIMER1_COMPA_vect)
  {
  digitalWrite (4, ! digitalRead (4));  //toggle D4 (pin 3 on chip)
  }

void setup() 
 {
  pinMode (4, OUTPUT);  // chip pin 3 

  // Timer 1
  TCCR1 = bit (CTC1);   // Clear Timer/Counter on Compare Match
  TCCR1 |= bit (CS10) | bit (CS13);  // prescaler of 256
  OCR1C = 123;          // what to count to (zero-relative)
  TIMSK = bit (OCIE1A); // interrupt on compare

  }  // end of setup

void loop() 
  { 
  // other code here
  }

You can tweak the time delay by changing both the prescaler and the counter in OCR1C. The prescaler gives you coarse tuning, which gets you into the ballpark of the delay time. The timer then counts up to give you the final delay.


Prescaler values

From the datasheet:

ATtiny85 timer 1 prescalers

In my case I chose a prescaler of 256 which is or'ing together CS10 and CS13.

The delay is then:

125 ns * 256 * 124 = 3.968 ms

Where 256 is the prescaler and 124 is what we are counting to. This is assuming an 8 MHz clock which gives a clock period of 125 ns.


Flashing more slowly

If you put an LED on pin 3 and want to see the flashing, you need it much slower. For example:

TCCR1 |= bit (CS10) | bit (CS11) | bit (CS12) | bit (CS13);  // prescaler: 16384

That toggles the pin every 254 ms, which is visible.


Warning

On this chip Timer 1 is an 8-bit timer, so you cannot put more than 255 into OCR1C.


More information

1
  • Wow that answer was above and beyond what I expected! All this info is super useful! I'll be putting it to good use, hell this post should be put somewhere where more people will see it too!
    – lukeb28
    Commented Feb 1, 2016 at 16:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.