我正在尝试在Atmel Studio中使用 C++ 对Atmel SAM D21微控制器进行编程。我正在尝试使用其中一个片上定时器来创建周期性的硬件中断。
我创建了Timer4
类来设置计时器main.cpp
。我试图创建一个在主函数中Timer4
调用的实例,但它说MyTimer4
'Timer4' was not declared in this scope
'MyTimer4' was not declared in this scope
我已经看到许多类似的讨论指向不正确/圆形#include
s。但是,我自己似乎没有看到同样的问题。有任何想法吗?
主文件
#include "timerSAMD21.h"
#include "sam.h"
void SampleADC(void)
{
}
int main(void)
{
SystemInit();
Timer4 MyTimer4;
MyTimer4.setRate(1000);
MyTimer4.onEvent(SampleADC);
MyTimer4.start;
}
定时器SAMD21.h
#ifdef TIMERSAMD21_H
#define TIMERSAMD21_H
#include "tc.h"
#include "tc4.h"
#include "gclk.h"
typedef void (*voidFuncPtr)(void);
class Timer4
{
public:
Timer4() {};
void setRate(int frequency);
void start(void);
void end(void);
void onEvent(voidFuncPtr funcOnEvent);
private:
void configure(int frequency);
void enable(void);
void disable(void);
void reset(void);
};
#endif
定时器SAMD21.cpp
#include "timerSAMD21.h"
voidFuncPtr callback = NULL;
void Timer4::setRate(int frequency) {
configure(frequency);
}
void Timer4::start(void) {
enable();
}
void Timer4::end(void) {
disable();
reset();
}
void Timer4::configure(int frequency) {
//Configuration code here. Removed for Stack Overflow.
}
void Timer4::enable(void){
REG_TC4_CTRLA |= TC_CTRLA_ENABLE; //Enable timer
while (TC4->COUNT8.STATUS.bit.SYNCBUSY);
}
void Timer4::disable(void) {
REG_TC4_CTRLA &= ~TC_CTRLA_ENABLE;
while (TC4->COUNT8.STATUS.bit.SYNCBUSY);
}
void Timer4::reset(void) {
REG_TC4_CTRLA = TC_CTRLA_SWRST;
while (TC4->COUNT8.STATUS.bit.SYNCBUSY);
while (TC4->COUNT8.CTRLA.bit.SWRST);
}
void Timer4::onEvent(voidFuncPtr funcOnEvent){
callback = funcOnEvent;
}
#ifdef __cplusplus
extern "C" {
#endif
void IRQHandlerTimer4(void) {
if (callback != NULL)
{
callback();
}
REG_TC4_INTFLAG = TC_INTFLAG_MC0;
}
#ifdef __cplusplus
}
#endif