#include <stdio.
h>
#include "xadcps.h" // Include the XADC driver header file
#include "xgpio.h"
#define XADC_DEVICE_ID     XPAR_XADCPS_0_DEVICE_ID
static XAdcPs XAdcInst;   // XADC driver instance
// Function to initialize the XADC
int XAdcConfig(u16 DeviceId)
{
    XAdcPs_Config *ConfigPtr;
    int Status;
    // Look up the configuration of the XADC
    ConfigPtr = XAdcPs_LookupConfig(DeviceId);
    if (ConfigPtr == NULL) {
        return XST_FAILURE;
    }
    // Initialize the XADC driver
    Status = XAdcPs_CfgInitialize(&XAdcInst, ConfigPtr, ConfigPtr->BaseAddress);
    if (Status != XST_SUCCESS) {
        return XST_FAILURE;
    }
    // Perform a self-test to ensure the XADC is working
    Status = XAdcPs_SelfTest(&XAdcInst);
    if (Status != XST_SUCCESS) {
        return XST_FAILURE;
    }
    // Set the sequencer to safe mode before making changes
    XAdcPs_SetSequencerMode(&XAdcInst, XADCPS_SEQ_MODE_SAFE);
    // Enable the VAUX1 channel for A0
    XAdcPs_SetSeqChEnables(&XAdcInst, XADCPS_SEQ_CH_AUX01);
    // Set the input mode for VAUX1 as unipolar (0-3.3V range)
    XAdcPs_SetSeqInputMode(&XAdcInst, 0);
    // Set averaging to 16 samples
    XAdcPs_SetAvg(&XAdcInst, XADCPS_AVG_16_SAMPLES);
    // Set the sequencer to continuous sampling mode
    XAdcPs_SetSequencerMode(&XAdcInst, XADCPS_SEQ_MODE_CONTINPASS);
    return XST_SUCCESS;
}
int main()
{
    int Status;
    u16 AdcDataVAUX1;
    float VoltageVAUX1;
    // Configure the XADC for VAUX1 (A0) readings
    Status = XAdcConfig(XADC_DEVICE_ID);
    if (Status != XST_SUCCESS) {
        printf("XADC configuration failed.\n");
        return XST_FAILURE;
    }
    // Continuously sample the VAUX1 channel (A0 pin)
    while(1) {
        // Reset the XADC before reading
        XAdcPs_Reset(&XAdcInst);
        // Read raw ADC data from VAUX1 (A0)
        AdcDataVAUX1 = XAdcPs_GetAdcData(&XAdcInst, XADCPS_CH_AUX_MIN + 1);   // 1
is VAUX1 for A0
        // Convert the raw ADC value to voltage
        VoltageVAUX1 = ((float)AdcDataVAUX1 * 3.3f) / 65536.0f;
        // Print the voltage reading
        printf("A0 (VAUX1) Voltage: %2.5f V\n", VoltageVAUX1);
        // Wait for a bit before the next sample (e.g., 1 second)
        sleep(1);
    }
    return 0;
}