Support Forum

Share your projects and post your questions

Register   or   Sign In
The Forum

Has anyone written C code for the R-Pi to convert 16 bit serial numbers from the ADC into parallel numbers and save these to the Pi’S memory?

1632 Views - Created 21/07/2018

21/07/2018

Posted by:
WayneA

WayneA Avatar

Does anyone have C code for the R-Pi B+ to convert 16/17 bit ADC serial numerical data into parallel data and store a series of these numbers in the R-Pi’s internal memory. Any code listing or suggestions would be greatly appreciated. Many thanks!

Wayne

21/07/2018

Posted by:
andrew

andrew Avatar

Location:
United Kingdom

andrew Twitter  andrew Website  

Hello Wayne

The usual way to store a series of numbers in C is using an array. You can find a good tutorial on arrays at https://www.tutorialspoint.com/cprogramming/c_arrays.htm

You can use our ADC Pi C library to read the values from the ADC Pi using the read_voltage() function.

The code example below creates an array with a size of 1000, reads the ADC 1000 times and then prints the recorded values back to the console.



#include <stdio.h>
#include <stdint.h>
#include <unistd.h>

#include "ABE_ADCPi.h"

int main(int argc, char **argv){
    setvbuf (stdout, NULL, _IONBF, 0); // needed to print to the command line

    double values[1000];

    // save the values from the ADC into the array
    for (int i=0; i < 1000; i++){
        values[i] = read_voltage(0x68,1, 16, 1, 1); // ADC on I2C address 0x68, channel = 1, 16 bit, pga = 1, conversion mode = continuous
        usleep(200000); // sleep 0.2 seconds
    }

    // print them back to the console
    for (int i=0; i < 1000; i++){
        printf("Value %i: %G \n", i, values[i]);
    }

	return (0);
}


Save the code as "demo-adc-array.c" and you can compile it with the following command:



gcc ABE_ADCPi.c demo-adc-array.c -o demo-adc-array


The ADC Pi library files "ABE_ADCPi.h" and ABE_ADCPi.c" will need to be in the same folder as the demo-adc-array.c file.

Sign in to post your reply

Note: documents in Portable Document Format (PDF) require Adobe Acrobat Reader 5.0 or higher to view.
Download Adobe Acrobat Reader or other PDF reading software for your computer or mobile device.