1

我正在尝试在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

我已经看到许多类似的讨论指向不正确/圆形#includes。但是,我自己似乎没有看到同样的问题。有任何想法吗?


主文件

#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
4

1 回答 1

3

(注意:做出回答是为了将其排除在未回答的问题列表之外。迈尔斯似乎决定不回答,我不认为这个问题是错字。)

您试图阻止重新包含您的标头的方式是使其仅在恰好已经定义了保护宏的情况下才使标头的内容可见,当然它永远不会。

为了解决这个问题,改变

#ifdef TIMERSAMD21_H
#define TIMERSAMD21_H

进入

#ifndef TIMERSAMD21_H
#define TIMERSAMD21_H

这将首先保持标题内容在第一次包含时可见。
然后它将定义保护宏,这将防止标题内容在同一翻译单元(即代码文件)中被第二次编译。

于 2018-07-31T05:28:07.333 回答