📄 background.c
字号:
/********************************************************************************
* 杭州利尔达 *
* MSP430FE42X单相防窃电多功能电表平台 *
* ----- ESP SD16实现 *
* *
* *
* 说明:本软件为杭州利尔达单相防窃电多功能电表DEMO软件 *
* *
*********************************************************************************/
#include "msp430xe42x.h"
#include "globe.h"
#include "emeter_cfg.h"
#include "extern_function.h"
void Switch_CalibrationOption_Add(void)
{
switch(emeter.ucCalibrationOption)
{
case 1: // GAINCORR1
emeter.ui_GainCorr1 += 1;
break;
case 2: // POWEROFFSET1
emeter.l_PowerOffset1 += 1;
break;
case 3: // PHASEOFFSET1
emeter.f_PhaseCorr1 += 0.001;
break;
case 4: // GAINCORR2
emeter.ui_GainCorr2 += 1;
break;
case 5: // POWEROFFSET2
emeter.l_PowerOffset2 += 1;
break;
case 6: // PHASEOFFSET2
emeter.f_PhaseCorr2 += 0.001;
break;
case 7: // CURRENTCORR1
emeter.ui_SD16_CurrentCorr1 += 1;
break;
case 8: // CURRENTCORR2
emeter.ui_SD16_CurrentCorr2 += 1;
break;
case 9: // VOLTAGECORR1
emeter.ui_SD16_VoltageCorr1 += 1;
break;
default:break;
}
}
void Switch_CalibrationOption_Sub(void)
{
switch(emeter.ucCalibrationOption)
{
case 1: // Cz
emeter.ui_GainCorr1 -= 1;
break;
case 2: // POWEROFFSET
emeter.l_PowerOffset1 -= 1;
break;
case 3: // PHASEOFFSET
emeter.f_PhaseCorr1 -= 0.001;
break;
case 4: // Cz
emeter.ui_GainCorr2 -= 1;
break;
case 5: // POWEROFFSET
emeter.l_PowerOffset2 -= 1;
break;
case 6: // PHASEOFFSET
emeter.f_PhaseCorr2 -= 0.001;
break;
case 7: // CURRENTCORR
emeter.ui_SD16_CurrentCorr1 += 1;
break;
case 8: // CURRENTCORR
emeter.ui_SD16_CurrentCorr2 += 1;
break;
case 9: // VOLTAGECORR
emeter.ui_SD16_VoltageCorr1 += 1;
break;
default:break;
}
}
/* 标记脉冲宽度 */
static int16 total_energy_led_remaining_time;
uint32 ul_actensper1,ul_actensper2;
union signed_long_word ds;
/****************************************************************
* ESP中断服务程序 *
****************************************************************/
interrupt[ESP430_VECTOR] void esp_isr (void)
{
uint16 msg_data= MBIN1;
uint16 msg = MBIN0;
if (msg == mEVENT)
{
if (msg_data & ENRDYFG)
{
emeter.ui_meter_status |= ENRDY_INT_OCCUR;
_BIC_SR_IRQ(LPM0_bits);
}
#ifndef ENERGY_1SEC
if (msg_data &ZXTRFG)
{
emeter.ui_meter_status |= V1_CZ_TRAILING;
}
if ((msg_data & ZXLDFG) && (emeter.ui_meter_status & V1_CZ_TRAILING))
{
emeter.ui_meter_status &= ~V1_CZ_TRAILING;
ul_actensper1 = (((uint32)ACTENSPER1_HI)<<16)+ACTENSPER1_LO;
ul_actensper2 = (((uint32)ACTENSPER2_HI)<<16)+ACTENSPER2_LO;
emeter.ui_meter_status |= ENSPER_INT_OCCUR;
emeter.ui_meter_status |= NEW_ENERGY_READY;
_BIC_SR_IRQ(LPM0_bits);
}
#endif
} // End of if (MBIN0 == mEVENT)
if (msg == mTEMPRDY)
{
emeter.i_temperature= msg_data;
emeter.ui_meter_status |= REQUIRE_RTC_CORRENT;
}
}
/************************************************************
* SD16中断函数 *
* *
* 说明:当ESP关闭时用SD16测量温度 *
*************************************************************/
interrupt[SD16_VECTOR] void SD16_ISR(void)
{
int16_t V_sample;
int16_t corrected;
int16_t I_live_sample;
int16_t I_neutral_sample;
int16_t adc_buffer[5];
int adc_ptr;
adc_buffer[0] = SD16MEM2; //Voltage
adc_buffer[1] = SD16MEM1; //Live current
adc_buffer[2] = SD16MEM0; //Neutral current
adc_ptr = -1;
//WDTCTL = WDT_ARST_1000;
//tpd_gen();
V_sample = adc_buffer[++adc_ptr];
if ((V_sample >= ADC_MAX || V_sample <= ADC_MIN) && phase->V_endstops)
phase->V_endstops--;
V_sample = dc_filter(&phase->V_dc_estimate[0], V_sample);
phase->V_history[(int) phase->V_history_index] = V_sample;
accum48(phase->V_sq_accum, imul16_up(V_sample, V_sample));
I_live_sample = phase->current.I_history[0];
phase->current.I_history[0] = phase->current.I_history[1];
corrected = adc_buffer[++adc_ptr];
if ((corrected >= ADC_MAX || corrected <= ADC_MIN) && phase->current.I_endstops)
phase->current.I_endstops--;
phase->current.I_history[1] = corrected;
I_live_sample = dc_filter(&phase->current.I_dc_estimate, I_live_sample);
accum48(phase->current.I_sq_accum, imul16_up(I_live_sample, I_live_sample));
++phase->current.sample_count;
I_neutral_sample = phase->neutral.I_history[0];
phase->neutral.I_history[0] = phase->neutral.I_history[1];
corrected = adc_buffer[++adc_ptr];
if ((corrected >= ADC_MAX || corrected <= ADC_MIN) && phase->neutral.I_endstops)
phase->neutral.I_endstops--;
phase->neutral.I_history[1] = corrected;
I_neutral_sample = dc_filter(&phase->neutral.I_dc_estimate, I_neutral_sample);
accum48(phase->neutral.I_sq_accum, imul16_up(I_neutral_sample, I_neutral_sample));
++phase->neutral.sample_count;
phase->V_history_index = (phase->V_history_index + 1) & V_HISTORY_MASK;
++phase->sample_count;
//if (phase->sample_count >= 4000)
if (phase->sample_count >= SAMPLES_PER_SECOND)
{
emeter.ui_anti_tamperring_status |= SD16_MEASURE_NEW_LOG;
_BIC_SR_IRQ(LPM0_bits);
}
}
uint16 ui_step_counter;
uint32 ul_total_power_infact;
static uint32 ul_energy_accu;
/*******************************************************************
* TIMERA中断函数 *
* *
* 说明:TIMERA定时中断发生校表脉冲 ×
********************************************************************/
interrupt[TIMERA0_VECTOR] void TA_isr (void)
{
TACCR0 += SAMPLE_PERIOD;
if((ul_energy_accu += ul_total_power_infact) >= ENERGY_PULSE_THRESHOLD)
{
ul_energy_accu -= ENERGY_PULSE_THRESHOLD;
SET_ACT_PULSE_LED;
total_energy_led_remaining_time = ENERGY_PULSE_DURATION;
}
if (total_energy_led_remaining_time && --total_energy_led_remaining_time == 0)
{
CLR_ACT_PULSE_LED;
}
}
interrupt[BASICTIMER_VECTOR] void BT_ISR(void)
{
static uint8 uc_BT_Int_Count;
static uint16 uiKeyDelay;
CheckExternVoltage();
if( ++uc_BT_Int_Count >= 15)
{
uc_BT_Int_Count = 0;
emeter.ui_meter_status |= BT_1SEC_INT_OCCUR;
}
if((P1IN & BIT0) == 0)
{
if(++uiKeyDelay >= 32)
{
switch(emeter.ucCalibrationMode)
{
case 1: // +
{
emeter.ui_meter_status |= REQUIRE_LCD_UPDATE;
Switch_CalibrationOption_Add();
break;
}
case 2: // -
{
emeter.ui_meter_status |= REQUIRE_LCD_UPDATE;
Switch_CalibrationOption_Sub();
break;
}
default:
{
break;
}
}
}
if(uiKeyDelay >= 33)
{
uiKeyDelay = 33;
}
}
else
{
if(uiKeyDelay >= 33)
{
if((emeter.ui_anti_tamperring_status & IN_ESP_MEASURE_MODE) == IN_ESP_MEASURE_MODE)
emeter.ui_meter_status |= REQUIRE_MODIFY_ESP_PARAM;
}
uiKeyDelay = 0;
}
_BIC_SR_IRQ(LPM3_bits);
}
/*****************************************************************
* P1口中断函数 *
* *
* 说明:实现简易校表功能 *
******************************************************************/
interrupt[PORT1_VECTOR] void P1_ISR(void)
{
if((P1IFG & BIT0) == BIT0)
{
switch(emeter.ucCalibrationMode)
{
case 1: // +
{
emeter.ui_meter_status |= REQUIRE_LCD_UPDATE;
if((emeter.ui_anti_tamperring_status & IN_ESP_MEASURE_MODE) == IN_ESP_MEASURE_MODE)
emeter.ui_meter_status |= REQUIRE_MODIFY_ESP_PARAM;
Switch_CalibrationOption_Add();
break;
}
case 2: // -
{
emeter.ui_meter_status |= REQUIRE_LCD_UPDATE;
if((emeter.ui_anti_tamperring_status & IN_ESP_MEASURE_MODE) == IN_ESP_MEASURE_MODE)
emeter.ui_meter_status |= REQUIRE_MODIFY_ESP_PARAM;
Switch_CalibrationOption_Sub();
break;
}
case 3:
{
emeter.ui_meter_status |= REQUIRE_LCD_UPDATE;
if(++emeter.ucCalibrationOption > 9)
emeter.ucCalibrationOption = 1;
break;
}
default:
{
break;
}
}
}
if((P1IFG & BIT1) == BIT1)
{
if(++emeter.ucCalibrationMode >= 4)
{
emeter.ucCalibrationMode = 0;
emeter.ui_meter_status |= REQUIRE_FLASH_OP;
}
emeter.ui_meter_status |= REQUIRE_LCD_UPDATE;
}
P1IFG = 0;
_BIC_SR_IRQ(LPM3_bits);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -