⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 batterymonitor.c

📁 jennic jn5139模块 电池电量监控程序代码
💻 C
字号:
/****************************************************************************
 *
 * MODULE:             Battery Level Monitor
 *
 * COMPONENT:          $RCSfile: BatteryMonitor.c,v $
 *
 * VERSION:            $Name: JN-AN-1012-1v0 $
 *
 * REVISION:           $Revision: 1.2 $
 *
 * DATED:              $Date: 2006/11/13 15:05:18 $
 *
 * STATUS:             $State: Exp $
 *
 * AUTHOR:             Ian Morris
 *
 * DESCRIPTION:        Supply voltage (battery) monitoring application. Reads
 *                     value of supply voltage using ADC whenever the tick timer
 *                     interrupt occurs. Value read from ADC is converted to mV
 *                     and transmitted via UART0. *
 * LAST MODIFIED BY:   $Author: imorr $
 *                     $Modtime: $
 *
 ****************************************************************************
 *
 * This software is owned by Jennic and/or its supplier and is protected
 * under applicable copyright laws. All rights are reserved. We grant You,
 * and any third parties, a license to use this software solely and
 * exclusively on Jennic products. You, and any third parties must reproduce
 * the copyright and warranty notice and any other legend of ownership on each
 * copy or partial copy of the software.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS". JENNIC MAKES NO WARRANTIES, WHETHER
 * EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE,
 * ACCURACY OR LACK OF NEGLIGENCE. JENNIC SHALL NOT, IN ANY CIRCUMSTANCES,
 * BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, SPECIAL,
 * INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY REASON WHATSOEVER.
 *
 * Copyright Jennic Ltd 2005, 2006. All rights reserved
 *
 ****************************************************************************/

/****************************************************************************/
/***        Include files                                                 ***/
/****************************************************************************/
#include <jendefs.h>
#include <AppApi.h>
#include <AppHardwareApi.h>
#include <Printf.h>

/****************************************************************************/
/***        Macro Definitions                                             ***/
/****************************************************************************/
#define REPORTING_PERIOD_ms         1000 /* ADC read period */
#define REPORTING_PERIOD_COUNT      (16000UL * REPORTING_PERIOD_ms )

/* Define macros to control LED1 on evaluation kit hardware */
#define LED_1_MASK                  0x00004000UL
#define LED_INIT                    (vAHI_DioSetDirection(0, LED_1_MASK))
#define LED_1_OFF                   (vAHI_DioSetOutput(LED_1_MASK, 0))
#define LED_1_ON                    (vAHI_DioSetOutput(0, LED_1_MASK))

/****************************************************************************/
/***        Type Definitions                                              ***/
/****************************************************************************/

/****************************************************************************/
/***        Local Function Prototypes                                     ***/
/****************************************************************************/
PRIVATE void vInitSystem(void);
PRIVATE void vTickTimerISR(uint32 u32Device, uint32 u32ItemBitmap);

/****************************************************************************/
/***        Exported Variables                                            ***/
/****************************************************************************/

/****************************************************************************/
/***        Local Variables                                               ***/
/****************************************************************************/

/****************************************************************************/
/***        Exported Functions                                            ***/
/****************************************************************************/
/****************************************************************************
 *
 * NAME: AppColdStart
 *
 * DESCRIPTION:
 * Entry point for a power on reset or wake from sleep mode.
 *
 * PARAMETERS:      Name            RW  Usage
 * None.
 *
 * RETURNS:
 * None.
 *
 * NOTES:
 * None.
 ****************************************************************************/
PUBLIC void AppColdStart(void)
{
    /* General initialisation */
    vInitSystem();

    while (1);
}

/****************************************************************************
 *
 * NAME: AppWarmStart
 *
 * DESCRIPTION:
 * Entry point for a wake from sleep mode with the memory contents held. We
 * are not using this mode and so should never get here.
 *
 * PARAMETERS:      Name            RW  Usage
 * None.
 *
 * RETURNS:
 * None.
 *
 * NOTES:
 * None.
 ****************************************************************************/
PUBLIC void AppWarmStart(void)
{
    AppColdStart();
}

/****************************************************************************/
/***        Local Functions                                               ***/
/****************************************************************************/
/****************************************************************************
 *
 * NAME: vInitSystem
 *
 * DESCRIPTION:
 * General system initialisation.
 *
 * PARAMETERS:      Name            RW  Usage
 * None.
 *
 * RETURNS:
 * None.
 *
 * NOTES:
 * None.
 ****************************************************************************/
PRIVATE void vInitSystem(void)
{
    /* Initialise hardware interface */
    (void)u32AppApiInit(NULL,NULL,NULL,NULL,NULL,NULL,NULL);
    (void)u32AHI_Init();

    /* Set DIO to drive LED1 on eval kit hardware */
    LED_INIT;
    LED_1_OFF;

    /* Gneral ADC initialisation */
    vAHI_ApConfigure(E_AHI_AP_REGULATOR_ENABLE,
                     E_AHI_AP_INT_DISABLE,
                     E_AHI_AP_SAMPLE_2,
                     E_AHI_AP_CLOCKDIV_500KHZ,
                     E_AHI_AP_INTREF);

    /* Wait for ADC to power up */
    while (!bAHI_APRegulatorEnabled());

    /* Enable ADC for battery voltage measurement */
    vAHI_AdcEnable(E_AHI_ADC_CONVERT_DISABLE,
                   E_AHI_AP_INPUT_RANGE_2,
                   E_AHI_ADC_SRC_VOLT);

    /* Initialise printf to use UART0 for reporting results of battery
       measurement */
    vUART_printInit();

    /* Start ADC sampling so that result will be ready the first time the
       tick timer interrupt occurs */
    vAHI_AdcStartSample();

    /* Set tick timer to give a periodic interrupt */
    vAHI_TickTimerConfigure(E_AHI_TICK_TIMER_DISABLE);
    vAHI_TickTimerWrite(0);
    vAHI_TickTimerInit(vTickTimerISR);
    vAHI_TickTimerInterval(REPORTING_PERIOD_COUNT);
    vAHI_TickTimerConfigure(E_AHI_TICK_TIMER_RESTART);
    vAHI_TickTimerIntEnable(TRUE);
}

/****************************************************************************
 *
 * NAME: vTickTimerISR
 *
 * DESCRIPTION:
 * Tick Timer Interrupt Service Routine. Read ADC and transmit result via UART0.
 *
 * PARAMETERS:      Name            RW  Usage
 * None.
 *
 * RETURNS:
 * None.
 *
 * NOTES:
 * None.
 ****************************************************************************/
PRIVATE void vTickTimerISR(uint32 u32Device, uint32 u32ItemBitmap)
{
    static bool_t bToggle;
    uint16 u16AdcReading;
    uint16 u16BattLevelmV;

    /* Toggle LED to show we are alive */
    if (bToggle)
    {
        LED_1_ON;
    }
    else
    {
        LED_1_OFF;
    }
    bToggle = !bToggle;

    /* Read ADC and convert value into mv */
    u16AdcReading = u16AHI_AdcRead();

    u16BattLevelmV = ((uint32)((uint32)(u16AdcReading * 586) +
                     ((uint32)(u16AdcReading * 586) >> 1)))  / 1000;

    /* Report result */
    vPrintf("\rBattery Voltage (mV): %d", u16BattLevelmV);

    /* Start ADC sampling again so that result will be ready next time we
       enter this loop */
    vAHI_AdcStartSample();
}

/****************************************************************************/
/***        END OF FILE                                                   ***/
/****************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -