简体   繁体   中英

How to get the int value from an ADC (PIC16F877A)?

I've made a configuration file regarding the registers:

void ADC_Init()
{
ADCON1bits.ADCS2 = 0;
ADCON0bits.ADCS1 = 1;
ADCON0bits.ADCS0 = 0;
//selection of a channel
ADCON0bits.CHS0=0;
ADCON0bits.CHS1=0;
ADCON0bits.CHS2=0;
//result format selection
ADCON1bits.ADFM=0; //right justify
//port configuration
ADCON1bits.PCFG0=0;
ADCON1bits.PCFG1=0;
ADCON1bits.PCFG2=0;
ADCON1bits.PCFG3=0;
//set status sit
ADCON0bits.GO_DONE=1;
//switch on ADC
ADCON0bits.ADON=1;
}

Now how do I get the int reading coming from the input?

Wait for the ADC to be ready and then read the result.

while (ADCONbits.GO_DONE);     //wait until conversion is ready
result = (((uint16_t)(ADRESH) << 8)) | ADRESL;

But best thing you could do is to write a function eg int_16_t readADC(int16_t channel) where you:
- select the channel
- start the ADC
- wait until its ready
- then return the result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM