The ATtiny24, 44, 84 series only has 1 external interrupt (INT0) [Page 48 of the datasheet]. This is the interrupt that we are most familiar with using on the Arduino. You can simply use this interrupt by saying “attachInterrupt()”
While it only had 1 interrupt, I wanted to have two buttons on my clock: one button to increase the time and another to decrease the time. Looking back on the datasheet, the AT tiny has two Pin Change Interrupt ports: PCINT0 and PCINT1 [Page 48].
The difference between External Interrupts and Pin Change Interrupts is that an External Interrupt has its own ISR (interrupt service routine). PCINT erupts share one ISR for all pins on one port. This means that with Pin Change Interrupts we can make any pin be an interrupt, but then we have to go through the trouble of determining which pin caused the interrupt on the port.
Here are two really good articles to read. This is how I learned:
- http://thewanderingengineer.com/2014/08/11/arduino… PCINT in general
- http://thewanderingengineer.com/2014/08/11/pin-cha… PCINT on the AT tiny
volatile int dig1 = 0; //Hours tens volatile int dig2 = 0; //Hours ones volatile int dig3 = 0; //Minutes tens volatile int dig4 = 0; //Minutes ones
Each of these variables determines what number is displayed on each of the four digits on the bubble display.
I declared them volatile because they will be changed in the interrupt routines.
Setting them to 0 means when I turn on my clock, the count will start at 00:00 (12:00 AM).
const int latch Pin = A3; //Pin connected to ST_CP of 74HC595 const int clock Pin = A4; //Pin connected to SH_CP of 74HC595 const int data Pin = A5; //Pin connected to DS of 74HC595
Physical pins 8(A5), 9(A4), and 10(A3) will be used with the 74HC595 shift register.
The shift register will allow us to control segments a, b, c, d, e, f, g, and dp with pins Q0, Q1, Q2, Q3, Q4, Q5, Q6, and Q7 respectively.
To display each digit, we will be multiplexing.
const int dig1Pin = A0; //Pin to multiplex and display digit1 const int dig2Pin = A1; //Pin to multiplex and display digit2 const int dig3Pin = A2; //Pin to multiplex and display digit3 const int dig4Pin = A6; //Pin to multiplex and display digit4
The bubble display is common cathode. This means that when the digit is grounded or pulled to a digital LOW, the digit will light up. We will be using physical pins 13(A0), 12(A1), 11(A2), and 7(A6) to control the display of each digit on the bubble display.
//Two values below for de bouncing. volatile unsigned long x last DE bounce Time = 0; volatile unsigned long y last DE bounce Time = 0; long de bounce Delay = 150; // the de bounce time; increase if the output flickers
These are some values we will be using to de bounce the two buttons when the interrupt is activated. Without de bouncing, one button press could be registered as five, and using the clock would be annoying.
Check out Arduino Examples/Digital/De bounce if you want.
//We will be using this value to prevent rollover for millis() that occurs every 49 days. unsigned long current Millis = 0; volatile unsigned long last Millis = 0;
Arduino can hold long variables. I will be using long variables current Millis and last Millis to read mill is(); After 49 days, the value of mill is() will get too big for the microcontroller and overflow back to 0.
This is a bad thing if I’m comparing current Millis and last Millis to debounce and the value of current Millis becomes 0. I’ll explain a little more later.
Read More: DIY Apple Clock