commit b98520cf39cc8c6e3493152cd925f02ca8897499 Author: Gerardo Marx Date: Thu Sep 26 03:53:53 2024 +0000 adc example diff --git a/adc b/adc new file mode 100755 index 0000000..0ba2b59 Binary files /dev/null and b/adc differ diff --git a/main.c b/main.c new file mode 100644 index 0000000..9db94d4 --- /dev/null +++ b/main.c @@ -0,0 +1,59 @@ +#include +#include +#include +#include + +// 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; +} \ No newline at end of file