The issue with the code could be related to the ADC conversion process. The function `ADC_Read_Potentiometer()` and `ADC_Read_Temperature()` are used to read the values from the potentiometer and temperature sensor, respectively. However, we need to ensure that the ADC module is properly configured and enabled before attempting to read values from it.
To fix the issue, we can add the necessary steps to initialize and enable the ADC module before using it to read values. We can modify the `ADC_Initialize()` function to configure and enable the ADC module. Additionally, we can call this function in the `SYSTEM_Initialize()` function to ensure that the ADC module is initialized before using it.
Here is the updated code:
```c
void ADC_Initialize(void) {
AD1PCFGbits.PCFG4 = 0; // Set AN4 as analog input
AD1CON1bits.FORM = 0b00; // Output format: Integer
AD1CON1bits.SSRC = 0b010; // Sample clock source: Timer3
AD1CON1bits.ASAM = 1; // Auto-sampling
AD1CON2bits.VCFG = 0b000; // Reference voltage: AVDD and AVSS
AD1CON2bits.CSCNA = 0; // Disable scanning
AD1CON2bits.SMPI = 0; // Interrupts at the completion of conversion for each sample/convert sequence
AD1CON2bits.BUFM = 0; // Single buffer mode
AD1CON3bits.ADRC = 0; // Clock derived from system clock
AD1CON3bits.SAMC = 0b11111; // Auto-sample time
AD1CON3bits.ADCS = 0b00000001; // Conversion clock select: Tcy
AD1CHSbits.CH0NA = 0; // Negative input for Channel 0: Vref-
AD1CHSbits.CH0SA = 0b00011; // Positive input for Channel 0: AN4
AD1CON1bits.ADON = 1; // Enable ADC module
}
void SYSTEM_Initialize(void) {
PIN_MANAGER_Initialize();
OSCILLATOR_Initialize();
INTERRUPT_Initialize();
ADC_Initialize();
UART2_Initialize();
}
```
Additionally, make sure that the potentiometer and temperature sensor are connected to the correct analog input pins (AN3 and AN4) on the microcontroller. Double-check the connections to ensure they are correctly wired.
With these modifications, the ADC module should be properly initialized and enabled before reading values from the potentiometer and temperature sensor.