问题描述
我试图读取 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 是:
请帮我解决这个问题。
解决方法
实际上,输出是正确的,但只是因为格式而看起来很奇怪。刚刚我查看了数据表,其中介绍了 ADC 结果格式的 2 种方式。
PIC16(L)F15325/45 Datasheet page-228
在 ADC_Initialize();
ADCON1 = 0x10 里面,这意味着 ADFM 是左移。我只是在 val1 = val1 >> 6;
之后做了 ADC_GetConversion(19);
并且它按预期工作。