weirdgyn
weirdgyn

Reputation: 988

PIC16F1618 returning ADC values exceeding 10bits

I'm writing my first PIC16F1618 project. I got stucked in a pretty strange error/behaviour. This's the repository of the project:

Github repo

The problem is the value resulting from the ADC sampling:

    adc_result_t getADCValue(adc_channel_t channel) {
        adc_result_t sum = 0;
        adc_result_t sample;
        
        for (uint8_t i = 0; i < 32; i++ )
        {
            sample = ADC_GetConversion(channel); 
            sum += sample;
        }
        
        return sum >> 5; // Divide per 32 tramite shift
    }

The MCC generated function ADC_GetConversion() should start, wait for ADC input being stable, wait for the conversion to be completed and return the 10bit value conversion from ADC. The result is packed in a adc_result_t value right aligned. I'm feeding the input with 1.5V ... I'm expecting a value around 0x133 but instead I got values about 0x4d00 that's well over 10bits.

NB I'm using MCC generated code where possibile.

MPLab debug shot

---- UPDATE ----

This's the screenshot of the ADC configuration:

ADC MCC configuration

But this's the code ADC_Initialize() generated code:

void ADC_Initialize(void)
{
    //ADPREF VDD; ADCS FOSC/64; ADFM left; 
    ADCON1 = 0x60;
...

left???? I'm a little bit disappointed... right align should mean 7th bit high... then +0x80?

Upvotes: 0

Views: 59

Answers (0)

Related Questions