【问题标题】:ADC giving strange output for PIC16F15325ADC 为 PIC16F15325 提供奇怪的输出
【发布时间】:2021-09-01 19:45:31
【问题描述】:

我试图读取 PIC16F15325 上引脚 RC3 上的模拟电压。我的电位器上有 3.23V 电压,它的输出接近 1.65V,连接到 PIC 微控制器的 RC3 引脚。对于配置和库,我使用了 MPLAB Code Cofigurator。代码如下:

#include "mcc_generated_files/mcc.h"


void main(void)
{
    // initialize the device
    SYSTEM_Initialize();
    EUSART1_Initialize();
    ADC_Initialize();
    
    adc_result_t val1 = 0;
    

    // When using interrupts, you need to set the Global and Peripheral Interrupt Enable bits
    // Use the following macros to:

    // Enable the Global Interrupts
    //INTERRUPT_GlobalInterruptEnable();

    // Enable the Peripheral Interrupts
    //INTERRUPT_PeripheralInterruptEnable();

    // Disable the Global Interrupts
    //INTERRUPT_GlobalInterruptDisable();

    // Disable the Peripheral Interrupts
    //INTERRUPT_PeripheralInterruptDisable();

    while (1)
    {
        // Add your application code
        val1 = ADC_GetConversion(19); // selected channel RC3
        printf("Value - %hu \n",val1);
        DELAY_milliseconds(1000);
    }
}

对于我上面提到的电压,我期望值接近“511”。任何超过 1023 [即(2^10) - 1] 很奇怪,因为 PIC 有 10 位 ADC。但是我得到的 uotput 是:

请帮我解决这个问题。

【问题讨论】:

  • 如果您需要帮助,您必须展示您的完整程序,而不仅仅是主循环。你真的认为 RC3 是 19 频道吗?
  • 请不要发布文本截图,将其作为文本插入。是的,您可以从 TeraTerm 复制粘贴。

标签: c microcontroller pic adc


【解决方案1】:

实际上,输出是正确的,但只是因为格式而看起来很奇怪。刚才我查看了数据表,其中介绍了 ADC 结果格式的 2 种方式。

PIC16(L)F15325/45 Datasheet page-228

ADC_Initialize(); ADCON1 = 0x10 内部,这意味着ADFM 是左移。我刚刚在ADC_GetConversion(19); 之后做了val1 = val1 >> 6;,它按预期工作。

【讨论】:

    猜你喜欢
    • 2017-08-07
    • 1970-01-01
    • 1970-01-01
    • 2014-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多