C Language Development Kit/Functions/BattLevel

From OptiWiki

Jump to: navigation, search
Description Returns an estimate of the remaining charge left in the battery pack.
unsigned int battlevel(void);
Arguments None
Returns The remaining charge level of the battery pack as an percentage of the estimated battery capacity: an unsigned integer value between 0 and 100.
A value of 0 corresponds to 0 % of the usable capacity of the battery pack.
A value of 100 corresponds to 100 % of the usable capacity of the battery pack.
Remarks The DWT7133 monitors the current flow into and out of the battery pack.

Together with the battery capacity the DWT7133 is then able to calculate the remaining charge level.

The battery capacity (amount of charge the battery pack can contain) is constantly monitored by the DWT7133: When the battery pack is completely charged, the DWT7133 knows that it is full. When the battery pack has run empty, the DWT7133 knows that there is no more charge left. By constantly monitoring the current flow between these two situations, the DWT7133 can calculate the actual battery capacity. The DWT7133 will update the estimated value of the battery capacity constantly, based on the most actual measurements. When the DWT7133 does not have sufficient information, it will assume a reasonable default value for the battery capacity. The accuracy of battlevel() is not guaranteed, unless a complete charge/discharge cycle has taken place.

Initially the charge level of the battery pack is not known. When the DWT7133 is started for the first time or after a battery change, the DWT7133 assumes that the battery is empty. The battery pack may actually contain more charge. The maximum charge level is assumed to be the zero level plus the estimated battery capacity. When the battery pack reaches the minimum level, but keeps being discharged, the DWT7133 will update the value of the minimum level. battlevel() will then return zero, and keeps doing that, although the DWT7133 knows that the charge level keeps decreasing. When the DWT7133 is placed on the cradle, and the pack is charged, the level starts to increase starting from zero.

The maximum level is always calculated as the estimated zero level plus the estimated battery capacity. When the maximum level has been reached, but the battery pack getting charged, the DWT7133 will update its estimated value of the battery capacity. battlevel() will then be returning 100 %.


The accuracy of this battlevel() is only guaranteed after a completed charge/discharge cycle. A complete charge may take upto 14 hours. Therefore it is advised to keep the DWT7133 on the cradle when it is used for the first time, or after a cold start.

// Displays the remaining battery capacity 
#include <stdio.h>
#include "lib.h"
void main( void )
    int oldlevel, newlevel;
    printf("\fBattery test");
    oldlevel = 200;
    while( 1 )
        newlevel = battlevel();
        if( newlevel != oldlevel )
            printf("\nBat: %i%%", newlevel);
            oldlevel = newlevel;

Personal tools