Skip to main content

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 0 after a compare match
  TIMSK = _BV(OCIE1A); // Interrupt on compare match with OCR1A
  
  // Start timer in CTC mode; prescaler = 4096; 
  TCCR1 = _BV(CTC1) | _BV(CS13) | _BV(CS12) | _BV(CS10);
  sei();
}

void loop() {
  if (timer1) {
    timer1 = false;
    led = !led;
    digitalWrite(1, led);
  }
  sleep_enable();
  sleep_cpu(); // CPU goes to sleep here; will be woken up by timer1 interrupt
}

The internal RC oscillator can have a factory variance of up to 10%. I observe that my blinker pulsed slightly faster than 1Hz.

Thankfully, unlike the watchdog oscillator, the internal RC oscillator can be calibrated by using the OSCCAL register, or adjusting the value of OCR1A. I believe it should not be too difficult to automatically calibrate the oscillator between NTP calls by taking the difference in actual time and clock time (in seconds) and adjust OCR1A accordingly to get as close to 1Hz as possible.

So on initial startup, the ESPCLOCK may not be pulsing at 1Hz, but after 15 or 30 minutes, it should be fully calibrated.

The downside with this approach is we can only use SLEEP_MODE_IDLE to keep Timer1 running during sleep, which is not as power efficient  as using the Watchdog Timer.

Comments

  1. Hi .help me to change the function from arduino codes at attyni85 usb .please help with .thanks
    void setup() {
    // put your setup code here, to run once:

    //timer2
    OCR2A = 127; //50% duty cycle
    TCCR2A = _BV(COM2A0) | _BV(WGM21) | _BV(WGM20); //toggle output, fast PWM mode
    TCCR2B = _BV(WGM22) | _BV(CS21) | _BV(CS20); // fast PWM mode, prescale 1:64
    pinMode(11, OUTPUT);

    }

    void loop()
    {

    delay(200); //duration of each frequency
    OCR2A += 10; //step size of frequency change
    if(OCR2A >= 245){OCR2A = 10; } //range of sweep, 245 is max.

    }

    ReplyDelete
  2. there are only 2 timers on attiny85, timer0 and timer1

    ReplyDelete

Post a Comment

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.

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