c - ADC dsPIC33 问题

标签 c pic mplab adc

我正在努力让 ADC 与我的设备配合使用。我正在使用 dsPIC33FJ128GP802,并尝试通过手动采样和转换缓慢启动。

我的代码发布在下面,我已经设置了 ADC 的每个寄存器,然后尝试仅采样一次以从我连接的传感器获取电压。我应该看到的值约为 0.7V,但我得到的值在 -17408 (10111100 00000000) 范围内。该值可能会上升到 -2000 左右,但该值首先不应该为负值。

#include <p33Fxxxx.h>

_FOSCSEL(FNOSC_FRCPLL) // select internal 7.37MHz osc with PPL
_FOSC(OSCIOFNC_OFF & POSCMD_XT) // no clock output, external OSC disabled
_FWDT(FWDTEN_OFF) // disable the watchdog timer
_FPOR(FPWRT_PWR1) // Turn off the power-up timers.

int ADCValue;

void DELAY(unsigned ms) {
    unsigned j;
    unsigned i;
    for (j = 0; j < ms; j++) {
        for (i = 0; i < 0x1F40; i++);
    }
 }

 int main(void) {

    // set up clock to 80MHz
    PLLFBD = 41; // sets M = 41+2 = 43
    CLKDIVbits.PLLPRE = 0; // sets N1 = 2
    CLKDIVbits.PLLPOST = 0; // sets N2 = 2
    while (!OSCCONbits.LOCK); // wait for PLL ready

    AD1CON1 = 0; // set everything to zero to start with.
    AD1CON1bits.ADON = 0; // turn ADC off.
    AD1CON1bits.ADSIDL = 0; // continue module operation in idle mode.
    AD1CON1bits.ADDMABM = 1; // DMA buffers are written in the order of conversion.
    AD1CON1bits.AD12B = 0; // set to 10bit mode.
    AD1CON1bits.FORM = 3; // set data output to signed fractional.
    AD1CON1bits.SSRC = 0; // manual conversion. clearing sample bit manually.
    AD1CON1bits.SIMSAM = 1; // collect samples from channels 0, 1, 2, 3 simultaneously.
    AD1CON1bits.ASAM = 0; // manual sample. samples when SAMP bit is set.
    AD1CON1bits.SAMP = 0; // sample enable bit.
    AD1CON1bits.DONE = 0; // ADC conversion status bit.

    AD1CON2 = 0; // set everything to zero to start with.
    AD1CON2bits.VCFG = 0; // converter voltage ref. set to AVdd and AVss.
    AD1CON2bits.CSCNA = 0; // input scan select bit. set to do not scan.
    AD1CON2bits.CHPS = 0; // channel select bits. set to just channel 0;
    AD1CON2bits.BUFS = 0; // buffer fill status (invalid as BUFM is 0);
    AD1CON2bits.SMPI = 0; // ADC interrupt is generated after every sample/conversion.
    AD1CON2bits.BUFM = 0; // buffer fill mode. set to always start filling from start address.
    AD1CON2bits.ALTS = 0; // Alternate input sample mode. set to always uses channel input from sample A.

    AD1CON3 = 0; // set everything to zero to start with.
    AD1CON3bits.ADRC = 0; // ADC conversion clock derived from system clock.
    AD1CON3bits.SAMC = 0; // auto sample time bits, TAD, set to 0.
    AD1CON3bits.ADCS = 0; // ADC conversion clock set to 0. 1 * TCY = TAD.

    AD1CON4 = 0; // set everything to zero to start with.
    AD1CON4bits.DMABL = 0; // allocates 1 word of buffer to each analogue input.

    AD1CHS123 = 0; // everything set to zero as not using channels 1, 2, or 3.

    AD1CHS0 = 0; // set everything to zero to start with.
    AD1CHS0bits.CH0NB = 0; // channel 0 negative input, set by CH0NA. sample B.
    AD1CHS0bits.CH0SB = 0; // channel 0 positive input, set by CH0SA. sample B.
    AD1CHS0bits.CH0NA = 0; // channel 0 negative input, for sample A. set to VREFL.
    AD1CHS0bits.CH0SA = 0; // channel 0 positive input is AN0.

    AD1CSSL = 0; // input scan register set to zero as not using it.

    AD1PCFGL = 0; // port configuration, set to analogue mode, ADC samples voltage.

    AD1CON1bits.ADON = 1; // turn on ADC

    AD1CON1bits.SAMP = 1; // Start sampling
    DELAY(1); // Wait for sampling time (1ms)
    AD1CON1bits.SAMP = 0; // Start the conversion
    while (!AD1CON1bits.DONE); // Wait for the conversion to complete
    ADCValue = ADC1BUF0; // Read the conversion result

    while (1);

}

我使用与 PIC 相同的电源轨为传感器供电,并且将传感器的输出连接到 AN0(引脚 2),正如我在代码中设置的那样。 PIC 由标准 Vss 和 Vdd(引脚 8 和 13)、模拟电源引脚 AVdd 和 AVss(引脚 28 和 27)以及跨 Vcap 和 Vss(引脚 20 和 19)的 33uF 电容器供电。我还需要在硬件方面做些什么吗?我对 AD1CHS0bits.CH0NA 寄存器有点困惑,因为我不知道是否必须将地连接到 VREFL 或在这种情况下该怎么做。

任何有关我应该采取哪些措施来纠正此问题的帮助将不胜感激!此外,有关如何在正确接收值后转换该值的任何帮助都将非常有帮助。

最佳答案

如果该值一开始就不应该为负数,那么您不应该使用此设置:

AD1CON1bits.FORM = 3; // set data output to signed fractional.

如果我期望你的值(value)是(使用Python评估):

int((2**10) *      # 10-bit operation
    (0.7/3.3)      # 0.7 volts on a 3.3 volt system
    - (2**9)       # Center at VDD / 2 because of signed operation
    ) << 6         # Fractional output left-shifted the 10-bit up by 6 to fill 16-bits
= -18816

这听起来像是您的代码输出的内容。

改为使用:

AD1CON1bits.FORM = 0; // set data output to integer.

使用此设置以及 10 位模式,我希望您的值为

int((2**10) *      # 10-bit operation
    (0.7/3.3))     # 0.7 volts on a 3.3 volt system
= 217

关于c - ADC dsPIC33 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15809613/

相关文章:

c - 嵌入式设备与远程服务器通信的协议(protocol)架构

c - 8 位系统上的 2 位乘法

c - C 中的 PIC 编程 : if any in list is true then

c - C 中 if(function() == TRUE) 的任何原因

c - fprintf 未写入文件

c - PTHREAD_MUTEX_INITIALIZER 与 pthread_mutex_init ( &mutex, param)

C - 每个服务器线程为许多客户提供服务

c - 在 pic18f2550 中运行 DES

c - 根据调试器,PIC18F26K83 LED 不工作

assembly - 如何在 MPLAB X 中使用 mips 汇编为每个宏调用创建唯一标签