ATtiny45 Arduino timer problem

I need a simple timer device to trigger the shutter of the Canon Eos camera. The timer device is completed but with some minor problem. That is the timer is lagging by one second per minute.
Is there any way to solve this problem?
Please see my blog for details.

Are you using an external crystal or the internal oscillator?

I removed the crystal & 22pf capacitors. It using 8Mhz internal clock.

That's around 1.6% slow, my bet is that the internal oscillator isn't calibrated quite right. I think Coding Badly's "TinyTuner" will run on an attiny45, it should let you dial things in for better accuracy.

Thank you, Bobnova. I found this site (http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1287558192) based on what you say. I will try this later on.

That's exactly what I was thinking of!
Let me/us know how it works, I haven't actually used it yet myself.
I've played with the OSCCAL register, it's great fun, but I wasn't aiming for 8mhz or 1mhz so tinytuner couldn't help.

I'm getting stuck after studying the "TinyTuner". What is the next thing I should do after getting the value of OSCCAL ?

In the setup section of your sketch (or anywhere, really, but I like setup as setting the register is definitely part of setting the thing up), put this line in:

OSCCAL = 0x4A //Set the clock calibration

Replace the 0x4A with whatever Tinytuner spits out as a calibration value.

Plus side: Your attiny is now running with a decently accurate clock.
Minus side: You have to do this for each individual chip, every MCU's oscillator is out to lunch in a random direction, the value that works perfectly for one attiny84 may give you a clock completely out to lunch in another attiny84. Most likely, in fact.
So if you put the same code on multiple chips you'll have to edit the calibration line in the sketch to match the MCU.

I added OSCCAL=0x3D; to my sketch (just after voide setup()) & it compiled successful.
But I haven't get the actual value of OSCCAL yet. After getting the OSCCAL value, I will recompile the sketch & upload to the ATTiny45 & test the timer device for accuracy.

It is 1.27AM. I have to sleep now.

Thank again, Bobnova.

#include <Time.h> //include date time function such as hour(), minute(), second()

const int cameraTriggerPin = 3; // TINY45 PIN2 as output

void setup() {
OSCCAL=0x3D;
pinMode(cameraTriggerPin, OUTPUT); // set the digital pin as output
digitalWrite(cameraTriggerPin, LOW);
cameraTrigger(); //shoot once on startup
setTime(0,0,0,01,01,01); //setTime(hr,min,sec,day,month,yr);
}

void loop()
{
if (minute()==1 && second()==0) cameraTrigger();
if (minute()==2 && second()==0) cameraTrigger();
if (minute()==3 && second()==0) cameraTrigger();
if (minute()==4 && second()==0) cameraTrigger();
if (minute()==5 && second()==0) cameraTrigger();
if (minute()==6 && second()==0) cameraTrigger();
}

void cameraTrigger(void)
{
digitalWrite(cameraTriggerPin, HIGH);
delay(1000); //delay 1000ms
digitalWrite(cameraTriggerPin, LOW);
}

Just for entertainment you can upload with it set to 4D and see how out to lunch it is. OSCCAL has a large range, I've been using it to drop from 8Mhz to 6.49Mhz.

By adding OSCCAL=0x6C; to the sketch, the timer is working accurate now.

At the same time, I changed the clock source from 8MHz to 1Mhz in order to save power (I'm using the battery to power the ATtiny45, so I solder a 7805 regulator to the circuit). Below is the settings that I use for the boards.txt & AVR Studio fuses

Modify f_cpu from boards.txt:
attiny45.build.f_cpu=1000000L

Check CKDIV8 from AVR Studio:
EXTENDED: 0XFF
HIGH: 0XDF
LOW: 0X62

Everything is working perfectly.

To really save power...

Run the ATtiny45 directly from battery power / eliminate the 7805 regulator.

Put the processor to sleep (idle mode) at the bottom of loop.

Awesome! Glad it's working for you.

The sleep functions are fun to play with, I think there are some sleep modes that leave the timer running at full speed and hence full accuracy.
I think that in the deepest sleep it switches to the 1xx khz doodad that has dubious accuracy. How dubious I don't know though.

I'm using a fairly deep sleep on one of my projects, I'll have to attach timestamps to the output and see how bad the sleep mode timer is.

I'm using solar cell 11.5v to power the camera & the timer device, so there is no way to remove the 7805 voltage regulator.
Regarding power save mode, I found this site Reduce ATtiny power consumption by sleeping with the Watchdog Timer - Part 2 - YouTube.
Anybody try this? How accuracy is this?

there are some sleep modes that leave the timer running at full speed and hence full accuracy

Idle mode.

I think that in the deepest sleep it switches to the 1xx khz doodad

Nope. All the other sleep modes turn off the processor clock.

The watchdog is independent of sleeping.

Anybody try this?

He's doing way too much work. The code in <wdt.h> takes care of all the things he's looking-up and and changing.

How accuracy is this?

The watchdog timer is about as accurate as the internal oscillator (the watchdog timer on newer processors appears to be rather good). Unfortunately, it's difficult to use it instead of the time functions.

In addition to this...
Put the processor to sleep (idle mode) at the bottom of loop.

This will also reduce the power consumption...
Locate and open "core_build_options.h" in the Tiny Core files. Towards the top of the file, locate the following #defines and change them to...

/*=============================================================================
  Low power / smaller code options
=============================================================================*/

#define INITIALIZE_ANALOG_TO_DIGITAL_CONVERTER    0
#define INITIALIZE_SECONDARY_TIMERS               0

Edit: spelling corrected.

Thank You, Coding Badly . I will test the power idle mode.

Good luck and you know where to go if you need help.