📄 main.c
字号:
/*
-05/10/2006: Petre M.
-this project represents a Reference Design for a power meter using ADE7169Fxx
*/
#include "main.h"
#pragma vector=0x03
__interrupt void INT0_ISR(void)
{
}
#pragma vector=0x0b
__interrupt void TIMER0_ISR(void)
{
}
#pragma vector=0x13
__interrupt void INT1_ISR(void)
{
}
#pragma vector=0x1b
__interrupt void TIMER1_ISR(void)
{
}
#pragma vector=0x23
__interrupt void UART_ISR(void)
{
}
#pragma vector=0x2b
__interrupt void TIMER2_ISR(void)
{
}
#pragma vector=0x33
__interrupt void TEMPERATURE_ISR(void)
{
}
#pragma vector=0x3b
__interrupt void SPI_I2C_ISR(void)
{
}
#pragma vector=0x43
__interrupt void PSM_ISR(void)
{
}
#pragma vector=0x4b
__interrupt void ADE_ISR(void)
{
ADE_Interrupt();
}
#pragma vector=0x53
__interrupt void RTC_ISR(void)
{
//this bit is toggled in order to create a 1sec CF2 output.
P0_bit.CF2 = P0_bit.CF2 ^ 1;
}
#pragma vector=0x5b
__interrupt void WATCHDOG_ISR(void)
{
}
void main( void )
{
void (* LP_func_ptr)(void);
//if reset provoked by watchdog, turn the display full on and blink it
Test_Watchdog();
//Initializations common to both PSM0 and PSM1 modes
//disable all interrupt sources before doing the initializations
IE_bit.EA = 0;
Setup_Watchdog(WD_1sec);//sets PRE=6 -> 1sec for watchdog period
Setup_I2C();
//LCD SFRs are initialized and the display is erased
Setup_LCD();
Setup_Power();
Init_variables();
//if VSW=VDD, then the part functions in PSM0 mode
if (PERIPH_bit.VSWSOURCE) {
//put PSM0 initializations here
Setup_UART();
Init_variables_PSM0();//initializes some of the variables to avoid having the compiler doing it
//enable all interrupt sources
IE_bit.EA = 1;
Setup_RTC();
//we need to initialize all the ADE registers. Some of them use values from EEPROM
Setup_Energy();
//read from EEPROM all data previously stored in EEPROM
//The program waits until the data is stored in IRAM!!!!
Reload_from_EEPROM();
}
//else, VSW=VBAT, so we were in PSM2 mode (deep sleep) and
//we have been awaken and are now in PSM1 mode
else {
Setup_RTC_PSM1();
//in PSM1 mode, blink the LCD at 0.25Hz
Blink_LCD(Blink_250mHz);
Init_variables_PSM1();
//enable all interrupt sources only after all variables have been initialized
IE_bit.EA = 1;
}
//if VSW=VDD, then the part functions in PSM0 mode
//This is the PSM0 loop
while (PERIPH_bit.VSWSOURCE) {
//put all High Priority tasks here
Backup_State();//this function executes a backup of Energy Meter state if VDCIN<1.2V
Accumulate_Energy();
Manage_UART_Rx();//this function manages UART communication with the PC
if (TIMECON_bit.ALARM) {
//clear ALARM flag
TIMECON_bit.ALARM = 0;
//set Bit0 in Low_Priority_Command[0]
Set_flag(Bit0, &Low_Priority_Command[0]);
}
if (Low_Priority_Command[0] & Bit0) {
//execute one Low Priority Task
LP_func_ptr = (void (*) (void))Table_LP_Func[LP_Task];
(*LP_func_ptr)();
//increment pointer
LP_Task=LP_Task+1;
}
}
//Now, the program enters in Battery Mode (PSM1)
Midnight_Management_PSM1();
Monthly_Energy_Storage();
Prepare_for_Sleep();
Refresh_Watchdog();//this function prevents watchdog from timeout
//enter PSM2 mode (Deep Sleep): Bit4 of POWCON=1
//set P0.6=SCLK and P0.4=SDATA with weak pull up enabled otherwise the processor still
//consumes power in sleep mode
IE_bit.EA = 0;//disable all interrupts
// PINMAP0 = 0x50;
KYREG = 0xa7;
POWCON=0x10;
//in case the processor has changed supply from the battery to VDD,
//the command to enter PSM2 does not have any effect. For this reason, this instruction
//is executed only if the supply has changed to VDD
//The opcode of this instruction is 0x020000
asm("ljmp 0x0");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -