Skip to main content

ESPCLOCK2, Part 2 - Interrupt-driven time keeping

Due to the time sensitive nature of I2C, everything we do in the ATtiny85 has to be interrupt-driven. We cannot use any delay() in the code.

The ATtiny85 has two timers, Timer0 and Timer1. We will use Timer0 to drive the clock pins, and Timer1 to keep time.

First we configure Timer1 to interrupt every second:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#define TIMER1_PRESCALER 4096
#define OCR1A_DEFVAL ((byte)(F_CPU / (float)TIMER1_PRESCALER * 1.0) - 1)

// Reset prescalers for Timer1
GTCCR |= bit(PSR1);

// Setup Timer1 
TCCR1 = 0;
TCNT1 = 0;
OCR1A = OCR1A_DEFVAL;
OCR1C = OCR1A_DEFVAL;
TCCR1 = bit(CTC1) | bit(CS13) | bit(CS12) | bit(CS10); // Start Timer1 in CTC mode; prescaler = 4096; 

// Interrupt on compare match with OCR1A
TIMSK |= bit(OCIE1A);

Then in the ISR for Timer1, we simply keep track of the actual time (we call this nettime, or actual time from the network that we are trying to get the clock face to match).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
volatile byte nettime[3]; // network clock time

// Interrupt service routine for Timer1
ISR(TIMER1_COMPA_vect) {
  incClockTime(nettime[HH], nettime[MM], nettime[SS]);
}

void incClockTime(volatile byte& hh, volatile byte& mm, volatile byte& ss) {
  if (++ss >= 60) {
    ss = 0;
    if (++mm >= 60) {
      mm = 0;
      if (++hh >= 12) {
        hh = 0;
      }
    }
  }
}

We configure Timer0 to interrupt every 200ms:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#define TIMER0_PRESCALER 1024
#define OCR0A_DEFVAL ((byte)(F_CPU / (float)TIMER0_PRESCALER * 200/1000.0) - 1)

// Reset prescalers for Timer0
GTCCR |= bit(PSR0)
  
// Setup Timer0 (but don't run it yet)
TCCR0A = 0;
TCCR0B = 0;
TCNT0  = 0;
TCCR0A = bit(WGM01); // CTC mode
OCR0A = OCR0A_DEFVAL;

// Interrupt on compare match with OCR0A and OCR1A
TIMSK |= bit(OCIE0A);

Then when we need to move the second hand by one tick, we do this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
volatile byte clocktime[3]; // physical clock time
volatile byte timer0_tickpin = PB3;

incClockTime(clocktime[HH], clocktime[MM], clocktime[SS]);

digitalWrite(timer0_tickpin, HIGH);
// Set prescaler to 1024, thereby starting Timer0
TCCR0B = bit(CS02) | bit(CS00); 

ISR(TIMER0_COMPA_vect) {
  digitalWrite(timer0_tickpin, LOW);
  timer0_tickpin = (timer0_tickpin == PB3 ? PB4: PB3);
  // Set prescaler to 0, thereby stopping Timer0
  TCCR0B = 0;
}

This is essentially doing the same as the following without using delay():

1
2
3
4
5
6
7
8
9
byte tickpin = PB3;

digitalWrite(tickpin, HIGH);
delay(200);

digitalWrite(tickpin, LOW);
delay(200);

tickpin = (tickpin == PB3 ? PB4: PB3);

The 200ms interval was picked after doing some stress tests witht the clock. 50ms, 100ms and 150ms resulted in some missed ticks when done in quick succession, while 200ms nailed all the tests.

Note that the Timer0 interrupt is enabled/disabled when required. Initially I tried turning off the Timer0 interrupt by setting OCIE0A in TIMSK to 0. This worked, as in the interrupt is indeed disabled, but OCR0A continues to run, resulting in inconsistent timing when interrupt is enabled again. Later, I found out that setting TCCR0B to 0 is the correct way to stop Timer0 when required.


Comments

Popular posts from this blog

Adding "Stereo Mixer" to Windows 7 with Conexant sound card

This procedure worked for my laptop (Thinkpad E530) with a Conexant 20671 sound card, but I suspect it will work for other sound cards in the Conexant family. I was playing with CamStudio to do a video capture of a Flash-based cartoon so that I can put it on the WDTV media player and play it on the big screen in the living room for my kids. The video capture worked brilliantly, but to do a sound capture, I needed to do some hacking. Apparently, there was this recording device called "Stereo Mixer" that was pretty standard in the Windows XP days. This allowed you to capture whatever was played to the speaker in all its digital glory. Then under pressure from various organizations on the dark side of the force, Microsoft and soundcard makers starting disabling this wonderful feature from Windows Vista onwards. So after much Googling around, I found out that for most sound cards, the hardware feature is still there, just not enabled on the software side. Unfortunately, to

Update: Line adapter for Ozito Blade Trimmer

Update (Dec 2021): If you access to a 3D printer, I would now recommend this solution , which makes it super easy to replace the trimmer line. I have been using it for a few months now with zero issue.

Attiny85 timer programming using Timer1

This Arduino sketch uses Timer1 to drive the LED blinker: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 /* * Program ATTiny85 to blink LED connected to PB1 at 1s interval. * Assumes ATTiny85 is running at 1MHz internal clock speed. */ #include <avr/io.h> #include <avr/wdt.h> #include <avr/sleep.h> #include <avr/interrupt.h> bool timer1 = false , led = true ; // Interrupt service routine for timer1 ISR(TIMER1_COMPA_vect) { timer1 = true ; } void setup() { // Setup output pins pinMode( 1 , OUTPUT); digitalWrite( 1 , led); set_sleep_mode(SLEEP_MODE_IDLE); // Setup timer1 to interrupt every second TCCR1 = 0 ; // Stop timer TCNT1 = 0 ; // Zero timer GTCCR = _BV(PSR1); // Reset prescaler OCR1A = 243 ; // T = prescaler / 1MHz = 0.004096s; OCR1A = (1s/T) - 1 = 243 OCR1C = 243 ; // Set to same value to reset timer1 to