Go Down

Topic: ATtiny45 Arduino timer problem (Read 2 times) previous topic - next topic

SM.Ching

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.
http://smching.wordpress.com/2011/08/11/attiny45-arduino/
Electronics and computer projects
http://ediy.com.my/

Coding Badly


Are you using an external crystal or the internal oscillator?

SM.Ching

I removed the crystal & 22pf capacitors. It using 8Mhz internal clock.
Electronics and computer projects
http://ediy.com.my/

Bobnova

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.

SM.Ching

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.
Electronics and computer projects
http://ediy.com.my/

Bobnova

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.

SM.Ching

I'm getting stuck after studying the "TinyTuner". What is the next thing I should do after getting the value of OSCCAL ?
Electronics and computer projects
http://ediy.com.my/

Bobnova

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.

SM.Ching

#8
Aug 13, 2011, 07:28 pm Last Edit: Aug 14, 2011, 05:18 am by ching Reason: 1
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);
}
Electronics and computer projects
http://ediy.com.my/

Bobnova

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.

SM.Ching

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.

Electronics and computer projects
http://ediy.com.my/

Coding Badly


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.

Bobnova

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.

SM.Ching

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 http://www.youtube.com/watch?v=Zwo_xuC5A48.
Anybody try this? How accuracy is this?
Electronics and computer projects
http://ediy.com.my/

Coding Badly

#14
Aug 14, 2011, 08:57 am Last Edit: Aug 15, 2011, 08:27 am by Coding Badly Reason: 1
Quote
there are some sleep modes that leave the timer running at full speed and hence full accuracy


Idle mode.

Quote
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.

Quote
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.

Quote
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...

Code: [Select]
/*=============================================================================
 Low power / smaller code options
=============================================================================*/

#define INITIALIZE_ANALOG_TO_DIGITAL_CONVERTER    0
#define INITIALIZE_SECONDARY_TIMERS               0



Edit: spelling corrected.

Go Up