首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >误取ADC输入

误取ADC输入
EN

Stack Overflow用户
提问于 2017-03-31 03:18:49
回答 1查看 386关注 0票数 0

我使用的是皮质M3基板。这是我第一次编程这种板,所以我不知道如何阅读库,并使它“发生”。

这个板带有温度传感器,我正在尝试使用adc库进行读取。

我的代码:

代码语言:javascript
复制
#include <adc.h>
#include <gpio.h>

using namespace EPOS;
//const NIC::Protocol NIC_PROTOCOL = 42;

ADC * temperature;

int main()
{
   temperature = new ADC( 0xE, 0x2, 0x3);
   //    printf("&temperature"); 

   return 0;
}

图书馆

代码语言:javascript
复制
// EPOS ARM Cortex-M Analog to Digital Converter (ADC) Mediator Declarations

#include <system/config.h>
#if !defined(__cortex_adc_h_) && defined(__ADC_H)
#define __cortex_adc_h_

#include <adc.h>
#include <machine.h>

__BEGIN_SYS

class ADC: private ADC_Common, private Machine_Model
{
public:
    enum Channel {
        SINGLE_ENDED_ADC0 = 0,
        SINGLE_ENDED_ADC1 = 1,
        SINGLE_ENDED_ADC2 = 2,
        SINGLE_ENDED_ADC3 = 3,
        SINGLE_ENDED_ADC4 = 4,
        SINGLE_ENDED_ADC5 = 5,
        SINGLE_ENDED_ADC6 = 6,
        SINGLE_ENDED_ADC7 = 7,
        DIFF8             = 8,
        DIFF9             = 9,
        DIFF10            = 10,
        DIFF11            = 11,
        GND               = 12,
        TEMPERATURE       = 14,
        AVDD5_3           = 15,
    };

    enum Reference {
        INTERNAL_REF   = 0,
        EXTERNAL_REF   = 1, // External reference on AIN7 pin
        SYSTEM_REF     = 2,
        EXTERNAL_DIFF  = 3
    };

    enum Resolution {
        BITS_7  = 0, //  7 bits resolution, 64  decimation rate
        BITS_9  = 1, //  9 bits resolution, 128 decimation rate
        BITS_10 = 2, // 10 bits resolution, 256 decimation rate
        BITS_12 = 3  // 12 bits resolution, 512 decimation rate
    };

    ADC(const Channel & channel = SINGLE_ENDED_ADC5, const Reference & reference = SYSTEM_REF, const Resolution & resolution = BITS_12)
    : _channel(channel), _reference(reference), _resolution(resolution) {
        Machine_Model::adc_config(_channel);
    }

    short read() {
        reg(ADCCON3) = (_reference * ADCCON3_EREF) | (_resolution * ADCCON3_EDIV) | (_channel * ADCCON3_ECH);
        while(!(reg(ADCCON1) & ADCCON1_EOC));
        short ret = (reg(ADCH) << 8) + reg(ADCL);
        switch(_resolution) {
            case BITS_7:  ret >>= 9; break;
            case BITS_9:  ret >>= 7; break;
            case BITS_10: ret >>= 6; break;
            case BITS_12: ret >>= 4; break;
        }
        return ret;
    }

    // returns the voltage corresponding to the reading, with three decimal places (e.g. 2534 means 2.534V)
    int convert(short raw_reading, int reference = 3300/*3.3V*/) {
        int limit;
        switch(_resolution) {
            case BITS_7:  limit =   63; break;
            case BITS_9:  limit =  255; break;
            case BITS_10: limit =  511; break;
            case BITS_12: limit = 2047; break;
        }
        return raw_reading * reference / limit;
    }

private:
    volatile CPU::Reg32 & reg(unsigned int o) { return reinterpret_cast<volatile CPU::Reg32*>(ADC_BASE)[o / sizeof(CPU::Reg32)]; }

    Channel _channel;
    Reference _reference;
    Resolution _resolution;
};

__END_SYS

#endif

错误:

temperature.cc:在函数'int ():temperature.cc:11:从'int‘到’EPOS::s::ADC::temperature.cc:11: temperature.cc:11:初始化参数1‘EPOS::s:ADC::ADC::temperature.cc:11&,temperature.cc:11:错误:从'int‘到’EPOS::s::temperature.cc:11:初始化参数2‘EPOS::s::ADC:temperature.cc:11:错误:从'int‘到’EPOS::s::temperature.cc:11:初始化参数3的‘EPOS::s::ADC:const :ADC::分辨率&)‘makefile:11:目标’Temature.o‘失败的make2:* Temature.o错误1 make2:离开目录’/home/thiago/make2/arm/ app‘makefile:17: 'app’failed make1:*应用错误2 make1:离开目录'/home/thiago/Documents/arm‘makefile:11:用于目标“所有”失败的配方:*所有错误2

编程语言: c++

谢谢。

EN

回答 1

Stack Overflow用户

发布于 2017-03-31 06:23:28

ADC构造函数需要3 引用传递的参数,因此必须传递变量。

您的代码至少应该是:

代码语言:javascript
复制
int main(void)
{  
   Channel channel = TEMPERATURE; 
   Reference reference = SYSTEM_REF;
   Resolution resolution = BITS_12;

   temperature = new ADC( channel, reference, resolution);

   return 0;
}

编辑

正如其他用户已经说过的,您应该避免在资源有限的uC嵌入式应用程序上使用动态分配(主要是RAM)。

因此,您可以实例化类如下:

代码语言:javascript
复制
int main(void)
{  
   short temperature;

   Channel channel = TEMPERATURE; 
   Reference reference = SYSTEM_REF;
   Resolution resolution = BITS_12;

   ADC reader( channel, reference, resolution);

   while (1)
   {
      temperature = reader.read();
   }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43131829

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档