Getting Current Voltage Input (VIN) on TS-7670 or TS-7400-V2

Here’s an example program our engineers might find useful. Kris Bahnsen, a long time engineer for Technologic Systems, wrote this simple program to get the voltage input (Vin) on the 8 – 28 VDC power rail on the TS-7670(Rev. D or later) or TS-7400-V2 (Rev. B or later). Without going into too much detail about implementation of the on-board supervisory microcontroller, there is a register which is used to store various ADC values, including Vin. This example program basically polls this 4byte register via I2C interface, accounts for the voltage divider (see TS-7670 schematic or TS-7400-V2 schematic), and outputs the Vin value.       Home

So, without further ado, here’s the code:

get_vin.c

/* 
 * To compile get_vin, use the appropriate cross compiler and run the
 * command:
 *
 *  gcc -fno-tree-cselim -Wall -O0 -mcpu=arm9 -o get_vin get_vin.c
 */

/* This software is intended to run on the TS-7670 Rev. D or later, or
 * the TS-7400-V2 Rev. B or later.
 *
 * The math below is done with all integers, the values are multiplied up
 * and then divided down to get the best resolution.  This software works
 * for the full VIN range read through the on-board supervisory microcontroller.
 */


#include <assert.h>
#include <unistd.h>
#include <fcntl.h>
#include <linux/i2c-dev.h>
#include <stdio.h>
#include <strings.h>
 
int main(void)
{
    unsigned char data[4];
    unsigned int vin;
    int fd;
 
    fd = open("/dev/i2c-0", O_RDWR);
    assert (fd != -1);
    
    if (ioctl(fd, I2C_SLAVE_FORCE, 0x78) < 0) {
        perror("MCU did not ACK 0x78\n");
        return -1;
    }
 
    bzero(data, 4);
    read(fd, data, 4);
    vin = (data[3]<<8)|data[2];
    vin = (((vin*24438)*172)/100000); /* 5.82% div., 2.5 Vref, 10b acc. */
    printf("vin_mv=%d\n", vin);

    return 0;
}

Compiling and Running

Once you’ve copied the source code over to the board, you can use the preloaded gcc command to compile like this:

root@ts7670-496b64:~# gcc get_vin.c -o get_vin

Then, of course, run it! We have a 12 VDC power adapter plugged into the 8 – 28 VDC input, so we expect to see 12V reported as Vin (represented in mV):

root@ts7670-496b64:~# ./get_vin
vin_mv=12028

Again, we hope you’ve found this example program to get Vin on the TS-7670 or TS-7400-V2 helpful. Let us know if you have any suggestions to make it better.                     Home

Leave a Reply

Your email address will not be published. Required fields are marked *