本文介绍了PIC16F690 ADC适用于一个通道,不适用于另一通道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了代码来设置和读取去往ADC板载PIC的电压,并在LCD上显示数字值.我在模拟通道9上运行了代码,它运行良好,在LCD上显示了正确的值.但是,当我切换到频道7时,我运行了相同的代码,但它不起作用(显示了错误的值).我非常肯定我的ADC和模拟通道设置正确.还可以肯定是问题所在是ADC.

这是一些代码:

I have written code to both setup and read the voltage going to the PICs onboard ADC, and displaying the digital value on an LCD. I ran the code with analog channel 9, and it worked perfectly, displaying the correct value on the LCD. However, when I changed to channel 7, I ran the same code and it didn''t work (it displayed an incorrect value). I''m just about positive that my setup of the ADC and Analog channels are correct. Also pretty sure that it is the ADC that is the problem.

Here is some code:

void setupADC(unsigned char channel)
{
    ADCON1 = 0b00010000;
    // Sets ad to Fosc/8
    ADCON0 = (channel << 2) + 0b10000001;
    // Right Justified, Vdd ref, channel adc, enable
    ADIE = 0;
    ADIF = 0; // Turns off AD interrupt and resets AD interrupt flag

}

unsigned int readADC()
{
    unsigned int ADCv;
    unsigned char ADCvh,ADCvl;
    GO = 1;
    while(!ADIF) continue;
    ADCvl = ADRESL;
    ADCvh = ADRESH;
    ADCvh = ADRESH & 0x03;
    ADCvh = ADCvh>>8;
    ADCv = ADCvl + ADCvh;
    return ADCv;
}





ANSEL = 0b10000000; // AN7 is analog input, all others are digital
ANSELH = 0b0111; // AN10,9,and 8 are all analog inputs

TRISB = 0b10110000;
TRISA = 0b00101000;
TRISC = 0b11001010;

	setupADC(7);
;




然后在while循环中,我只使用readADC()然后在LCD上显示.输入2.5 V直流电压时,ADC返回255,但是如果我的ADC为10位,则应该为512.

谢谢,
GabeA




Then in a while loop, I just use readADC() then display on LCD. When I input a dc voltage of 2.5 V, the ADC returns 255, but if my ADC is 10 bit, I should be getting 512.

Thanks,
GabeA

推荐答案

ADCvh = ADCvh>>8;
ADCv = ADCvl + ADCvh;
return ADCv;



将8位值右移8位是未定义的操作.

允许C编译器执行此操作.

最可能的实现是它导致0或根本不执行任何移位.

也许您的意思是:



Shifting an 8 bit value right by 8 is an undefined operation.

A C compiler is allowed to do anything it wants with that.

The most likely implementations are that it either results in 0 or in no shift being performed at all.

Perhaps what you meant was:

ADCv = (((unsigned int)(ADCvh & 0x03)) << 8) + ADCvl;
return ADCv;


这篇关于PIC16F690 ADC适用于一个通道,不适用于另一通道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 18:16