我是新手用C ++创建类,我无法看到我在以下代码中犯的错误。
我只想在每次调用ISR时递增变量 cnt ,并在调用 stopTimer 时返回。
这是AllTimer.cpp
#include "avr/interrupt.h"
#include "Arduino.h"
#include "util/delay.h"
#include "AllTimer.h"
AllTimer::AllTimer()
{
}
static int cnt;
void AllTimer::dofun(void)
{
//TCCR1B = 0X00; //stop the timer
digitalWrite(13,!digitalRead(13));
TCNT1 = 0X001F;
cnt = cnt+1;
//TCCR1B |= (1<<CS12) && (1<<CS10); //start the timer
}
int AllTimer::stopTimer()
{
TCCR1B = 0X00;
return(cnt);
}
void AllTimer::setTimer(void)
{
TCNT1 = 0x001F;
TCCR1A = 0x00;
TCCR1B = 0x00;
TCCR1B |= 0x03;
TIMSK1 |= (1<<TOIE1);
cnt = 0;
sei();
}
ISR(TIMER1_OVF_vect)
{
AllTimer::dofun();
}
标题文件:
#ifndef AllTimer_h
#define AllTimer_h
class AllTimer{
public:
AllTimer();
static void setTimer(void);
static void dofun(void);
static int stopTimer(void);
private:
static int cnt;
};
#endif
arduino IDE中的代码:
#include <AllTimer.h>
AllTimer mytimer;
void setup()
{
pinMode(13,OUTPUT);
Serial.begin(9600);
mytimer.setTimer();
}
void loop()
{
delay(1000);
int x = mytimer.stopTimer();
Serial.println("x is: ");
Serial.print(x);
}
编译时,这是错误:
AllTimer / Alltimer.cpp.o:在函数__vector_13':
/usr/share/arduino/libraries/AllTimer/Alltimer.cpp:43: undefined reference to
AllTimer :: cnt'中
/usr/share/arduino/libraries/AllTimer/Alltimer.cpp:44:对AllTimer::cnt'
/usr/share/arduino/libraries/AllTimer/Alltimer.cpp:44: undefined reference to
AllTimer :: cnt'的未定义引用
/usr/share/arduino/libraries/AllTimer/Alltimer.cpp:44:对AllTimer::cnt'
/usr/share/arduino/libraries/AllTimer/Alltimer.cpp:44: undefined reference to
AllTimer :: cnt'的未定义引用
AllTimer / Alltimer.cpp.o:/usr/share/arduino/libraries/AllTimer/Alltimer.cpp:44:对“AllTimer :: cnt”的更多未定义引用关注
collect2:错误:ld返回1退出状态