adc example

master
Gerardo Marx 3 months ago
commit b98520cf39

BIN
adc

Binary file not shown.

@ -0,0 +1,59 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
// Path to the raw adc's values:
#define ADC_PATH "/sys/bus/iio/devices/iio:device0/in_voltage%d_raw"
// Prototype Functions:
// 1 Function to read ADC value from a given channel (0 to 6)
int ReadADC(int channel);
// main:
int main( ) {
printf("Starting ADC program\n");
int channel = 0; // select the channel 0 - 6
int adcVal = ReadADC(channel);
if (adcVal >= 0) {
printf("ADC Channel %d raw value: %d\n", channel, adcVal);
} else {
printf("Failed to read ADC value\n");
}
return 0;
}
int ReadADC(int channel) {
char filePath[64];
char buffer[16];
int fd, adcValue;
// file path to adc
snprintf(filePath, sizeof(filePath), ADC_PATH, channel);
//printf(filePath);
// open the ADC file
fd = open(filePath, O_RDONLY);
if (fd < 0){
perror("Unable to open ADC Path");
return -1;
}
// read ADC value
if (read(fd, buffer, sizeof(buffer)) < 0) {
perror("Unable to read ADC value");
close(fd);
return -1;
}
// converting string to int
adcValue = atoi(buffer);
close(fd);
return adcValue;
}
Loading…
Cancel
Save