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

📄 batmodes.c

📁 TDK 6521 SOC 芯片 DEMO程序
💻 C
字号:
/***************************************************************************
 * This code and information is provided "as is" without warranty of any   *
 * kind, either expressed or implied, including but not limited to the     *
 * implied warranties of merchantability and/or fitness for a particular   *
 * purpose.                                                                *
 *                                                                         *
 * Copyright (C) 2005 Teridian SemiConductor, Corporation.                 *
 * All Rights Reserved.                                                    *
 ***************************************************************************/
//**************************************************************************
//  DESCRIPTION: 71M65xx POWER METER - Battery modes.  The actual
//  switching of modes is handled by the initialization in main ().  It's
//  there so that it is centralized, integrated with initialization, and 
//  automatically treats resets and non-reset start-ups in the same way.
// 
//  AUTHOR:  RGV
//
//  HISTORY: See end of file
//**************************************************************************
// File: BATMODES.C
//
#include "options.h"
#if BROWNOUT_BATMODE
#include "meter.h"
#include "defaults.h"       // default data tables
#include "delay.h"          // shared delay loop
#include "main.h"
#include "lcd.h"  // initialized for the meter's display
#include "eeprom.h"  // initialized for the meter's display
#include "wh.h"  // watt-hour display for brownout
#include "batmodes.h"

uint8_t batmode;
uint8_t batmode_valid;
uint8_t wake_count = 1;
#if M6520
#define IN_BROWNOUT_MODE (COMP0 != 0x41)
#endif

#pragma save
#pragma noaregs
bool batmode_is_brownout (void) small reentrant
{
#if M6520
    return IN_BROWNOUT_MODE;
#elif TRACE10
    return FALSE;
#else
#error unkown device
#endif
}
#pragma restore

#if LCD_ACTIVE && AUTOSLEEP
void batmode_display (void)
{
    // any other needed system initalization goes here
    LCD_Init();  // initialize the LCD interface
    eeprom_enable();  // initialize the EEPROM interface
    meter_brownout_lcd();  // meter\meter.c's logic to display the data
}
#endif

// does the right thing if there was a transition from
// brownout to power mode and vice-verse, which in this case
// is to simulate a reset, because the startup logic
// copes with power transitions.
// This is a separate routine because in the past there have
// been needs to insert debug code, etc.
#pragma save
#pragma noaregs
static void batmode_change (void) small reentrant
{
    EA = 0;
    CONFIG0 = 0;    // Make sure we are running as fast as we can.
    IFLAGS = (~IE_PLLFALL_) & (~IE_PLLRISE_);  // force an edge to occur

    // if entering brownout mode, and there's no battery, hang
    while(!BATTERY_MODE_ENABLE && IN_BROWNOUT_MODE)
        RESET_WD(); // wait for the power to die, OK due to slow MPU.

    main_soft_reset ();
}
#pragma restore

#pragma save
#pragma noaregs
void pll_isr (void) small reentrant interrupt PLL_IV
{
    EA = 0;
    batmode_change ();
}
#pragma restore

#pragma save
#pragma noaregs
void batmode_background (void) small reentrant
{
#if 0
    // if the PLL_OK bit -changes- from the state at reset,
    // then do something.
    if (batmode != IN_BROWNOUT_MODE 
            || batmode_valid != YES
            || (IFLAGS & (IE_PLLFALL_ | IE_PLLRISE_))
       )
    { 
        EA = 0;
        batmode_change ();
    }
#endif
}
#pragma restore

// start a wake timer in seconds.
#if AUTOSLEEP || CLI
void batmode_wait_seconds (uint16_t cs)
{
#if M6520
    delay_clks (3);
    cs = ((cs * 10) + 13)/25;
    cs = max (1, cs);
    cs = min ((WAKE_PRD), cs);
    cs &= WAKE_PRD;
    cs |= _2P5_SECONDS;
    wake_count = cs;
    cs |= WAKE_ARM;
    WAKE = cs;
#elif TRACE10
#else
#error unknown device
#endif
}
#endif

// start a wake timer in seconds.
#if CLI
void batmode_wait_minutes (uint16_t cs)
{
#if M6520
    delay_clks (3);
    cs = max (1, cs);
    cs = min ((WAKE_PRD), cs);
    cs &= WAKE_PRD;
    cs |= _1_MINUTE;
    wake_count = cs;
    cs |= WAKE_ARM;
    WAKE = cs;
#elif TRACE10
#else
#error unknown device
#endif
}
#endif

#if AUTOSLEEP || CLI
// enter LCD_only mode, a power-down mode that continues to display
// the LCD
void batmode_lcd (void)
{
#if M6520
    uint8_t tmp;
    delay_clks (3);
    tmp = LCD_ONLY | wake_count;
    wake_count = 1;
    WAKE = tmp;
#elif TRACE10
#else
#error unknown device
#endif
}
#endif // autosleep or cli

#if AUTOSLEEP || CLI
// enter sleep mode, a power-down mode that does nothing.
// but can be exited by either a wake timer, or a pushbutton
void batmode_sleep (void)
{
#if M6520
    uint8_t tmp;
    delay_clks (3);
    tmp = SLEEP | wake_count;
    wake_count = 1;
    WAKE = tmp;
#elif TRACE10
#else
#error unknown device
#endif
}
#endif // autosleep or cli

// initialize the battery mode
void batmode_init (void)
{
#if M6520
    bool ea = EA;                       // save interrupt state
    EA = 0;                             // disable interrupts
    wake_count = 1;
    WAKE = 0;                           // disable preceding wake stuff
    CLR_IE_WAKE();                      // clear the flags
    CLR_IE_PB();
    batmode = IN_BROWNOUT_MODE;
    batmode_valid = YES;
    // Interrupt priorities are set in main.
    CONFIG2 |= EX_PLL;                  // Enable PLL interrupts.  
    EX_PLL_OK   = TRUE;                 // Enable PLL state change interrupts.
    //batmode_background ();            // cope with a change...
    // if the PLL_OK bit -changes- from the state at reset,
    // then do something.
    if (batmode != IN_BROWNOUT_MODE 
            || batmode_valid != YES
            || (IFLAGS & (IE_PLLFALL_ | IE_PLLRISE_))
       )
    { 
        EA = 0;
        batmode_change ();
    }
    EA = ea;                            // restore interrupt state
#elif TRACE10
#else
#error unknown device
#endif
}

/***************************************************************************
 * $Log: batmodes.c,v $
 * Revision 1.30  2006/10/13 00:48:13  tvander
 * Removed compile options for 6530, 6515;
 * renamed 6511 and 6513 to trace11 and trace13;
 * Binary verified unchanged from previous version.
 *
 * Revision 1.29  2006/09/27 01:02:32  tvander
 * 6520 has a "Battery mode jumper" on DIO_8, added comments.
 * Changes baud rate depending on battery mode jumper.
 *
 * Revision 1.28  2006/09/20 23:50:26  tvander
 * Set interrupt priorities only once, during startup
 *
 * Revision 1.27  2006/09/19 16:55:17  tvander
 * The object is to reduce or handle a persistent hang in the PLL_OK interrupt.
 * Polling test looks for a hung PLL_OK interrupt, i.e. a set PLLRISING or PLLFALLING flag in IFLAGS.
 * Polling test is run as the last step of the start-up code, to speed recovery from
 * some hangs befor ethat point.
 * Also, initialization code disables interrupts.
 * The results are generally improved.
 *
 * Revision 1.26  2006/09/18 20:29:29  tvander
 * Combined common code, centralized interrupt priorities
 *
 * Revision 1.25  2006/09/09 01:10:42  gmikef
 * *** empty log message ***
 *
 * Revision 1.24  2006/09/06 02:16:25  tvander
 * Removed delay routine to its own file.
 *
 * Revision 1.23  2006/08/30 21:55:39  gmikef
 * *** empty log message ***
 *
 * Revision 1.22  2006/08/30 02:09:16  gmikef
 * *** empty log message ***
 *
 * Revision 1.21  2006/08/09 00:56:57  tvander
 * *** empty log message ***
 *
 * Revision 1.20  2006/07/11 22:31:03  tvander
 * Initialize flags
 *
 * Revision 1.19  2006/07/07 22:17:44  tvander
 * Restores data from EEPROM before displaying it in LCD mode.
 *
 * Revision 1.18  2006/07/07 01:13:06  tvander
 * Fixed reentrancy errors, build errors.
 * Added  untested code to hang brownout when batttery is not present.
 *
 * Revision 1.17  2006/06/24 04:43:23  tvander
 * Debugged the battery modes, especially the mission->brownout transition
 * Includes necessary changes for the reset hack needed for the 6521A03.
 *
 * Revision 1.16  2006/06/20 20:32:03  tvander
 * Incorporates logic to debounce the battery mode
 *
 * Revision 1.15  2006/06/15 20:57:10  tvander
 * Would display invalid power numbers.
 *
 * Revision 1.14  2006/06/14 02:59:05  tvander
 * Simplifiedthe brownout, regressing to 17april06 version, but with hang
 * removed, and a brownout display that always executes before entering sleep mode.
 *
 * Reduced the MPU clock 8x from 4.9mHz to 614kHz
 *
 * Revision 1.13  2006/06/09 00:21:32  tvander
 * Wrote, then commented-out the logic to blink an LCD segment.
 *
 * Revision 1.12  2006/05/30 17:17:40  tvander
 * Deprecated hardware dependencies
 *
 * Revision 1.11  2006/05/18 23:18:46  tvander
 * 16K and 32K
 * First cut at new requirements.
 * 32K 6521 is grossly tested.
 * All others have a clean compile with C51 8.02
 *
 * Revision 1.10  2006/03/06 03:33:00  Michael T. Fischer
 * More 6530 prep.
 *
 * Revision 1.9  2006/02/08 03:43:19  tvander
 * Made "import" the default power measurement mode, rather than net-metering
 *
 * Revision 1.8  2006/01/10 03:59:30  gmikef
 * Added PDATA support for CE Outputs.
 *
 * Revision 1.6  2005/11/10 22:51:32  tvander
 * 6520 has battery mode commands.
 * Brownout always has decimal point 7.
 * LCD Mode always has decimal points 7 and 6.
 * Sag detection is disabled.
 *
 * Revision 1.5  2005/11/09 02:21:16  tvander
 * Added code to display watt hours from brownout mode.
 * Added code to clear EEPROM (lapie command "EEE")
 *
 * Revision 1.4  2005/11/05 01:57:24  tvander
 * Added code to clear pushbutton and wake flags.
 *
 * Revision 1.3  2005/11/03 19:20:53  tvander
 * Fixed initialization of PLL_FALLING interrupt.
 * Rolled date.
 *
 * Revision 1.2  2005/10/12 23:00:02  tvander
 * Includes demonstratable mission mode, brownout, LCD and sleep modes
 *
 * Revision 1.1  2005/10/06 20:55:02  tvander
 * Preliminary logic to manage the battery modes.
 *
 * Copyright (C) 2005 Teridian Semiconductor Corp. All Rights Reserved.    *
 * this program is fully protected by the United States copyright          *
 * laws and is the property of Teridian Semiconductor Corporation.         *
 ***************************************************************************/
#endif

⌨️ 快捷键说明

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