I'm making steady progress on this drizzly day. I have my first C++ linux program running that samples the an ADC pin on the SAMA4D4 Xplained Ultra Board.
In the mid 90's, I was a C++ software developer. Around that same time, the standard template library (STL) came into fashion. It's great to use it once again. |
Getting the ADC values
Once again, this is inspired by Robert Peter Oakes's work on this board, SAMA5D4 Xplained Ultra - Tips and Tricks #2 - Using the built in IO and external devices.
I'm using a very similar setup as Peter, but instead of using the Linux command line to read the sample results, I've written a C++ program.
But we're both triggering a sample and reading results by accessing /sys/bus/iio/devices/iio:device0/in_voltage0_raw.
The schematic is very simple: a potentiometer with both ends connected to ground and 3.3V of the Xplained board. The wiper goes to A1.
To make things funny, A1 is analog port 0 in the linux mapping. (I don't know yet what A0 is, but it has a digital signal on it when I sample it with my scope.)
The program is cross-compiled using the GNU toolchain on a windows machine, loaded to the linux root user's home folder, and executed.
It shows a sample result in the terminal window every second.
I haven't used any of the Atmel libraries. All is done via standard file access.
#include <iostream>
#include <fstream>
using namespace std;
const char fileName[] = "/sys/bus/iio/devices/iio:device0/in_voltage0_raw";
int main() {
string line;
while(1) {
ifstream myfile(fileName, ios::in);
if (myfile.is_open()) {
while (getline (myfile,line)) {
cout << line << '\n';
}
myfile.close();
} else {
cout << "Unable to open file " << fileName << "\n";
}
}
return 0;
}
That's the whole program. After compilation, linking, uploading and setting the executable flag, here's what it looks like in the terminal:
The value changes when moving the potentiometer wiper.


Top Comments
-
michaelkellett
-
Cancel
-
Vote Up
0
Vote Down
-
-
Sign in to reply
-
More
-
Cancel
-
Jan Cumps
in reply to michaelkellett
-
Cancel
-
Vote Up
0
Vote Down
-
-
Sign in to reply
-
More
-
Cancel
-
michaelkellett
in reply to Jan Cumps
-
Cancel
-
Vote Up
0
Vote Down
-
-
Sign in to reply
-
More
-
Cancel
Comment-
michaelkellett
in reply to Jan Cumps
-
Cancel
-
Vote Up
0
Vote Down
-
-
Sign in to reply
-
More
-
Cancel
Children