 ## Overview

Welcome to another informative post from Mevihub.com! This article will delve into battery voltage measurement using the TMS320F28069 microcontroller. We’ll walk you through the provided code step by step and explain the process of calculating battery voltage using a detailed formula. So, let’s get started!

## Introduction

A key component of many electronic systems, especially those that use batteries for power, is battery voltage measuring. We can precisely measure battery voltage thanks to the analog-to-digital converter (ADC) on the TMS320F28069 microcontroller. The included code provides a thorough formula for calculating the voltage and an example of how to interact with the ADC to measure battery voltage.

Battery Voltage Calculation Formula

The formula at the centre of the voltage measurement procedure is adjusted to the parameters provided:

Battery Voltage Calculation Formula with offset

Battery Voltage (in volts) = ((ADC_Result – Offset_Count) * Multiplication_factor) / (ADC_Resolution – Offset_Count).

Battery Voltage Calculation Formula without offset

## Practical Examples without offset

Here’s a breakdown of the formula components:

3. Maximum_Battery_Voltage: The maximum battery voltage (750 volts).

### Given Specifications

1. Battery voltage readings range from 0 to 750 volts.
2. The maximum ADC voltage for the TMS320F28069 microcontroller is 3.294 volts.
3. The ADC has a resolution of 12 bits, which means it can represent values from 0 to 4095.

There’s no offset voltage to consider in this setup.

### Formula

• Here, Multiplication_factor is 750 volts.

Let’s calculate the battery voltage for three different ADC results:

1. ADC_Result = 2048 (Mid-scale value)
2. ADC_Result = 1024 (One-fourth of full-scale value)
3. ADC_Result = 3072 (Three-fourths of full-scale value)

### Example 1: ADC_Result = 2048 (Mid-Scale)

``````Battery Voltage (in volts) = ((2048 - 0) * 750) / (4095 - 0)
= (2048 * 750) / 4095
≈ 375.61 volts``````

Consequently, the estimated battery voltage is 375.61 volts for an ADC_Result of 2048.

### Example 2: ADC_Result = 1024 (One-Fourth of Full-Scale)

``````Battery Voltage (in volts) = ((1024 - 0) * 750) / (4095 - 0)
= (1024 * 750) / 4095
≈ 187.81 volts``````

The estimated battery voltage for an ADC_Result of 1024 is 187.81 volts.

## Practical Examples of offset

### Formula with Offset

Battery Voltage (in volts) = ((ADC_Result – Offset_Count) * Multiplication_factor) / (ADC_Resolution – Offset_Count).

### Given Specifications

1. Battery voltage readings range from 0 to 750 volts.
2. The maximum ADC voltage for the TMS320F28069 microcontroller is 3.294 volts.
3. The ADC has a resolution of 12 bits, which means it can represent values from 0 to 4095.
4. An offset voltage of 0.2 volts is present.

Here, Multiplication_factor is 750 volts.

### Calculating the Offset_Count

We must translate the offset voltage (0.2 volts) into the appropriate ADC count in order to get the Offset_Count:

``````   Offset_Count        = (0.2 / 3.294) * 4095
Offset_Count        ≈ 248.76 (rounded to the nearest integer, which is 249)``````

As a result, the offset count is around 249.

### The formula for the Multiplication factor

Multiplication Factor = (Max_Voltage_Input * Reference_Voltage)/Actival_Voltage

``````Multiplication Factor = (750v*3.3v)/3.294v
Multiplication Factor = 2474/3.294
Multiplication Factor = 751.36v
``````

Example 1: ADC_Result = 2048 (Mid-Scale)

``````Battery Voltage (in volts) = ((2048 - 249) * 751) / (4095 - 249)
= (1799 * 751) / 3846
≈ 351.28 volts``````

For an ADC_Result of 2048 and an offset voltage of 0.2 volts, the estimated battery voltage is roughly 351.28 volts.

### Example 2: ADC_Result = 1024 (One-Fourth of Full-Scale)

``````Battery Voltage (in volts) = ((1024 - 249) * 751) / (4095 - 249)
= (775 * 751) / 3846
≈ 151.33 volts``````

## Final Code

```/*
*
*  Created on:
*      Author: Mevihub.com
*/

#include "DSP28x_Project.h"
#include <string.h>

#define Btry_Ip_Voltage 0
#define Output_Voltage  1
#define Output_Crnt     2

/* ========================================================
----------Battery Parameters Union Structure ----------
======================================================== */

union battery{

unsigned int uiData;
struct{
unsigned int uiBtry_Ip_Volt; // Battery Voltage 450 - 750V 2 Bytes
unsigned int uiOutPut_Volt;  // Output Voltage 24V
unsigned int uiOutPut_Crnt;  // Output Current 170A
unsigned int uiTermistor;    // HeatSink Temperature
}reg;
};

union battery Battery;

unsigned int  uiData;
unsigned char ucData;
struct{
unsigned int Space_Flag :15;
}reg;
};

union config{
struct{
unsigned int uiBtry_Ip_Volt_Mult;
unsigned int uiOutput_Volt_Mult;
unsigned int uiOutput_Crnt_Mult;

unsigned int uiBtry_Ip_Volt_Lmt;
unsigned int uiOutPut_Volt_Lmt;
unsigned int uiOutPut_Crnt_Lmt;
}reg;
};

union config Config;

// Input Voltage
unsigned int  uiIpVolt      = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
unsigned long ulIpVolt_Sum      = 0;

void main(void)
{
InitSysCtrl();
InitPieCtrl();
InitPieVectTable();
InitGpio();

Config.reg.uiBtry_Ip_Volt_Mult  = 750;
Config.reg.uiOutput_Crnt_Mult   = 170;

memset(&Battery, '\0', sizeof(Battery));

EALLOW;
EDIS;

PieCtrlRegs.PIEIER1.bit.INTx1 = 1;
IER |= M_INT1;

EINT;
ERTM;

/*
*
Scaling Factor
Formula
Scaling Factor  = Multiplication Factor / ADC_Resolution - OffsetVoltage
= 750 / 4095 - 0
= 750 / 4095
= 750 * [ 1/4095]
= 750 * 0.0002442;

*
*/

while(1)
{

}

}

{
{

uiIpVolt_ADC                = ulIpVolt_Sum >> 4; // Nothing but ulIpVolt_Sum /16

/*
*
Formula for calculate Battery voltage and current

*
*/

Battery.reg.uiBtry_Ip_Volt     =  (unsigned int)uiIpVolt_ADC * ADC_Scaling_Factor;     //MF * (1 / 4095) =   750 + 0.0002442 = 0.18315

}

}

{
EALLOW;

EDIS;

}

{
PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;                                 // Acknowledge interrupt to PIE

}

```