Last active
June 8, 2016 10:07
-
-
Save cidzoo/4fb56190bc703d6f2c581cc39b82c27c to your computer and use it in GitHub Desktop.
Poll the XADC dedicated analog input VP/VN to sample external signals
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "xparameters.h" | |
| #include "xadcps.h" | |
| #include "stdio.h" | |
| #include "sleep.h" | |
| /** | |
| * Return the fractional part of a float as an integer | |
| */ | |
| int fract(float num) | |
| { | |
| float tmp; | |
| tmp = num; | |
| if (num < 0) { | |
| tmp = -(num); | |
| } | |
| return( ((int)((tmp -(float)((int)tmp)) * (1000.0f)))); | |
| } | |
| int main(void) | |
| { | |
| int status; | |
| XAdcPs xadc; | |
| u32 raw_data; | |
| float voltage; | |
| printf("\r\nPolling XADC dedicated analog input VP/VN...\r\n"); | |
| XAdcPs_Config *cfg = XAdcPs_LookupConfig(XPAR_XADCPS_0_DEVICE_ID); | |
| if (cfg == NULL) | |
| goto error; | |
| status = XAdcPs_CfgInitialize(&xadc, cfg, cfg->BaseAddress); | |
| if (status != XST_SUCCESS) | |
| goto error; | |
| status = XAdcPs_SelfTest(&xadc); | |
| if (status != XST_SUCCESS) | |
| goto error; | |
| /* Configure XADC to "Single Channel Mode", continuous, channel VP/VN, 0-1V range */ | |
| XAdcPs_SetSequencerMode(&xadc, XADCPS_SEQ_MODE_SINGCHAN); | |
| status = XAdcPs_SetSingleChParams(&xadc,XADCPS_CH_VPVN,FALSE,FALSE,FALSE); | |
| if (status != XST_SUCCESS) | |
| goto error; | |
| /* read value from ADC every sec */ | |
| while (TRUE) { | |
| raw_data = XAdcPs_GetAdcData(&xadc, XADCPS_CH_VPVN); | |
| raw_data >>= 4; // 12-bits value is located at MSB, so shift to read it correctly | |
| voltage = (float)raw_data / (float)0xFFF; // compute voltage value between 0-1V | |
| xil_printf("VP/VN input = %d mV \t (raw=%d)\r\n", fract(voltage), raw_data); | |
| sleep(1); | |
| } | |
| return XST_SUCCESS; | |
| error: | |
| printf("Exiting with error code %d!\n", status); | |
| return XST_FAILURE; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment