0

I want to make a timer library. The cpp file I have written is:

#include "avr/interrupt.h"
#include "Arduino.h"

#include "AllTimer.h"

AllTimer::AllTimer()
{}

void AllTimer::dofun(void)
{
    TIFR1 |= _BV(OCF1A);
    TCCR1B = 0X00;
    digitalWrite(13,!digitalRead(13));
}

void AllTimer::setTimer(void)
{
    TCNT1 = 0x0BDC;
    TCCR1A = 0x00;
    TCCR1B = 0x00;
    TCCR1B |= (1<<CS12) && (1<<CS10);
    TIMSK1 |= (1<<TOIE1);
    sei();
}

ISR (TIMER1_OVF_vect)
{
    dofun();
}

And the header file:

#ifndef AllTimer_h
#define AllTimer_h

class AllTimer{
public:
    AllTimer();
    void setTimer(void);

private:
    void dofun(void);
};

#endif

While compiling the file i am getting the following error:

sketch_jan05a.cpp: In function ‘void __vector_13()’:
sketch_jan05a.cpp:34:9: error: ‘dofun’ was not declared in this scope
1
  • You know its time to go home when you make this mistake. readTouchscreenParamsGeneric in header vs readTouchScreenParamsGeneric in cpp. Generates the same error message. Doing a search all, case-insensitive, led me to believe I had made no typos.... when in doubt, also turn on case-sensitive search.
    – Kevin
    Commented Dec 2, 2015 at 0:54

3 Answers 3

0

Answering here because I do not have enough rep to comment. At each iteration, the value in the timer counter must be reloaded. Either the ISR or dofun are responsible to reload TCNT1.

4

Because in the call:

 dofun();

you should put from which class you are calling that function. In your case:

AllTimer::dofun();

in that case your function must be declared as static method.

EDIT: Thanks to the comment:

I'm not pretty sure if your code reflects what you want to do. Because you can create your timer after defining the class:

Alltimer mytimer;

and call the method as a normal function (it must be declared as public):

ISR (TIMER1_OVF_vect)
{
    mytimer.dofun();
}

But it is hard to say since I m not sure what you want to do.

1
  • 1
    That will work only if dofun() is modified to be a static method. It must also be made public:.
    – jfpoilpret
    Commented Jan 5, 2015 at 10:11
1

dofun() is currently declared as a private member function, meaning you can't call it directly. It can only be called internally by an instance of the AllTimer class.

It looks like you probably want to declare it as a public static function instead. To do that, remove the private specifier from the class declaration, and put static before the function declaration. Your header should look something like this:

class AllTimer
{
public:
    AllTimer();
    void setTimer(void);
    static void dofun(void);
};

You will then need to change the ISR to include the class name in the call:

ISR (TIMER1_OVF_vect)
{
    AllTimer::dofun();
}
3
  • How do i define dofun in cpp file?
    – explorer
    Commented Jan 6, 2015 at 19:33
  • @user3929110 The existing definition should be fine. Commented Jan 6, 2015 at 22:56
  • The code compiled well but the timer isn't working. The LED is remaining ON only.
    – explorer
    Commented Mar 16, 2015 at 8:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.