类中的易失性变量:“'volatile'之前的预期不合格ID"? [英] Volatile variable in class: "expected unqualified-id before 'volatile'"?

查看:95
本文介绍了类中的易失性变量:“'volatile'之前的预期不合格ID"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在类 ADC 中定义了两个 static volatile 变量.该类的编写方式为:(裁剪以节省空间)

I have two static volatile variables defined in my class ADC. The class is written as: (cropped to save space)

#pragma once

#include "../PeriodicProcess/PeriodicProcess.h"
#include <stdint.h>
#include <stdlib.h>

class ADC
{
private:
    static inline unsigned char SPI_transfer(unsigned char data);
    void read(uint32_t tnow);

    static const unsigned char adc_cmd[9];
    static volatile uint32_t _sum[8];
    static volatile uint16_t _count[8];

public:
    ADC();
    void raw();
    void init(PeriodicProcess * scheduler);
    double ch(uint8_t c);
    bool available(const uint8_t *channelNumbers);
    uint32_t ch6(const uint8_t *channelNumbers, double *result);
};

但是编译项目会在第10、29、39和第10行的cpp文件中抛出错误.44:

But compiling the project throws me an error in the cpp file at line 10, 29, 39, & 44:

ADC.cpp:

#include "ADC.h"
extern "C" {
    // AVR LibC Includes
    #include <inttypes.h>
    #include <stdint.h>
    #include <avr/interrupt.h>
}
#include "Arduino.h"

const unsigned char ADC::adc_cmd[9] = { 0x87, 0xC7, 0x97, 0xD7, 0xA7, 0xE7, 0xB7, 0xF7, 0x00 };


#define bit_set(p,m)   ((p) |= ( 1<<m))
#define bit_clear(p,m) ((p) &= ~(1<<m))

// We use Serial Port 2 in SPI Mode
#define ADC_DATAOUT     51    // MOSI
#define ADC_DATAIN      50    // MISO
#define ADC_SPICLOCK    52    // SCK
#define ADC_CHIP_SELECT 33    // PC4   9 // PH6  Puerto:0x08 Bit mask : 0x40

// DO NOT CHANGE FROM 8!!
#define ADC_ACCEL_FILTER_SIZE 8

#define TCNT2_781_HZ   (256-80)
#define TCNT2_1008_HZ  (256-62)
#define TCNT2_1302_HZ  (256-48)

unsigned char ADC::SPI_transfer(unsigned char data)
{
    // Put data into buffer, sends the data
    UDR2 = data;
    // Wait for data to be received
    while ( !(UCSR2A & (1 << RXC2)) );
    // Get and return received data from buffer
    return UDR2;
}

ADC::ADC()
{
    //Do nothing
}

void ADC::read(uint32_t tnow)
{
    uint8_t ch;

    bit_clear(PORTC, 4);                            // Enable Chip Select (PIN PC4)
    ADC_SPI_transfer(adc_cmd[0]);                       // Command to read the first channel

    for (ch = 0; ch < 8; ch++) {
        uint16_t v;

        v = ADC_SPI_transfer(0) << 8;            // Read first byte
        v |= ADC_SPI_transfer(adc_cmd[ch + 1]);  // Read second byte and send next command

        if (v & 0x8007) {
            // this is a 12-bit ADC, shifted by 3 bits.
            // if we get other bits set then the value is
            // bogus and should be ignored
            continue;
        }

        if (++_count[ch] == 0) {
            // overflow ... shouldn't happen too often
            // unless we're just not using the
            // channel. Notice that we overflow the count
            // to 1 here, not zero, as otherwise the
            // reader below could get a division by zero
            _sum[ch] = 0;
            _count[ch] = 1;
        }
        _sum[ch] += (v >> 3);
    }

    bit_set(PORTC, 4);                  // Disable Chip Select (PIN PC4)
}

void ADC::init(AP_PeriodicProcess * scheduler)
{
    pinMode(ADC_CHIP_SELECT, OUTPUT);

    digitalWrite(ADC_CHIP_SELECT, HIGH);  // Disable device (Chip select is active low)

    // Setup Serial Port2 in SPI mode
    UBRR2 = 0;
    DDRH |= (1 << PH2); // SPI clock XCK2 (PH2) as output. This enable SPI Master mode
    // Set MSPI mode of operation and SPI data mode 0.
    UCSR2C = (1 << UMSEL21) | (1 << UMSEL20); // |(0 << UCPHA2) | (0 << UCPOL2);
    // Enable receiver and transmitter.
    UCSR2B = (1 << RXEN2) | (1 << TXEN2);
    // Set Baud rate
    UBRR2 = 2;  // SPI clock running at 2.6MHz

    // get an initial value for each channel. This ensures
    // _count[] is never zero
    for (uint8_t i=0; i<8; i++) {
        uint16_t adc_tmp;
        adc_tmp  = ADC_SPI_transfer(0) << 8;
        adc_tmp |= ADC_SPI_transfer(adc_cmd[i + 1]);
        _count[i] = 1;
        _sum[i]   = adc_tmp;
    }

    last_ch6_micros = micros();

    scheduler->register_process( AP_ADC_ADS7844::read );

}

// Read one channel value
double ADC::ch(uint8_t c)
{
    uint16_t count;
    uint32_t sum;

    // Ensure we have at least one value
    while (_count[c] == 0); // Waiting while

    // grab the value with interrupts disabled, and clear the count
    cli();
    count = _count[c];
    sum   = _sum[c];
    _count[c] = 0;
    _sum[c]   = 0;
    sei();

    return ((double)sum)/count;
}

// See if Ch6() can return new data
bool ADC::available(const uint8_t *channelNumbers)
{
    uint8_t i;

    for (i=0; i<6; i++)
    {
        if (_count[channelNumbers[i]] == 0)
        {
            return false;
        }
    }
    return true;
}

// Read 6 channel values
// this assumes that the counts for all of the 6 channels are
// equal. This will only be true if we always consistently access a
// sensor by either Ch6() or Ch() and never mix them. If you mix them
// then you will get very strange results
uint32_t ADC::ch6(const uint8_t *channelNumbers, double *result)
{
    uint16_t count[6];
    uint32_t sum[6];
    uint8_t i;

    // Ensure we have at least one value
    for (i=0; i<6; i++)
    {
        while (_count[channelNumbers[i]] == 0); // Waiting while
    }

    // Grab the values with interrupts disabled, and clear the counts
    cli();
    for (i=0; i<6; i++)
    {
        count[i] = _count[channelNumbers[i]];
        sum[i]   = _sum[channelNumbers[i]];
        _count[channelNumbers[i]] = 0;
        _sum[channelNumbers[i]]   = 0;
    }
    sei();

    // Calculate averages. We keep this out of the cli region
    // to prevent us stalling the ISR while doing the
    // division. That costs us 36 bytes of stack, but I think its
    // worth it.
    for (i = 0; i < 6; i++)
    {
        result[i] = (sum[i] + count[i]) / (double)count[i];
    }

    // Return number of microseconds since last call
    uint32_t us = micros();
    uint32_t ret = us - last_ch6_micros;
    last_ch6_micros = us;
    return ret;
}


错误报告提供:


With the error report providing:

ADC.cpp:10: error: expected unqualified-id before 'volatile'
ADC.cpp:10: error: expected `)' before 'volatile'
ADC.cpp:10: error: expected `)' before 'volatile'
ADC.cpp:29: error: expected unqualified-id before 'volatile'
ADC.cpp:29: error: expected `)' before 'volatile'
ADC.cpp:29: error: expected `)' before 'volatile'
ADC.cpp:39: error: expected unqualified-id before 'volatile'
ADC.cpp:39: error: expected `)' before 'volatile'
ADC.cpp:39: error: expected `)' before 'volatile'
ADC.cpp:44: error: expected unqualified-id before 'volatile'
ADC.cpp:44: error: expected `)' before 'volatile'
ADC.cpp:44: error: expected `)' before 'volatile'

这是怎么了?为什么指向我恒定的静态defn?为什么讨厌我的3种课堂方法?为什么在该行上不存在挥发物的情况下期望挥发物有一个')'?为什么期望一个不合格的ID (不是这样)吗?

What is wrong here? Why is it pointing to my constant static defn? Why does it hate 3 of my class methods? Why is it expecting a ')' for a volatile where a volatile does not exist on that line? Why is it expecting an unqualified-id (shouldn't that be the other way around)?

推荐答案

ADC 显然是AVR库下的保留字.我更改了班级名称,所有错误均消失了.麻烦的是使用了不合适的班级名称!

ADC is apparently a reserved word under an AVR library. I changed my class name and all the errors vanished. All that hassle for an improper class name!

我发现找到答案的来源: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1283472517 在第9个帖子上.

Source I found that figured it out: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1283472517 on the 9th post.

这篇关于类中的易失性变量:“'volatile'之前的预期不合格ID"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆