Example C Code for Reading ADC Inputs on i.MX28 Based Embedded Systems

Let’s take a quick look at what it takes to read from the ADC inputs of a i.MX28 based embedded system using example C code provided by Technologic Systems. Now, while this can be generically applied to many i.MX28 based embedded systems, we’ll be working with a TS-7680. Right, let’s get started!

Hardware Hookups

Before we dive into software, let’s take a look at our hardware. On our TS-7680, the four ADC inputs are located on the bottom screw terminal connector. Referring to the TS-7680 schematic, we’ll find these pins are connected to the CPU ADC pins with some glue logic in between, enabling 4 – 20 mA and 0 – 10 VDC inputs. For our example code testing purposes, we’ll be working with 0 – 5 VDC input. Take a look at the wiring diagram below for how we’re going to hook everything up. We’re going to use the 5 VDC pin of the HD1 header. You’ll see we’re using a trimpot located on a breadboard to vary our input voltage from 0 – 5 VDC on the pin labeled AN-1. For AN-0, we’re going to use constant 5 VDC. Of course, we’ll need to connect the pin labeled GND to, well, ground (also provided by the HD1 header). That should do it!

Jumping into Code

Now, the fun part. I’m going to assume you already know how to console or SSH into the board you’re working on since we’re going to be compiling everything onboard. If you’re on a TS-7680, and you don’t have either of these things, you can refer to Practical Guide to Getting Started with the TS-7680. Right, so let’s start by finding a spot for our example code and downloading it onto our board. I’m going to call it getadcsince it’s easier to remember and type than mx28adcctl.

Okay, with that out of the way, let’s take a look at what we’re about to compile. You can use vim, nano, cat, less, or whatever to view the file getadc.c. You’ll notice there’s a nicely commented section about the program, what it does to obtain the voltage input values, and the different calculations for different platforms. You’ll also notice the output will be in mV, in order to avoid any floating point arithmetic. For the sake of convenience, here it is:

Directly following this comment block is the code that loops through all seven ADC inputs of the i.MX28 CPU, calculates the voltage input values based on what was said in the comments, and spits out their values to the console.

If we were to look at the rest of the code, it probably feels overwhelming. There’s a lot of code involved with setting up registers and whatnot. Let’s not get too caught up in this as we might lose sight of the end goal. Just know it’s necessary to set ourselves up for reading ADC input values of the i.MX28. If you’re really curious, you could find this information available in the i.MX28 datasheet. For now, it’s enough to see that for our TS-7680, we need to use the formula already calculated for us by Technologic Systems’ engineering:

So, we’ll modify the printf statement from within the for loop to read:

Save the file and let’s try to compile. As written in the comment header, to compile, we use:

Pro Tip: The gcc arg -fno-tree-cselim is required to avoid a nasty bug when using the volatiletype.

Hooray! If all went well, you should now have a new getadcbinary to run. With our hardware all setup, and our trimpot turned down slightly from 5 VDC, you should get output similar to this:

You can validate using a multimeter and you’ll find it to be accurate. At this point, you’re ready to move onto modifying the example code to suit your particular needs. For example, maybe you want to copy getadcinto your PATH and use it to obtain a single ADC pin value. Oddly specific, right? That’s because I used it this way in another project. You can find the Makefile and getadc.c files all right here in GIT so you can get a head start.

If I’ve left anything out or you’re still yearning for knowledge, please leave a comment below or contact engineering directly using the contact form in the sidebar.

Happy coding!

Author: Derek Hildreth

eBusiness Manager for Technologic Systems