📄 power.c
字号:
/*
-02/22/2006: Petre M.
-this file contains functions used to manage the power supply of ADE7169Fxx
*/
#include "ioADE7169F16.h"
#include "extern_declarations.h"
#include "EEPROM_locations.h"
//this routine initializes the SFRs correspondent to power management
void Setup_Power(void) {
//clear eventual flags in IPSMF
IPSMF = 0x00;
//set BATPR only when the part is supplied from VDD
if (PERIPH_bit.VSWSOURCE) {
/*000000=reserved
00=battery switchover enabled on Low VDD only
*/
BATPR = 0x00;
}
//background battery measurements are enabled every 1 minute
BATVTH = Battery_Low;//we need to introduce here a real threshold!!!!
STRBPER=STRBPER | 0x0c;
//bit2=0, i.e.no external reference connected to REFIN pin
//bit1,0=01=RX pin enabled with wake-up disabled
// When UART communication is developed, these bits must be set!!!
//Until then, set these pins as GPIO!!!
PERIPH = (PERIPH & 0xF8)|Bit0;
/*0=no PSM interrupt when VSW passes from VBAT to VDD because this is something
PSM1 and PSM2 modes have to deal with
x=do not care about this bit here
0=no PSM interrupt for SAG event
0=reserved
0=no PSM interrupt for VSW related events
0=disable PSM interrupt for VBAT monitoring
0=no PSM interrupt when VSW switches from VDD to VBAT
0=disable PSM interrupt for VDCIN<1.2V
*/
IPSME = IPSME & 0x40;
//disable PSM interrupt in IEIP2
IEIP2_bit.EPSM = 0;
return;
}
//If a backup command has been received, then the state of the energy meter is stored
//into the EEPROM. We want to execute it only one time, so we use Bit0 of
//Command_Status[0] to manage this.
void Backup_State(void) {
//if a command to execute a backup has been given because VDCIN<1.2V
if (IPSMF_bit.FVDC) {
IPSMF_bit.FVDC = 0;
if (!(Command_Status[0]&Bit0)) {
//disable Alarm RTC interrupt otherwise the processor would
//be awoken every 0.5sec and at this point (VDD falling), we don't need anymore
//to execute Low Priority Tasks
TIMECON_bit.ITEN = 0;
//erase an eventual ALARM based command
//clear ALARM flag
TIMECON_bit.ALARM = 0;
//disable Energy Metering Interrupt
IEIP2_bit.EADE = 0;
//store the energies in EEPROM
Active_Energy_write();
//we read this byte to ensure the write process ends before the processor enters in sleep mode
Tx_CTRL_byte(Nr_Bytes_1, EEPROM_Fault_Days,&Fault_Days);
//wait until the backup has finished to switch on to the battery
/*000000=reserved
01=battery switchover enabled on Low VDD and VDCIN<1.2V
*/
BATPR = 0x01;
Blink_LCD(Blink_250mHz);//LCD is set to blink at 0.25Hz frequency (BLKFREQ=3)
Set_flag(Bit0, &Command_Status[0]);
//refresh watchdog here because the Alarm interrupt has been
//disabled and it can be that the watchdog is not anymore refreshed
Refresh_Watchdog();//this function prevents watchdog from timeout
}
}
return;
}
//this functions blinks the baterry icon of the LCD if VBAT<BATVTH with the frequency
//determined by the frequency of ALARM RTC interrupts (currently at 0.5sec period)
//The blink option of the LCD module cannot be used because we want to
//blink only the battery icon, not the entire LCD
void Battery_Alarm(void) {
char a;
//if IPSMF_bit.FBAT is 1 and BATTADC<BATVTH, then blink the battery LCD icon
if (IPSMF_bit.FBAT) {
//clear the flag
IPSMF_bit.FBAT=0;
a = BATADC;
if (a<Battery_Low) {
//Bit2 of LCD_buffer[4] is toggled with a period equal to ALARM RTC period
#ifdef GOWORLD_LCD
LCD_buffer[4] = LCD_buffer[4] ^ Bit2;
#endif
#ifdef OCULAR_LCD
LCD_buffer[8] = LCD_buffer[8] ^ Bit7;
#endif
//set a new threshold BATVTH=0xff
BATVTH = 0xff;
}
//if VBAT becomes greater than a value Battery_Low2, then stop blinking.
//Battery_Low2 != Battery_Low in order to obtain a hysteresis
//in triggering various modes
if (a>Battery_Low2) {
//clear the battery LCD icon
#ifdef GOWORLD_LCD
LCD_buffer[4] = LCD_buffer[4] & NBit2;
#endif
#ifdef OCULAR_LCD
LCD_buffer[8] = LCD_buffer[8] & NBit7;
#endif
//set a new threshold BATVTH=Battery_Low
BATVTH = Battery_Low;
}
}
return;
}
//this function executes some tasks that must be
//done before entering Deep Sleep Mode PSM2
void Prepare_for_Sleep(void) {
//copy LCD_command_ptr into Scratch1 register
//It is necessary to account for measurement visualizations
SCRATCH3 = Meter_Errors[0];
SCRATCH4 = LCD_Command[0];
return;
}
//this function reads SCRATCH SFRs and various data from
//EEPROM when the processor is awoken from PSM2
void Init_variables_PSM1(void) {
Meter_Errors[0] = SCRATCH3;
LCD_Command[0] = SCRATCH4;
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -