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.

ESPCLOCK1 / ESPCLOCK2 / ESPCLOCK3 / ESPCLOCK4

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

Hacking a USB-C to slim tip adapter cable to charge the Thinkpad T450s

This hack is inspired by this post . A year ago, I bought an adapter cable for my wife's Thinkpad X1 Carbon (2nd Gen) that allows her to power her laptop with a 60W-capable portable battery (20V x 3A). A USB-C cable goes from the battery into the adapter, which converts it to the slim tip output required by the laptop. Everything works out of the box, so I didn't give much thought about it. Recently, I decided to buy a similar cable for my Thinkpad T450s. I know technically it should work because the T450s can go as low as 45W (20V x 2.25A) in terms of charging (though I have the 65W charger - 20V x 3.25A).  I went with another adapter cable because it was cheaper and also I prefer the single cable design. So imagine my surprise when the cable came and I plugged it into my laptop and it didn't work! The power manager just cycle in and out of charging mode before giving up with an error message saying there is not enough power. After much research and reading the Thinkwiki

Using Google Dashboard to manage your Android device backup

I used to use AppBrain/Fast Web Install to keep track of which apps I have installed on my phone, and to make it easier to reinstall those apps when the phone gets wiped or replaced. But AppBrain had been going down the tubes, and Fast Web Install had always been a hit-and-miss affair. Android's own "backup to the cloud" system had previously been even more unusable. There isn't a place where you can see what has been backed up. And when you setup a new phone with your Google account, you just have to wait and pray that your favorite apps will be restored to the phone. Typically all the stars have to be aligned just right for this to happen. More often than not, after waiting for an hour or so and nothing happens, you just curse under your breath and proceed to install your favorites apps manually via the Play Store. But I just looked again recently and was pleasantly surprised that things are much more civilized now. Firstly there is a place now where you can loo