BAT12-7670 example: Difference between revisions

From embeddedTS Manuals
(Created page -- it's literally all code.)
 
m (Links auto-updated for 2022 re-branding ( https://files.embeddedarm.com/ts-silo/ts-bat12/samples/ →‎ https://files.embeddedTS.com/ts-silo/ts-bat12/samples/))
 
(10 intermediate revisions by 2 users not shown)
Line 1: Line 1:
This source can be found on the Technologic Systems FTP [https://files.embeddedTS.com/ts-silo/ts-bat12/samples/ here].
<source lang=cpp>
<source lang=cpp>
// TS-7670 BAT12 Monitor
// TS-7670 BAT12 Monitor
Line 14: Line 16:
#include <stdlib.h>
#include <stdlib.h>
#include <strings.h>
#include <strings.h>
#include <iostream>
#include <linux/i2c-dev.h>
#include "i2c-dev.h"


#define CONVERSION_DIVISOR 24.7
#define CONVERSION_DIVISOR 24.7
#define DATASET_SIZE 28
#define DATASET_SIZE 28
using namespace std;


const char copyright[] = "Copyright (c) Technologic Systems - " __DATE__ ;
const char copyright[] = "Copyright (c) Technologic Systems - " __DATE__ ;
/// Convert raw ADC value into voltage level.
/// \arg rawValue The raw ADC value to be converted.
float vConvert(int rawValue);
/// Read I2C bus, grab ADC value for vin.
/// \arg twifd File descriptor for i2c bus.
int get_vin_adc(int twifd);
/// Open the file descriptor for i2c.
int i2cInit(void);
/// Program Main Entry Point.
int main(void)
{
int raw, fd;
float voltage;
fd = i2cInit();
        if(fd == -1) {
          cout << "VIN=ERROR\n I2C=FAIL";
          return 1; // quit on error.
        }
raw = get_vin_adc(fd);
voltage = vConvert(raw);
cout << "VIN=" << voltage << endl;
return 0;
}


/// Divide raw by conversion value.
/// Divide raw by conversion value.
/// \arg rawValue The ADC value to convert to volts.
float vConvert(int rawValue){
float vConvert(int rawValue){
float theConversion;
float theConversion;
Line 63: Line 35:
/// Gets ADC value from i2c.
/// Gets ADC value from i2c.
/// \arg twifd file descriptor for i2c.
/// \arg twifd file descriptor for i2c.
/// \out adcValue raw ADC data.  Byte 3 is MSB, byte 2 is LSB.
int getVinAdc(int twifd)
int get_vin_adc(int twifd)
{
{
uint8_t data[DATASET_SIZE];
uint8_t data[DATASET_SIZE];
Line 70: Line 41:
int adcValue = 0;
int adcValue = 0;


read(twifd, data, DATASET_SIZE); // most of this data is garbage
read(twifd, data, DATASET_SIZE); // most of this data is reserved
  // but the microcontroller will
  // but the microcontroller will
  // only dump all or none so...
  // only dump all or none.
adcValue = data[3]<<8|data[2];
adcValue = data[3]<<8|data[2]; // Byte 3 is MSB, byte 2 is LSB.
return adcValue;
return adcValue;
}
}
Line 80: Line 51:
int i2cInit()
int i2cInit()
{
{
static int fd = -1;
int fd = -1;
fd = open("/dev/i2c-0", O_RDWR);
fd = open("/dev/i2c-0", O_RDWR);
if(fd != -1) {
if(fd != -1) {
if (ioctl(fd, I2C_SLAVE_FORCE, 0x78) < 0) {
if (ioctl(fd, I2C_SLAVE_FORCE, 0x78) < 0) {
perror("Hardware did not ACK 0x78\n");
return -2;
return -1;
}
}
}
}
return fd;
return fd;
}
}
/// Main Entry Point.
//  program quits with -1 on file io error, -2 on i2c error.
int main(void)
{
int raw, fd;
float voltage;
fd = i2cInit();
        if(fd == -1 || fd == -2) {
          printf("VIN=%d\n", fd);
          return fd; // quit on error.
        }
raw = getVinAdc(fd);
voltage = vConvert(raw);
        printf("VIN=%f\n", voltage);
return 0;
}
</source>
</source>

Latest revision as of 16:29, 17 January 2022

This source can be found on the Technologic Systems FTP here.

// TS-7670 BAT12 Monitor
// Written by Michael D. Peters
// C. 2016 Technologic Systems, Inc.

// Read ADC at I2C address 0x78, parse bytes 3 (adc high byte)
//  and 2 (adc low byte), divide by magic 24.7 and presto input
//  voltage.
// Output VIN=<input voltage> so a script can parse and use.

#include <stdio.h>
#include <stdint.h>
#include <fcntl.h>
#include <stdlib.h>
#include <strings.h>
#include <linux/i2c-dev.h>

#define CONVERSION_DIVISOR 24.7
#define DATASET_SIZE 28

const char copyright[] = "Copyright (c) Technologic Systems - " __DATE__ ;

/// Divide raw by conversion value.
/// \arg rawValue The ADC value to convert to volts.
float vConvert(int rawValue){
	float theConversion;

	theConversion = (float)rawValue / CONVERSION_DIVISOR;

	return theConversion;
}

/// Gets ADC value from i2c.
/// \arg twifd file descriptor for i2c.
int getVinAdc(int twifd)
{
	uint8_t data[DATASET_SIZE];
	bzero(data, DATASET_SIZE);
	int adcValue = 0;

	read(twifd, data, DATASET_SIZE); // most of this data is reserved
					   // but the microcontroller will
					   // only dump all or none.
	adcValue = data[3]<<8|data[2];  // Byte 3 is MSB, byte 2 is LSB.
	return adcValue;
}

/// Opens I2C to 0x78.  Returns file descrptor if OK else quits.
int i2cInit()
{
	int fd = -1;
	fd = open("/dev/i2c-0", O_RDWR);
	if(fd != -1) {
		if (ioctl(fd, I2C_SLAVE_FORCE, 0x78) < 0) {
			return -2;
		}
	}
	return fd;
}

/// Main Entry Point.
//  program quits with -1 on file io error, -2 on i2c error.
int main(void)
{
	int raw, fd;
	float voltage;

	fd = i2cInit();
        if(fd == -1 || fd == -2) {
          printf("VIN=%d\n", fd);
          return fd; // quit on error.
        }
	raw = getVinAdc(fd);
	voltage = vConvert(raw);
        printf("VIN=%f\n", voltage);
	return 0;
}