0

我对 C++ 和除了 Arduino 的基础知识还很陌生,所以我坚持这一点。我收到了一个函数,可以包含在我的代码中,对我正在读取的传感器值进行一些计算。我在这里和网上搜索并阅读了很多类似的主题,但没有找到任何可以帮助我解决我缺乏理解的东西。

我设置了一个带有循环的基本 Arduino 草图,以读取传感器值。然后我#included头文件并将函数添加到循环外的代码底部,只是为了查看它是否会编译,它没有任何错误。所以现在需要调用函数并向它传递 2 个值,millis()以及我刚刚读取的传感器值。

部分头文件...

struct TCS1000v {
    unsigned short int  u16RawVal;      
    unsigned short int  u16RawValPrev;  
    unsigned short int  u16CycleTime;
    unsigned short int  u16Iso4um;      
    unsigned short int  u16Iso6um;      
    unsigned short int  u16Iso14um; 
        .....
};

typedef struct TCS1000v;

extern void vCalcCS1000v2(TCS1000v* ptCS1000, unsigned short int u16RawVal, unsigned short int u16CycleTime);

草图的循环部分

void loop() {
    // reading the sensor...
    unsigned short int u16CycleTime = millis();
    unsigned short int u16RawVal = adc.readsensor(channel);
    // the function to call - not sure about this?
    vCalcCS1000v2(TCS1000v* ptCS1000, unsigned short int u16RawVal, unsigned short int u16CycleTime)
}

提供的功能...

    void vCalcCS1000v2(TCS1000v* ptCS1000, unsigned short int u16RawVal, unsigned short int u16CycleTime)
{
    ptCS1000->u16RawVal   = u16RawVal;
    ptCS1000->u16CycleTime  = u16CycleTime;
    //.... and the rest of the code in the function
}

编译错误...

test.ino: In function 'void loop()':
cs1000:30:25: error: expected primary-expression before '*' token
   vCalcCS1000v2(TCS1000v* ptCS1000, unsigned short int u16RawVal, unsigned short int u16CycleTime)
                         ^
cs1000:30:27: error: 'ptCS1000' was not declared in this scope
   vCalcCS1000v2(TCS1000v* ptCS1000, unsigned short int u16RawVal, unsigned short int u16CycleTime)
                           ^
cs1000:30:37: error: expected primary-expression before 'unsigned'
   vCalcCS1000v2(TCS1000v* ptCS1000, unsigned short int u16RawVal, unsigned short int u16CycleTime)
                                     ^
cs1000:30:67: error: expected primary-expression before 'unsigned'
   vCalcCS1000v2(TCS1000v* ptCS1000, unsigned short int u16RawVal, unsigned short int u16CycleTime)
                                                                   ^
exit status 1
expected primary-expression before '*' token

所以结构是在头文件中设置的,但问题似乎出在指针上?还是我只是没有正确调用该函数?

4

2 回答 2

0

在头文件中,您定义了一种名为 TCS1000v 的新类型,但不正确。利用

typedef struct {
    unsigned short int  u16RawVal;      
    unsigned short int  u16RawValPrev;  
    unsigned short int  u16CycleTime;
    unsigned short int  u16Iso4um;      
    unsigned short int  u16Iso6um;      
    unsigned short int  u16Iso14um; 
} TCS1000v;

extern void vCalcCS1000v2(TCS1000v* ptCS1000, 
                          unsigned short int u16RawVal, 
                          unsigned short int u16CycleTime);
于 2019-08-22T23:52:54.167 回答
0

最后我不得不设置一个全局变量,现在一切正常。谢谢你看:)

TCS1000v g_tCS1000v;  
vCalcCS1000v2(&(g_tCS1000v), RawVal, CycleTime);
于 2019-08-30T04:12:21.977 回答