Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
17 views2 pages

Message

The document contains C code for configuring and using the XADC (Xilinx Analog-to-Digital Converter) to read voltage from the VAUX1 channel (A0). It initializes the XADC, sets it to continuous sampling mode, and continuously reads and prints the voltage value. The code includes error handling for configuration failures and performs a self-test to ensure proper operation of the XADC.

Uploaded by

koulachannel05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views2 pages

Message

The document contains C code for configuring and using the XADC (Xilinx Analog-to-Digital Converter) to read voltage from the VAUX1 channel (A0). It initializes the XADC, sets it to continuous sampling mode, and continuously reads and prints the voltage value. The code includes error handling for configuration failures and performs a self-test to ensure proper operation of the XADC.

Uploaded by

koulachannel05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#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;
}

You might also like