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?
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.
The proper use of C's volatile keyword is poorly understood by many programmers. This is not surprising, as most C texts dismiss it in a sentence or two. This article will teach you the proper way to do it.
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
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 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 ?