2012-04-03 26 views
4

我有兩個static volatile變量在我的類ADC中定義。該類被寫爲:(裁剪以節省空間)類中的易變變量:「在'volatile'之前預期的不合格id」「?

#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); 
}; 

但是編譯項目拋出我的CPP文件中的錯誤,在第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; 
} 

與錯誤報告提供:

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' 

這裏有什麼問題?爲什麼它指向我的恆定靜態定義?爲什麼它討厭我的三個類方法?爲什麼它期望一個')'的揮發性不存在於該行?爲什麼它期待不合格ID(應該不是相反的方式)?

+1

請貼上一個簡單的,自包含的*相干*和*完整*的例子來證明你的問題,而不是這個科學怪人的複製粘貼湯。 – 2012-04-03 22:22:53

+0

您向我們展示了您的頭文件,但錯誤來自.cpp文件。請發佈_relevant_代碼(在這種情況下,定義)。 – ildjarn 2012-04-03 22:27:11

+0

@KerrekSB對不起。我認爲問題出在我的頭文件中。我編輯了這篇文章,包括我目前在ADC.cpp中的內容。 – jakebird451 2012-04-03 22:35:51

回答

2

ADC顯然是AVR庫下的保留字。我改變了我的班名,所有的錯誤都消失了。所有這一切都爲一個不合適的課程名稱而煩惱!

來源我發現,計算出來:http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1283472517在第9貼。

+1

它不是保留字,因爲它們只在語言定義中設置。 – Mark 2012-04-05 10:51:36

相關問題