[PIC] ADC value not updating without resetting device

Status
Not open for further replies.

dtusllitg

Newbie
Joined
Sep 29, 2020
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
29
Hi everyone,

I'm trying to read and convert analog values of a 10K-potentiometer into voltages (0V to 3V3) with PIC16F18877. To see the result, an LED toggling method is used to detect if the voltage is greater or less than 1.65V. I use MPLAB v5.5, XC8 to generate code with its MCC library, shown below. Please see a few beginner questions after code.

main.c

C:
#include "mcc_generated_files/mcc.h"

void display_volt(float);

/*
                         Main application
*/
void main(void)
{
    adc_result_t convResult = 0;
    float v = 0;
 
    // initialize the device
    SYSTEM_Initialize();
    ADCC_StartConversion(POT);
 
    while (1)
    {
       
        while(!ADCC_IsConversionDone()); // check the ADC conversion
        convResult = ADCC_GetConversionResult(); // get ADC value
        v = (convResult * 3.3)/1023; // get voltage value
     
      
        display_volt(v); // send the value to display
    }
}


void display_volt(float v){
    while (v >1.65){
        LED_SetHigh();
    }
    LED_SetLow();
}
/**
End of File
*/

Questions:
1) Why isn't the converted voltage value updated unless the MCU being reset?
2) Why does an ADC implementation in PIC need a timer? I see some tutorials using a timer and some don't use it. I try both and get the same result, the voltage value never gets updated unless the MCU is reset.
3) In the MCC window, there is an option to enable ADC interrupt, is it necessary to enable it?

Thanks -- Dan
 

Code:
void display_volt(float v){
    while (v >1.65){
        LED_SetHigh();
    }
    LED_SetLow();
}

When you jump to this loop and v > 1.65 it never exits this loop because you
do not read any more conversions .....


Regards, Dana.
 

void display_volt(float v){
while (v >1.65){
LED_SetHigh();
}
LED_SetLow();
}

When you jump to this loop and v > 1.65 it never exits this loop because you
do not read any more conversions .....

Thank you for looking into my code.
I tried changing this function's condition to a simple if-else loop as below

C:
void display_volt(float v) {
    if (v > 1.65) {
        LED_SetHigh();
    } else {
        LED_SetLow();
    }
}

and still experience the same problem.
 

Not a PIC expert, look into an example project they must have, I would bet
there is interrupt dependency, one that has to be declared and started and
restarted. If there are any variables used in interrupt, that are declared external
to it, must be declared volatile.





Regards, Dana.
 
I would bet
there is interrupt dependency, one that has to be declared and started and
restarted. If there are any variables used in interrupt, that are declared external
to it, must be declared volatile.

I did try volatile qualifier but that did not help. I will have to read more on the interrupt.
Thanks -dan
 

You only read the ADC once so it isn't surprising it doesn't update!

Move the line: "ADCC_StartConversion(POT);" inside the while loop and before you check the conversion is completed.

Brian.
 
The code mod in your #3 post should work.

I checked on interrupts, they do not seem to need any involvement, at least that I could find.

In your main while{} loop do you have to keep restating that A/D inside while{} ?
Check on that. In other words does it do a single shot conversion and you have to
restart it or is it doing a continuous conversion ?

From datasheet -




Regards, Dana.
 
Reactions: dtusllitg

    dtusllitg

    Points: 2
    The answer not only helps the problem specific but also entails knowledge to understand why it should be solved that way and with rich information resource of further self-learning. Thank you.
You only read the ADC once so it isn't surprising it doesn't update!

Move the line: "ADCC_StartConversion(POT);" inside the while loop and before you check the conversion is completed.

Brian.
Thank you. That works.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…