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

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

3D Printer Filament Joiner

I have been looking at various ways of joining 3D printing filaments. One method involves running one end of a filament through a short PTFE tubing, melting it with a lighter or candle, retracting it back into the tubing and immediately plunging the filament to be fused into the tubing: One problem with this method is that you can't really control the temperature at which you melt the filament, so you frequently end up with a brittle joint that breaks upon the slightest bend. Aliexpress even sells a contraption that works along the same line. As it uses a lighter or candle as well, it suffers from the same weakness. I am not even sure why you need a special contraption when a short PTFE tubing will work just as well. Another method involves using shrink tubing/aluminium foil, and a heat gun: But a heat gun is rather expensive, so I wanted to explore other alternatives. The candle + PTFE tubing method actually works quite well when you happen to melt it at the rig