picmplabmcc

Mikrocontroller (PIC16F1827) ADC scrambled output with MCC in MPLAB


Im trying to construct an AD-converter from a potentiometer to an Arduino. I´m trying to learn MCC in MPLAB at the same time. So far I have generated a code that fits my PIC (I think...). My problem is now that my bit represented output is incorrect. This is hoe my PIC16F1827 is configured (se picture) enter image description here

RA0 = input, RB1 and RB2 = EUSART and RB0,RB3,RA7,RA6,RB7,RB6,RB5,RB4 = output.

My main file look like this (se code). I get an output but its represented wrong and i can´t figure ut why...

char ADC_temp_in;
    while (1) //Infinite Loop
    {
        // Add your application code
        printf("pot_value =%d\r\n", ADC_GetConversion(channel_AN0_ADC));
        ADC_temp_in = ADC_GetConversion(channel_AN0_ADC);  // temp
        PORTB = ADC_temp_in;                  //Write Lower bits to PORTB
        PORTA = ADC_temp_in>>6;               //Write Higher 2 bits to PORTA
        __delay_ms(100);            //Delay     
    }

VREF+ = 5V and is connected directly to VDD.

My goal is to have RB0 as LSB and RA7 as MSB with the voltage difference 0-5 V with the potentiometer.


Solution

  • Two things:
    ADC_temp_in had to by a 16 Bit value to hold a value greater than 8 Bit.
    So try: uint16_t ADC_temp_in;
    Of course your function ADC_GetConversion had to return a uint_16 value.

    Another thing is, to get the MSB you had to shift your value 8 times right.

    PORTA = ADC_temp_in>>8;