Pages: [1] 2   Go Down
Author Topic: ATtiny45 Arduino timer problem  (Read 2519 times)
0 Members and 1 Guest are viewing this topic.
Malaysia
Offline Offline
Newbie
*
Karma: 0
Posts: 29
A place for DIY projects
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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/
Logged

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

Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 197
Posts: 12744
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


Are you using an external crystal or the internal oscillator?
Logged

Malaysia
Offline Offline
Newbie
*
Karma: 0
Posts: 29
A place for DIY projects
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Humboldt, CA
Offline Offline
Full Member
***
Karma: 2
Posts: 223
Arduino BBB
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Malaysia
Offline Offline
Newbie
*
Karma: 0
Posts: 29
A place for DIY projects
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Humboldt, CA
Offline Offline
Full Member
***
Karma: 2
Posts: 223
Arduino BBB
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Malaysia
Offline Offline
Newbie
*
Karma: 0
Posts: 29
A place for DIY projects
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Humboldt, CA
Offline Offline
Full Member
***
Karma: 2
Posts: 223
Arduino BBB
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Malaysia
Offline Offline
Newbie
*
Karma: 0
Posts: 29
A place for DIY projects
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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);
}
« Last Edit: August 13, 2011, 10:18:38 pm by ching » Logged

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

Humboldt, CA
Offline Offline
Full Member
***
Karma: 2
Posts: 223
Arduino BBB
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Malaysia
Offline Offline
Newbie
*
Karma: 0
Posts: 29
A place for DIY projects
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

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

Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 197
Posts: 12744
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


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

Humboldt, CA
Offline Offline
Full Member
***
Karma: 2
Posts: 223
Arduino BBB
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Malaysia
Offline Offline
Newbie
*
Karma: 0
Posts: 29
A place for DIY projects
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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 .
Anybody try this? How accuracy is this?
Logged

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

Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 197
Posts: 12744
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
/*=============================================================================
  Low power / smaller code options
=============================================================================*/

#define INITIALIZE_ANALOG_TO_DIGITAL_CONVERTER    0
#define INITIALIZE_SECONDARY_TIMERS               0


Edit: spelling corrected.
« Last Edit: August 15, 2011, 01:27:41 am by Coding Badly » Logged

Pages: [1] 2   Go Up
Jump to: