1258 lines
44 KiB
C
1258 lines
44 KiB
C
/********************************** (C) COPYRIGHT *******************************
|
||
* File Name : peripheral_main.c
|
||
* Author : WCH / wangfq
|
||
* Version : V2.0 (algorithm ported from DLD154V4B)
|
||
* Date : 2026/07/18
|
||
* Description : DLD110SV4 ����Ȧ���������� ������
|
||
*
|
||
* �㷨˵�� (��ֲ�� DLD154V4B TaskLoop V2.0, M1H ����ϵ):
|
||
* - ����Ӧ������: LPCNT = MEASUREMENT_BASE / Xn, ������ʱ������ȦƵ����
|
||
* - һ�� IIR (79/256) + б���� 5% (�ܾ� EMI ˲̬���)
|
||
* - �ȶ��� 128 ���������� (�ƹ��˲�, С���� 100)
|
||
* - ���߸��� ���� 500 + ���ᱣ�� (��ʱ + �ȶ���˫����ǿ�Ƹ���)
|
||
* - ����ȷ�� 3 ��; �뿪: ƽ̹���������ж� (ר�� CN200910309382)
|
||
* - �����ڳ�ʱ / ��ȫ��λ: ȫ��λ�ؽ�����
|
||
*********************************************************************************
|
||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||
* Attention: This software (modified or not) and binary are used for
|
||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||
*******************************************************************************/
|
||
|
||
/******************************************************************************/
|
||
/* ͷ�ļ����� */
|
||
#include "CONFIG.h"
|
||
#include "HAL.h"
|
||
#include "gattprofile.h"
|
||
#include "peripheral.h"
|
||
#include "storage.h"
|
||
#include <stdlib.h>
|
||
#include "cmcng.h"
|
||
#include "dbn_ble_srv.h"
|
||
|
||
|
||
uint8_t g_flag_bt_state = 0; // 0 ����û�����ӣ�1 ����������
|
||
uint8_t g_flag_output = 0;
|
||
uint32_t g_xn_counter = 0;
|
||
uint8_t g_function_mode = 0;
|
||
uint32_t g_sys_freq = 0;
|
||
|
||
#define RLY1_ON (R32_PA_OUT |= BV(8))
|
||
#define RLY1_OFF (R32_PA_OUT &= ~BV(8))
|
||
#define RLY2_ON (R32_PB_OUT |= BV(23))
|
||
#define RLY2_OFF (R32_PB_OUT &= ~BV(23))
|
||
#define LEDA_ON (R32_PB_OUT &= ~BV(22))
|
||
#define LEDA_OFF (R32_PB_OUT |= BV(22))
|
||
#define LEDA_ON_OFF() (GPIOB_ReadPortPin(GPIO_Pin_22) ? GPIOB_ResetBits(GPIO_Pin_22) : GPIOB_SetBits(GPIO_Pin_22))
|
||
#define FLEVEL_H_LOW (R32_PA_OUT &= ~BV(12))
|
||
#define FLEVEL_H_HIGH (R32_PA_OUT |= BV(12))
|
||
#define FLEVEL_L_LOW (R32_PA_OUT &= ~BV(13))
|
||
#define FLEVEL_L_HIGH (R32_PA_OUT |= BV(13))
|
||
|
||
#define KEY_SA1 (GPIOA_ReadPortPin(GPIO_Pin_14))
|
||
#define KEY_SA2 (GPIOA_ReadPortPin(GPIO_Pin_15))
|
||
|
||
const uint8_t MAX_Store_Size = 80;
|
||
uint8_t g_loop_power_up_state = 0; // ��������ʱ��Ȧ�Ƿ�����
|
||
__IO uint32_t TimingDelayInc;
|
||
|
||
/*===========================================================================
|
||
* �㷨���� -- ��ֲ�� DLD154V4B (M4 �Ż���), ���� CH591R @60MHz
|
||
*
|
||
* ������: MEASUREMENT_BASE ��ϵͳʱ�� tick, ����ȦƵ����
|
||
* 524288(2^19) / 60MHz ~= 8.74ms, �� vd1_task ���������� ~8.74ms
|
||
* (DLD154V4B Ϊ 131072 @120MHz + 10ms ������ѯ, ʱ��߶Ȼ���һ��)
|
||
*===========================================================================*/
|
||
#define MEASUREMENT_BASE 524288L // 2^19, ����Ӧ������Ŀ��ֵ (Value ~= 524288)
|
||
#define ALFA_CAP1 79 // IIR a=79/256~=0.31 (@8.74ms -> tau~=24ms)
|
||
#define MAX_SLOPE_RATE 5 // б����: �������仯 5% (�ܾ� EMI ���)
|
||
#define ENTRY_CONFIRM 3 // ����ȷ��: ���� N �ε�����ֵ�����г�
|
||
#define FREEZE_TIMEOUT 1000 // ���߶��ᳬʱ: ~8.7s, ����ƫ�����ȶ���ǿ�Ƹ���
|
||
#define FREEZE_STABILITY_RATE 2 // �����ȶ��Դ���: �ο�ֵ +-2%
|
||
#define USE_FLATNESS_EXIT 1 // 1=ƽ̹���������뿪, 0=���ͻط���
|
||
#define WINDOW_ORIGIN 500 // ���߸��ٴ��� (500 �� ~= 4.4s)
|
||
#define STABLE_SAMPLES 128 // �ȶ��������� (~1.1s)
|
||
#define VARIATION_EVENT_TH 40 // BLE variation �¼���ֵ (��ֵ�� 2^19, ��Ӧ�ɴ���� 5)
|
||
#define ENV_ACT_TH 30 // ������Ծ��: CAPVD ���ڲ����ֵ��ֵ (���ֳ���֤)
|
||
|
||
uint32_t mstick(void){
|
||
return TimingDelayInc;
|
||
}
|
||
|
||
uint32_t u32CountWdt = 0;
|
||
uint8_t g_loop_cut_last = 0;
|
||
uint32_t g_loop_cut_amount = 0;
|
||
|
||
|
||
/*********************************************************************
|
||
* GLOBAL TYPEDEFS
|
||
*/
|
||
__attribute__((aligned(4))) uint32_t MEM_BUF[BLE_MEMHEAP_SIZE / 4];
|
||
|
||
#if(defined(BLE_MAC)) && (BLE_MAC == TRUE)
|
||
const uint8_t MacAddr[6] = {0x84, 0xC2, 0xE4, 0x03, 0x02, 0x02};
|
||
#else
|
||
uint8_t MACAddr[6] = {0x84, 0xC2, 0xE4, 0x03, 0x02, 0x02};
|
||
#endif
|
||
|
||
uint8_t g_dev_number[6] = ""; // �豸��� ��Ʒ���
|
||
char g_flag_debug = 1;
|
||
|
||
|
||
uint8_t g_freq_sens;
|
||
uint8_t loop1_SensLevel;
|
||
uint8_t g_freq_level;
|
||
|
||
uint16_t g_safe_max_cnt = LC_HOLD_TIME;
|
||
|
||
uint16_t Hold_CNT = 0;
|
||
uint8_t Flt_Reg = 0;
|
||
uint8_t TM1cnt = 0;
|
||
uint16_t g_hold_time = 0;
|
||
|
||
/*===========================================================================
|
||
* ���� & ����
|
||
*===========================================================================*/
|
||
uint16_t loop1_Xn; // �����½������� (TMR0 ֱ�Ӹ���)
|
||
uint32_t loop1_dlt_ORG; // ��ǰ��������ֵ
|
||
uint32_t loop1_Origin; // ���� (���ο�ֵ)
|
||
uint16_t loop1_LPCNT; // ÿ���������ۼӵ� Xn ����
|
||
uint16_t loop1_CapCnt; // ��ǰ�������ۼӴ���
|
||
uint32_t loop1_CapSum; // ��ǰ�����ۼӺ�
|
||
uint32_t loop1_CAPVD; // IIR �˲���IJ���ֵ
|
||
uint32_t loop1_Value; // һ����������ԭʼ Sigma-Xn
|
||
uint32_t loop1_ORG_SUM; // ���߸����ۼӺ�
|
||
uint16_t loop1_ORG_CNT; // ���߸��ټ���
|
||
|
||
/*===========================================================================
|
||
* ������
|
||
*===========================================================================*/
|
||
uint8_t loop1_INCNT;
|
||
uint8_t loop1_OUTCNT;
|
||
|
||
/*===========================================================================
|
||
* ��־λ
|
||
*===========================================================================*/
|
||
uint8_t loop1_INI_LOOP;
|
||
uint8_t loop1_CAP_OK;
|
||
uint8_t loop1_VD_FLAG;
|
||
uint8_t loop1_VD_HOLD;
|
||
uint8_t loop1_RF_FLAG;
|
||
uint8_t loop1_LOOP_OK;
|
||
uint8_t loop1_LOOP_OK0;
|
||
uint8_t loop1_FLAG_IN;
|
||
uint8_t loop1_FLAG_OUT;
|
||
uint8_t loop1_FLAG_PLUSE;
|
||
uint8_t loop1_FLAG_PLUSE_IN_F = 0;
|
||
uint8_t loop1_FLAG_PLUSE_IN = 0;
|
||
uint8_t loop1_FLAG_CUT;
|
||
uint8_t loop1_FLAG_HOLD_TIMEOUT; // �����ڳ�ʱ
|
||
|
||
/*===========================================================================
|
||
* ��ȫ��λ
|
||
*===========================================================================*/
|
||
uint8_t loop1_LC_HOLD = 0;
|
||
uint8_t loop1_LC_Reset = 0;
|
||
uint32_t LC_Hold_CNT = 0;
|
||
|
||
/*===========================================================================
|
||
* ���㷨״̬ (DLD154V4B ��ֲ)
|
||
*===========================================================================*/
|
||
uint8_t g_loop_stable = 0; // ��Ȧ��ֵ���ȶ� (0=�ȶ�����, 1=�ȶ�)
|
||
uint16_t loop1_stable_cnt = 0; // �ȶ�����������
|
||
uint8_t loop1_entry_cnt = 0; // ����ȷ�ϼ���
|
||
uint8_t loop1_cnt_release = 0; // ���뿪�������� (USE_FLATNESS_EXIT=0)
|
||
uint16_t loop1_freeze_cnt = 0; // ���߶����������
|
||
uint32_t loop1_freeze_ref = 0; // �����ȶ��Բο�ֵ
|
||
#if USE_FLATNESS_EXIT
|
||
uint8_t g_exit_state = 0; // 0=������, 1=�ȴ�ƽ̹
|
||
uint16_t g_max_slope = 0; // ��һ����������� |f'|
|
||
uint16_t g_max_slope_rate = 0; // ��һ����������� |f''|
|
||
uint16_t g_delta2 = 0; // һ��ƽ̹��ֵ
|
||
uint16_t g_delta3 = 0; // ����ƽ̹��ֵ
|
||
int32_t g_prev_capvd = 0; // ��һ֡ CAPVD (�����)
|
||
int32_t g_prev_first_deriv = 0; // ��һ֡һ����
|
||
uint8_t g_slope_flat_cnt = 0; // �������������
|
||
uint8_t g_flat_ok_cnt = 0; // ƽ̹����������������
|
||
#endif
|
||
|
||
/* ������Ծ�� (�� BLE condition �ֶ��ϱ�, ����� flt_mgr.cnt_not_idle) */
|
||
uint8_t g_env_activity = 0;
|
||
uint32_t g_env_prev_capvd = 0;
|
||
|
||
/*===========================================================================
|
||
* ����������Ĭ�ϱ� (д�� g_loop_sens_list, ����ʱ�� g_loop_sens_list Ϊ)
|
||
* ������ֵ = Origin * sens_in / 65536; �뿪��ֵ = Origin * sens_out / 65536
|
||
*===========================================================================*/
|
||
const uint16_t SensTable[MAX_LOOP_SENS_AMOUNT] = { 108, 54, 28};
|
||
const uint16_t SensTable1[MAX_LOOP_SENS_AMOUNT] = {81, 32, 16};
|
||
|
||
uint16_t g_loop_out_delay = 10; // �뿪��ʱ, 50ms Ϊ��λ, �� delay_time ����
|
||
|
||
DBN_BLE_State g_dbn_ble_state_acs_enable = {0};
|
||
Loop_ACS_Info g_loop_acs_info = {0,};
|
||
|
||
float g_freq_tmp = 0.0;
|
||
|
||
|
||
/* ����APP�ж��ļ���Ч�� */
|
||
const uint32_t Address = 0xFFFFFFFF;
|
||
|
||
__attribute__((aligned(4))) uint32_t Image_Flag __attribute__((section(".ImageFlag"))) = (uint32_t)&Address;
|
||
|
||
|
||
|
||
uint8_t loop_cng_default[7] = {2,1,0,0,4,0,0};
|
||
|
||
|
||
void set_factory_param(void)
|
||
{
|
||
uint8_t i;
|
||
|
||
memcpy(&g_loop_cng_unit.sensitvity, loop_cng_default, 7);
|
||
|
||
for(i = 0; i < g_loop_sens_list.total; i++)
|
||
{
|
||
g_loop_sens_list.sens[i].sens_in = SensTable[i];
|
||
g_loop_sens_list.sens[i].sens_out = SensTable1[i];
|
||
}
|
||
|
||
}
|
||
|
||
|
||
void factory_dev(void){
|
||
uint8_t i = 0, k = 0;
|
||
|
||
uint8_t *rBuf = (uint8_t *)malloc(MAX_Store_Size);
|
||
if(rBuf == NULL){
|
||
SYS_ResetExecute();
|
||
}
|
||
for(i = 0; i < MAX_Store_Size; i++){
|
||
rBuf[i] = 0;
|
||
}
|
||
|
||
rBuf[0] = 0x33;
|
||
rBuf[1] = 0xBB;
|
||
rBuf[2] = 0xC3;
|
||
rBuf[3] = 0x3C;
|
||
|
||
i = 4;
|
||
memcpy(&rBuf[i], &g_loop_cng_unit.sensitvity, 7);
|
||
i += 7;
|
||
|
||
i = Addr_Loop_Sens_List_Offset;
|
||
if(g_loop_sens_list.total > MAX_LOOP_SENS_AMOUNT)
|
||
{
|
||
g_loop_sens_list.total = MAX_LOOP_SENS_AMOUNT;
|
||
}
|
||
rBuf[i++] = g_loop_sens_list.total;
|
||
for(k = 0; k < MAX_LOOP_SENS_AMOUNT; k++)
|
||
{
|
||
rBuf[i++] = g_loop_sens_list.sens[k].sens_in;
|
||
rBuf[i++] = g_loop_sens_list.sens[k].sens_in >> 8;
|
||
rBuf[i++] = g_loop_sens_list.sens[k].sens_out;
|
||
rBuf[i++] = g_loop_sens_list.sens[k].sens_out >> 8;
|
||
}
|
||
|
||
write_cfg_to_store(0, rBuf, MAX_Store_Size);
|
||
|
||
free(rBuf);
|
||
|
||
}
|
||
|
||
void poll_sens_key(void)
|
||
{
|
||
static uint8_t _last_sens = 3;
|
||
|
||
#if DEBUG
|
||
g_freq_sens = 2; // Ĭ�����������
|
||
#else
|
||
if(KEY_SA1){
|
||
if(KEY_SA2){
|
||
g_freq_sens = 0;
|
||
}
|
||
else {
|
||
g_freq_sens = 2;
|
||
}
|
||
}
|
||
else{
|
||
if (KEY_SA2) {
|
||
g_freq_sens = 1;
|
||
}
|
||
}
|
||
#endif
|
||
|
||
if(_last_sens == 3){
|
||
_last_sens = g_freq_sens;
|
||
}
|
||
|
||
if(_last_sens != g_freq_sens){
|
||
/* ��������ֵÿ������ g_loop_sens_list ʵʱ����, �������� */
|
||
_last_sens = g_freq_sens;
|
||
}
|
||
|
||
}
|
||
|
||
|
||
void para_store_init(void)
|
||
{
|
||
uint8_t *rBuf = (uint8_t *)malloc(MAX_Store_Size);
|
||
|
||
if(rBuf == NULL){
|
||
SYS_ResetExecute();
|
||
}
|
||
memset(rBuf, 0, MAX_Store_Size);
|
||
|
||
read_cfg_from_store(0, rBuf, MAX_Store_Size);
|
||
|
||
if(rBuf[0] == 0x33 && rBuf[1] == 0xBB && rBuf[2] == 0xC3 && rBuf[3] == 0x3C){
|
||
memcpy(&g_loop_cng_unit, &rBuf[4], sizeof(g_loop_cng_unit));
|
||
memcpy(&g_loop_sens_list, &rBuf[Addr_Loop_Sens_List_Offset], sizeof(g_loop_sens_list));
|
||
|
||
poll_sens_key();
|
||
|
||
g_loop_out_delay = g_loop_cng_unit.delay_time * 2; // ��ʱ���� 50ms Ϊ��λ, ��ʱ��100msΪ��λ
|
||
g_hold_time = g_loop_cng_unit.exist_mode * 30 * 20; //50ms/per, 30s
|
||
g_safe_max_cnt = g_loop_cng_unit.loopSafe_Timeout * 10 * (1000 / 50);
|
||
g_freq_level = g_loop_cng_unit.loopFreq_Level - 1;
|
||
g_function_mode = g_loop_cng_unit.direction_mode >> 4;
|
||
|
||
#ifdef DEBUG
|
||
g_freq_level = 0;
|
||
#endif
|
||
|
||
switch(g_freq_level)
|
||
{
|
||
case 0: {
|
||
// ��Ƶ
|
||
FLEVEL_H_LOW; FLEVEL_L_LOW;
|
||
} break;
|
||
case 1: {
|
||
// �и�
|
||
FLEVEL_H_LOW; FLEVEL_L_HIGH;
|
||
} break;
|
||
case 2: {
|
||
// �е�
|
||
FLEVEL_H_HIGH; FLEVEL_L_LOW;
|
||
} break;
|
||
case 3: {
|
||
// ��Ƶ
|
||
FLEVEL_H_HIGH; FLEVEL_L_HIGH;
|
||
} break;
|
||
default:{
|
||
// Ĭ�ϵ�Ƶ
|
||
g_freq_level = 3;
|
||
FLEVEL_H_HIGH; FLEVEL_L_HIGH;
|
||
} break;
|
||
}
|
||
free(rBuf);
|
||
}
|
||
else{
|
||
free(rBuf);
|
||
set_factory_param();
|
||
factory_dev();
|
||
|
||
DelayMs(10);
|
||
SYS_ResetExecute();
|
||
}
|
||
|
||
}
|
||
|
||
|
||
|
||
void tm_init(void){
|
||
|
||
// Cap ����
|
||
GPIOA_ResetBits(GPIO_Pin_9);
|
||
GPIOA_ModeCfg(GPIO_Pin_9, GPIO_ModeIN_Floating);
|
||
|
||
TMR0_CapInit(FallEdge_To_FallEdge);
|
||
TMR0_CAPTimeoutCfg(0xFFFFFFFF);
|
||
TMR0_ClearITFlag(TMR0_3_IT_DATA_ACT);
|
||
TMR0_ITCfg(ENABLE, TMR0_3_IT_DATA_ACT);
|
||
PFIC_EnableIRQ(TMR0_IRQn);
|
||
|
||
|
||
// TMR1
|
||
TMR1_TimerInit(FREQ_SYS / 200); // ���ö�ʱʱ�� 5ms
|
||
TMR1_ITCfg(ENABLE, TMR0_3_IT_CYC_END); // �����ж�
|
||
PFIC_EnableIRQ(TMR1_IRQn);
|
||
}
|
||
|
||
void gpio_uart_init(void)
|
||
{
|
||
/* ���ô���0��������IO��ģʽ�������ô��� */
|
||
#ifdef DEBUG
|
||
GPIOPinRemap(ENABLE, RB_PIN_UART0);
|
||
GPIOA_SetBits(GPIO_Pin_14);
|
||
GPIOA_ModeCfg(GPIO_Pin_14, GPIO_ModeOut_PP_5mA); // TXD-�������������ע������IO������ߵ�ƽ
|
||
|
||
UART0_BaudRateCfg(230400); //115200
|
||
R8_UART0_FCR = (2 << 6) | RB_FCR_TX_FIFO_CLR | RB_FCR_RX_FIFO_CLR | RB_FCR_FIFO_EN; // FIFO��������4�ֽ�
|
||
R8_UART0_LCR = RB_LCR_WORD_SZ;
|
||
R8_UART0_IER = RB_IER_TXD_EN;
|
||
R8_UART0_DIV = 1;
|
||
|
||
GPIOA_ModeCfg(GPIO_Pin_15, GPIO_ModeIN_PU); // �������أ�����������
|
||
#else
|
||
|
||
GPIOA_ModeCfg(GPIO_Pin_15|GPIO_Pin_14, GPIO_ModeIN_PU); // �������أ�����������
|
||
|
||
#endif
|
||
|
||
|
||
GPIOA_SetBits(GPIO_Pin_12|GPIO_Pin_13);
|
||
GPIOA_ModeCfg(GPIO_Pin_12|GPIO_Pin_13, GPIO_ModeOut_PP_5mA);
|
||
FLEVEL_H_LOW; FLEVEL_L_LOW;
|
||
|
||
GPIOA_SetBits(GPIO_Pin_8);
|
||
GPIOA_ModeCfg(GPIO_Pin_8, GPIO_ModeOut_PP_5mA);
|
||
GPIOB_SetBits(GPIO_Pin_22|GPIO_Pin_23);
|
||
GPIOB_ModeCfg(GPIO_Pin_22|GPIO_Pin_23, GPIO_ModeOut_PP_5mA);
|
||
RLY1_OFF; RLY2_OFF; LEDA_OFF;
|
||
|
||
}
|
||
|
||
|
||
/*===========================================================================
|
||
* һ�� IIR ��ͨ�˲��� (���� M1H ��ʽ)
|
||
* CAPVD_new = a * new_value + (1-a) * CAPVD_old, a = Flt_Reg/256
|
||
*===========================================================================*/
|
||
uint32_t get_flt_value(uint32_t new_value, uint32_t last_Value) {
|
||
uint32_t value_Flt;
|
||
uint32_t delta = (new_value > last_Value) ? (new_value - last_Value) : (last_Value - new_value);
|
||
uint32_t scaled_delta = (delta * (uint32_t)Flt_Reg) >> 8;
|
||
|
||
if (new_value > last_Value) {
|
||
value_Flt = last_Value + scaled_delta;
|
||
} else {
|
||
value_Flt = last_Value - scaled_delta;
|
||
}
|
||
|
||
return value_Flt;
|
||
}
|
||
|
||
|
||
/*===========================================================================
|
||
* ����ƽ�����߸���
|
||
* ע��: window ������ uint16_t (WINDOW_ORIGIN=500 > 255, uint8_t �ᾲĬ�ض�)
|
||
*===========================================================================*/
|
||
void update_moving_average(uint32_t* p_sum, uint16_t* p_cnt, uint32_t* p_origin, uint32_t new_value, uint16_t window) {
|
||
if (!p_sum || !p_cnt || !p_origin || window == 0) {
|
||
return;
|
||
}
|
||
|
||
*p_sum += new_value;
|
||
(*p_cnt)++;
|
||
|
||
if (*p_cnt >= window) {
|
||
*p_origin = (uint32_t)(*p_sum / window);
|
||
*p_cnt = 0;
|
||
*p_sum = 0;
|
||
}
|
||
}
|
||
|
||
|
||
/*===========================================================================
|
||
* ��ʼ�����м��״̬ (�ϵ� / �����ڳ�ʱ / ��Ȧ���� ʱ����)
|
||
* ע��: ����λ g_loop_power_up_state / g_loop_cut_amount (BLE �ϱ���)
|
||
*===========================================================================*/
|
||
void INIT_VD(void)
|
||
{
|
||
loop1_INI_LOOP = 1;
|
||
loop1_LPCNT = 0;
|
||
loop1_INCNT = 0;
|
||
loop1_OUTCNT = 0;
|
||
loop1_CapCnt = 0;
|
||
loop1_CapSum = 0;
|
||
loop1_Value = 0;
|
||
loop1_CAPVD = 0;
|
||
loop1_Origin = 0;
|
||
|
||
loop1_VD_FLAG = 0;
|
||
loop1_RF_FLAG = 0;
|
||
loop1_LOOP_OK = 1;
|
||
loop1_LOOP_OK0 = 0;
|
||
loop1_CAP_OK = 0;
|
||
|
||
loop1_SensLevel = 2;
|
||
Flt_Reg = ALFA_CAP1; //79
|
||
|
||
loop1_ORG_CNT = 0;
|
||
loop1_ORG_SUM = 0;
|
||
|
||
loop1_FLAG_IN = 0;
|
||
loop1_FLAG_OUT = 0;
|
||
loop1_FLAG_PLUSE = 0;
|
||
loop1_FLAG_PLUSE_IN_F = 0;
|
||
loop1_FLAG_PLUSE_IN = 0;
|
||
loop1_FLAG_CUT = 0;
|
||
loop1_FLAG_HOLD_TIMEOUT = 0;
|
||
|
||
loop1_VD_HOLD = 0;
|
||
Hold_CNT = 0;
|
||
loop1_LC_HOLD = 0;
|
||
loop1_LC_Reset = 0;
|
||
LC_Hold_CNT = 0;
|
||
|
||
/* ���㷨״̬ */
|
||
g_loop_stable = 0;
|
||
loop1_stable_cnt = 0;
|
||
loop1_entry_cnt = 0;
|
||
loop1_cnt_release = 0;
|
||
loop1_freeze_cnt = 0;
|
||
loop1_freeze_ref = 0;
|
||
#if USE_FLATNESS_EXIT
|
||
g_exit_state = 0;
|
||
g_max_slope = 0;
|
||
g_max_slope_rate = 0;
|
||
g_delta2 = 0;
|
||
g_delta3 = 0;
|
||
g_prev_capvd = 0;
|
||
g_prev_first_deriv = 0;
|
||
g_slope_flat_cnt = 0;
|
||
g_flat_ok_cnt = 0;
|
||
#endif
|
||
g_env_activity = 0;
|
||
g_env_prev_capvd = 0;
|
||
|
||
g_loop_acs_info.car_state = 0;
|
||
}
|
||
|
||
|
||
/*===========================================================================
|
||
* �����뿪 / �ͷ� ��������
|
||
*===========================================================================*/
|
||
static void loop_release_common(void)
|
||
{
|
||
loop1_VD_FLAG = 0;
|
||
loop1_FLAG_OUT = 1;
|
||
loop1_VD_HOLD = 0;
|
||
loop1_LC_HOLD = 0;
|
||
Hold_CNT = 0;
|
||
LC_Hold_CNT = 0;
|
||
loop1_ORG_CNT = 0;
|
||
loop1_ORG_SUM = 0;
|
||
|
||
g_loop_acs_info.flag_change = 1;
|
||
g_loop_acs_info.flag_event = 1;
|
||
g_loop_acs_info.car_state = 0;
|
||
}
|
||
|
||
|
||
/*===========================================================================
|
||
* vd1_task -- ���ļ���㷨 (��ֲ DLD154V4B, ÿ�� CAP_OK ����, ~8.74ms)
|
||
*
|
||
* ����:
|
||
* 1. IIR �˲� + б����
|
||
* 2. �ȶ���: С���ڿ�����������
|
||
* 3. ��: ���߸��� (���ᱣ��) + ����ȷ��
|
||
* 4. �г�: ƽ̹���������뿪�ж� (���ͻ�)
|
||
*===========================================================================*/
|
||
void vd1_task(void){
|
||
|
||
if (loop1_Origin == 0) return;
|
||
|
||
/*--- 1. IIR �˲� + б���� ---*/
|
||
if (loop1_CAPVD == 0) {
|
||
loop1_CAPVD = loop1_Value;
|
||
} else {
|
||
int32_t raw_delta = (int32_t)loop1_Value - (int32_t)loop1_CAPVD;
|
||
int32_t max_step = (int32_t)(loop1_CAPVD * MAX_SLOPE_RATE / 100);
|
||
uint32_t clamped_value;
|
||
if (max_step < 100) max_step = 100;
|
||
if (raw_delta > max_step) raw_delta = max_step;
|
||
if (raw_delta < -max_step) raw_delta = -max_step;
|
||
clamped_value = (uint32_t)((int32_t)loop1_CAPVD + raw_delta);
|
||
loop1_CAPVD = get_flt_value(clamped_value, loop1_CAPVD);
|
||
}
|
||
|
||
/*--- 2. �ȶ���: �ƹ� IIR/б����, С���ڿ������� ---*/
|
||
if (!g_loop_stable) {
|
||
loop1_CAPVD = loop1_Value;
|
||
update_moving_average(&loop1_ORG_SUM, &loop1_ORG_CNT, &loop1_Origin, loop1_CAPVD, 100);
|
||
loop1_stable_cnt++;
|
||
if (loop1_stable_cnt >= STABLE_SAMPLES) {
|
||
loop1_stable_cnt = 0;
|
||
g_loop_stable = 1;
|
||
}
|
||
return;
|
||
}
|
||
|
||
/*--- 3. BLE �ϱ�״̬ˢ�� (flag_change ��λ�ڼ䶳��, �����¼�ʱ�̵�ֵ) ---*/
|
||
if (g_loop_acs_info.flag_change == 0) {
|
||
int32_t _var = (int32_t)loop1_Origin - (int32_t)loop1_CAPVD;
|
||
g_loop_acs_info.loop_capvd = loop1_CAPVD;
|
||
g_loop_acs_info.variation = _var;
|
||
if (g_flag_bt_state && (_var > VARIATION_EVENT_TH || _var < -VARIATION_EVENT_TH)) {
|
||
g_loop_acs_info.flag_event = 1;
|
||
}
|
||
}
|
||
|
||
/*--- 4. ������Ծ�� (BLE condition �ֶ�, 0~15) ---*/
|
||
{
|
||
int32_t d = (int32_t)loop1_CAPVD - (int32_t)g_env_prev_capvd;
|
||
if (d < 0) d = -d;
|
||
if (g_env_prev_capvd != 0 && d > ENV_ACT_TH) {
|
||
if (g_env_activity < 200) g_env_activity++;
|
||
} else {
|
||
if (g_env_activity) g_env_activity--;
|
||
}
|
||
g_env_prev_capvd = loop1_CAPVD;
|
||
}
|
||
|
||
if (!loop1_VD_FLAG) {
|
||
/*================================================================
|
||
* ��״̬: ���߸��� (���ᱣ��) + ����ȷ��
|
||
*================================================================*/
|
||
loop1_dlt_ORG = ((uint32_t)loop1_Origin * g_loop_sens_list.sens[g_freq_sens].sens_in) >> 16;
|
||
{
|
||
int32_t dev = (int32_t)loop1_CAPVD - (int32_t)loop1_Origin;
|
||
if (dev < (int32_t)(loop1_dlt_ORG * 4)) {
|
||
loop1_freeze_cnt = 0;
|
||
update_moving_average(&loop1_ORG_SUM, &loop1_ORG_CNT, &loop1_Origin, loop1_CAPVD, WINDOW_ORIGIN);
|
||
} else {
|
||
/*--- CAPVD �쳣ƫ��: ��ͣ����, ���ᳬʱ + �ȶ���˫���� ---*/
|
||
if (loop1_freeze_cnt == 0) {
|
||
loop1_freeze_ref = loop1_CAPVD;
|
||
} else {
|
||
int32_t drift = (int32_t)loop1_CAPVD - (int32_t)loop1_freeze_ref;
|
||
if (drift < 0) drift = -drift;
|
||
if (drift > (int32_t)(loop1_freeze_ref * FREEZE_STABILITY_RATE / 100)) {
|
||
loop1_freeze_cnt = 0;
|
||
loop1_freeze_ref = loop1_CAPVD;
|
||
}
|
||
}
|
||
loop1_freeze_cnt++;
|
||
if (loop1_freeze_cnt >= FREEZE_TIMEOUT) {
|
||
loop1_Origin = loop1_CAPVD; /* ȷ���ȶ����³�̬ */
|
||
loop1_freeze_cnt = 0;
|
||
loop1_freeze_ref = 0;
|
||
}
|
||
loop1_ORG_CNT = 0;
|
||
loop1_ORG_SUM = 0;
|
||
}
|
||
}
|
||
|
||
/*--- ����ȷ��: ���� ENTRY_CONFIRM �ε�����ֵ�����г� ---*/
|
||
if (loop1_CAPVD < (loop1_Origin - loop1_dlt_ORG)) {
|
||
loop1_entry_cnt++;
|
||
if (loop1_entry_cnt >= ENTRY_CONFIRM) {
|
||
loop1_entry_cnt = 0;
|
||
loop1_freeze_cnt = 0;
|
||
loop1_freeze_ref = 0;
|
||
|
||
loop1_VD_FLAG = 1;
|
||
loop1_FLAG_IN = 1;
|
||
|
||
if (g_loop_cng_unit.exist_mode) { /* ������ */
|
||
loop1_VD_HOLD = 1;
|
||
Hold_CNT = 0;
|
||
}
|
||
if (g_loop_cng_unit.loopSafe_Timeout) { /* ��Ȧ��ȫģʽ: ������ȫ��λ��ʱ */
|
||
loop1_LC_HOLD = 1;
|
||
LC_Hold_CNT = 0;
|
||
} else {
|
||
loop1_LC_HOLD = 0;
|
||
}
|
||
if (loop1_LC_Reset) {
|
||
loop1_LC_Reset = 0;
|
||
}
|
||
|
||
loop1_ORG_CNT = 0;
|
||
loop1_ORG_SUM = 0;
|
||
|
||
g_loop_acs_info.flag_change = 1;
|
||
g_loop_acs_info.flag_event = 1;
|
||
g_loop_acs_info.car_state = 1;
|
||
|
||
#if USE_FLATNESS_EXIT
|
||
/* ����ƽ̹��״̬ (ר�� CN200910309382) */
|
||
g_exit_state = 0;
|
||
g_max_slope = 0;
|
||
g_max_slope_rate = 0;
|
||
g_delta2 = 0;
|
||
g_delta3 = 0;
|
||
g_slope_flat_cnt = 0;
|
||
g_flat_ok_cnt = 0;
|
||
g_prev_capvd = (int32_t)loop1_CAPVD;
|
||
g_prev_first_deriv = 0;
|
||
#endif
|
||
}
|
||
} else {
|
||
if (loop1_entry_cnt > 0) loop1_entry_cnt = 0;
|
||
}
|
||
}
|
||
else {
|
||
#if USE_FLATNESS_EXIT
|
||
/*================================================================
|
||
* �г�״̬: ƽ̹���������ж��뿪 (ר�� CN200910309382)
|
||
* ��ֵ��ֵ���²����� 2^19 ���� (DLD154V4B: 100/5/2 @2^17)
|
||
*================================================================*/
|
||
#define K1 8
|
||
#define K2 8
|
||
#define SLOPE_FLAT_THRESH 350
|
||
#define MIN_DELTA2 18
|
||
#define MIN_DELTA3 7
|
||
#define FLAT_CONFIRM_CNT 3
|
||
|
||
int32_t first_deriv, abs_fd, abs_sd, second_deriv;
|
||
|
||
first_deriv = (int32_t)loop1_CAPVD - g_prev_capvd;
|
||
second_deriv = first_deriv - g_prev_first_deriv;
|
||
abs_fd = (first_deriv >= 0) ? first_deriv : -first_deriv;
|
||
abs_sd = (second_deriv >= 0) ? second_deriv : -second_deriv;
|
||
|
||
if (g_exit_state == 0) {
|
||
/*--- ��0: �ٵ�һ��������, ����Ӧ����ƽ̹��ֵ ---*/
|
||
if (abs_fd > (int32_t)g_max_slope) g_max_slope = (uint16_t)abs_fd;
|
||
if (abs_sd > (int32_t)g_max_slope_rate) g_max_slope_rate = (uint16_t)abs_sd;
|
||
if (abs_fd < SLOPE_FLAT_THRESH) {
|
||
g_slope_flat_cnt++;
|
||
if (g_slope_flat_cnt >= 3) {
|
||
g_delta2 = g_max_slope / K1;
|
||
g_delta3 = g_max_slope_rate / K2;
|
||
if (g_delta2 < MIN_DELTA2) g_delta2 = MIN_DELTA2;
|
||
if (g_delta3 < MIN_DELTA3) g_delta3 = MIN_DELTA3;
|
||
g_exit_state = 1;
|
||
g_flat_ok_cnt = 0;
|
||
}
|
||
} else {
|
||
g_slope_flat_cnt = 0;
|
||
}
|
||
} else {
|
||
/*--- ��1: ��ֵ�ع� + һ��ƽ̹ + ����ƽ̹ ������ȷ���뿪 ---*/
|
||
int32_t dev = (int32_t)loop1_CAPVD - (int32_t)loop1_Origin;
|
||
int32_t cond1 = (dev >= 0) ? dev : -dev;
|
||
loop1_dlt_ORG = ((uint32_t)loop1_Origin * g_loop_sens_list.sens[g_freq_sens].sens_out) >> 16;
|
||
if (cond1 < (int32_t)loop1_dlt_ORG && abs_fd < (int32_t)g_delta2
|
||
&& abs_sd < (int32_t)g_delta3) {
|
||
g_flat_ok_cnt++;
|
||
if (g_flat_ok_cnt >= FLAT_CONFIRM_CNT) {
|
||
g_flat_ok_cnt = 0;
|
||
g_exit_state = 0;
|
||
loop_release_common();
|
||
}
|
||
} else {
|
||
g_flat_ok_cnt = 0;
|
||
}
|
||
}
|
||
g_prev_capvd = (int32_t)loop1_CAPVD;
|
||
g_prev_first_deriv = first_deriv;
|
||
#else
|
||
/*================================================================
|
||
* �г�״̬: ���ͻ� + �����뿪
|
||
*================================================================*/
|
||
loop1_dlt_ORG = ((uint32_t)loop1_Origin * g_loop_sens_list.sens[g_freq_sens].sens_out) >> 16;
|
||
|
||
if ((loop1_Origin - loop1_dlt_ORG) < loop1_CAPVD) {
|
||
loop1_cnt_release++;
|
||
if (loop1_cnt_release >= 3) {
|
||
loop1_cnt_release = 0;
|
||
loop_release_common();
|
||
}
|
||
} else {
|
||
if (loop1_cnt_release > 0) loop1_cnt_release = 0;
|
||
}
|
||
#endif
|
||
}
|
||
}
|
||
|
||
|
||
/*===========================================================================
|
||
* cjq_srv -- ��������ѭ������
|
||
* - �����ڳ�ʱ / ��Ȧ�Ͽ�����: ȫ��λ�ؽ�����
|
||
* (��Ȧ�Ա�ռ��ʱ, �»�������ռ��ֵ��, �̵��������ͷ� -- ���� M1H ��Ϊ)
|
||
* - CAP_OK ����ʱִ�к��ļ���㷨
|
||
*===========================================================================*/
|
||
void cjq_srv(void){
|
||
|
||
if(loop1_LOOP_OK){
|
||
if(loop1_FLAG_HOLD_TIMEOUT){
|
||
/* �����ڳ�ʱ: ȫ��λ, ���¾����ȶ����ؽ����� */
|
||
INIT_VD();
|
||
return;
|
||
}
|
||
if(loop1_FLAG_CUT){
|
||
/* ��Ȧ�Ͽ�������: ȫ��λ, ��������Ӧ���������ؽ����� */
|
||
g_loop_cut_last = 0;
|
||
INIT_VD();
|
||
return;
|
||
}
|
||
if(!loop1_LOOP_OK0){
|
||
DelayMs(200); /* ��Ȧ���������ȴ� 200ms ���������� */
|
||
loop1_LOOP_OK0 = 1;
|
||
}
|
||
if(loop1_CAP_OK){
|
||
loop1_CAP_OK = 0;
|
||
if(!g_loop_power_up_state){
|
||
g_loop_power_up_state = 1; // ��ʾ��������Ȧ
|
||
}
|
||
vd1_task();
|
||
poll_sens_key();
|
||
}
|
||
}// end if loop1_LOOP_OK
|
||
else{
|
||
loop1_FLAG_CUT = 1;
|
||
loop1_VD_FLAG = 0;
|
||
loop1_FLAG_IN = 0;
|
||
loop1_FLAG_OUT = 0;
|
||
loop1_FLAG_PLUSE = 0;
|
||
loop1_FLAG_PLUSE_IN_F = 0;
|
||
loop1_FLAG_PLUSE_IN = 0;
|
||
|
||
// ��Ȧ�Ͽ�: �̵����ͷ�, ����״̬����
|
||
RLY1_OFF; RLY2_OFF;
|
||
g_loop_acs_info.relay1_state = 0;
|
||
g_loop_acs_info.relay2_state = 0;
|
||
g_loop_acs_info.car_state = 0;
|
||
|
||
if(g_loop_cut_last != loop1_FLAG_CUT){
|
||
g_loop_cut_last = loop1_FLAG_CUT;
|
||
if(g_loop_power_up_state){
|
||
g_loop_cut_amount++;
|
||
if(g_loop_cut_amount >= 255){
|
||
g_loop_cut_amount = 255;
|
||
}
|
||
}
|
||
}
|
||
|
||
}
|
||
}
|
||
|
||
|
||
|
||
|
||
/*********************************************************************
|
||
* @fn Main_Circulation
|
||
*
|
||
* @brief ��ѭ��
|
||
*
|
||
* @return none
|
||
*/
|
||
__HIGH_CODE
|
||
__attribute__((noinline))
|
||
void Main_Circulation()
|
||
{
|
||
|
||
while(1)
|
||
{
|
||
TMOS_SystemProcess();
|
||
#ifndef DEBUG
|
||
poll_dbn_ble();
|
||
#endif
|
||
|
||
cjq_srv();
|
||
|
||
if(g_flag_bt_state == 0){
|
||
// ��������Ͽ��������ϱ���Ҫ����
|
||
if(g_dbn_ble_state_acs_enable.enable){
|
||
g_dbn_ble_state_acs_enable.enable = 0;
|
||
}
|
||
}
|
||
|
||
#ifdef DEBUG
|
||
if(g_flag_output){
|
||
uint32_t tmp;
|
||
g_flag_output = 0;
|
||
tmp = (loop1_Xn) ? (g_sys_freq / loop1_Xn) : 0;
|
||
PRINT("Xn:%d, LPCNT:%d, edges:%d, CAPVD:%d, Origin:%d, VD:%d, freq:%d\n",
|
||
loop1_Xn, loop1_LPCNT, g_xn_counter, loop1_CAPVD, loop1_Origin, loop1_VD_FLAG, tmp);
|
||
|
||
g_xn_counter = 0;
|
||
}
|
||
#endif
|
||
}
|
||
}
|
||
|
||
|
||
/*********************************************************************
|
||
* @fn main
|
||
*
|
||
* @brief ������
|
||
*
|
||
* @return none
|
||
*/
|
||
int main(void)
|
||
{
|
||
uint8_t i;
|
||
#if(defined(DCDC_ENABLE)) && (DCDC_ENABLE == TRUE)
|
||
PWR_DCDCCfg(ENABLE);
|
||
#endif
|
||
SetSysClock(CLK_SOURCE_PLL_60MHz);
|
||
#if(defined(HAL_SLEEP)) && (HAL_SLEEP == TRUE)
|
||
GPIOA_ModeCfg(GPIO_Pin_All, GPIO_ModeIN_PU);
|
||
GPIOB_ModeCfg(GPIO_Pin_All, GPIO_ModeIN_PU);
|
||
#endif
|
||
|
||
gpio_uart_init();
|
||
g_sys_freq = FREQ_SYS;
|
||
GetMACAddress(MACAddr);
|
||
for(i = 0; i < 6; i++)
|
||
{
|
||
g_dev_number[i] = MACAddr[i];
|
||
}
|
||
g_loop_sens_list.total = MAX_LOOP_SENS_AMOUNT; // max
|
||
INIT_VD();
|
||
para_store_init();
|
||
|
||
tm_init();
|
||
|
||
CH59x_BLEInit();
|
||
HAL_Init();
|
||
GAPRole_PeripheralInit();
|
||
Peripheral_Init();
|
||
Main_Circulation();
|
||
}
|
||
|
||
|
||
|
||
/* @fn TMR0_IRQHandler
|
||
*
|
||
* @brief TMR0 ���벶���ж� -- ��ȦƵ�ʲ���
|
||
* FallEdge_To_FallEdge ģʽ, CAPGetData ֱ�Ӹ��������½������� Xn
|
||
* �ۼ� LPCNT �� -> һ�������� Value (~= MEASUREMENT_BASE)
|
||
*
|
||
* @return none
|
||
*/
|
||
__INTERRUPT
|
||
__HIGH_CODE
|
||
void TMR0_IRQHandler(void) // TMR0 �����ж�
|
||
{
|
||
if(TMR0_GetITFlag(TMR0_3_IT_DATA_ACT)){
|
||
loop1_Xn = (uint16_t)TMR0_CAPGetData();
|
||
|
||
#ifdef DEBUG
|
||
g_xn_counter++;
|
||
#endif
|
||
|
||
if(loop1_Xn == 0){
|
||
R8_TMR0_INT_FLAG |= 0xff;
|
||
return;
|
||
}
|
||
|
||
if(!loop1_RF_FLAG){
|
||
loop1_RF_FLAG = 1;
|
||
}
|
||
else{
|
||
loop1_CapCnt++;
|
||
|
||
if(loop1_INI_LOOP){
|
||
/*--- ��ʼ���� ---*/
|
||
if(loop1_LPCNT == 0){
|
||
if(loop1_CapCnt > 200){ /* �ϵ�/��Ƶ��ȴ����ȶ� */
|
||
uint32_t lpcnt = (uint32_t)MEASUREMENT_BASE / loop1_Xn;
|
||
if(lpcnt == 0 || lpcnt > 65535){
|
||
lpcnt = 100; /* �쳣 Xn ���� */
|
||
}
|
||
loop1_LPCNT = (uint16_t)lpcnt;
|
||
|
||
g_freq_tmp = (float)g_sys_freq/((float)loop1_Xn);
|
||
|
||
loop1_CAPVD = 0;
|
||
loop1_CapSum = 0;
|
||
loop1_CapCnt = 0;
|
||
}
|
||
}
|
||
else{
|
||
/* ��һ��������: ֱ����Ϊ���� Origin */
|
||
loop1_CapSum += loop1_Xn;
|
||
if(loop1_CapCnt >= loop1_LPCNT){
|
||
loop1_CAPVD = loop1_CapSum;
|
||
loop1_Origin = loop1_CAPVD;
|
||
if(g_loop_acs_info.flag_change == 0){
|
||
g_loop_acs_info.loop_capvd = loop1_CAPVD;
|
||
}
|
||
loop1_INI_LOOP = 0;
|
||
loop1_CapSum = 0;
|
||
loop1_Value = 0;
|
||
loop1_CapCnt = 0;
|
||
}
|
||
}
|
||
}// end if loop1_INI_LOOP
|
||
else {
|
||
/*--- ��������: �ۼӵ������� ---*/
|
||
loop1_CapSum += loop1_Xn;
|
||
|
||
if(loop1_CapCnt >= loop1_LPCNT){
|
||
loop1_Value = loop1_CapSum;
|
||
loop1_CAP_OK = 1;
|
||
loop1_CapSum = 0;
|
||
loop1_CapCnt = 0;
|
||
}
|
||
}
|
||
}
|
||
|
||
R8_TMR0_INT_FLAG |= 0xff;
|
||
}
|
||
}
|
||
|
||
|
||
|
||
/*********************************************************************
|
||
* @fn TMR1_IRQHandler
|
||
*
|
||
* @brief TMR1 ��ʱ�ж� 5ms/per -- ʱ��״̬�� / �̵������ / LED
|
||
*
|
||
* @return none
|
||
*/
|
||
__INTERRUPT
|
||
__HIGH_CODE
|
||
void TMR1_IRQHandler(void)
|
||
{
|
||
static uint16_t _counter1_init = 0;
|
||
#ifdef DEBUG
|
||
static uint16_t _cnt = 0;
|
||
#endif
|
||
|
||
if(TMR1_GetITFlag(TMR0_3_IT_CYC_END))
|
||
{
|
||
#ifdef DEBUG
|
||
_cnt++;
|
||
if(_cnt >= 400){
|
||
g_flag_output = 1;
|
||
_cnt = 0;
|
||
}
|
||
#endif
|
||
|
||
if(g_dbn_ble_state_acs_enable.enable && g_flag_bt_state){
|
||
g_loop_acs_info.report_counter++;
|
||
}
|
||
|
||
TimingDelayInc++;
|
||
u32CountWdt++;
|
||
|
||
/*--- LED: �ȶ��� 200ms ����; ��Ȧ�Ͽ� 150ms ���� ---*/
|
||
if(loop1_FLAG_CUT == 0)
|
||
{
|
||
if(!g_loop_stable)
|
||
{
|
||
_counter1_init++;
|
||
if(_counter1_init >= 40) // 40: 200ms
|
||
{
|
||
_counter1_init = 0;
|
||
LEDA_ON_OFF();
|
||
}
|
||
}
|
||
}
|
||
else
|
||
{
|
||
_counter1_init++;
|
||
if(_counter1_init >= 30) // 30: 150ms
|
||
{
|
||
_counter1_init = 0;
|
||
LEDA_ON_OFF();
|
||
}
|
||
}
|
||
|
||
TM1cnt++;
|
||
if(TM1cnt >= 10) //about=50ms
|
||
{
|
||
TM1cnt = 0;
|
||
if(loop1_FLAG_IN) // ��Ȧ1 �������г�
|
||
{
|
||
loop1_FLAG_OUT = 0;
|
||
loop1_OUTCNT = 0;
|
||
loop1_FLAG_IN = 0;
|
||
if(g_loop_cng_unit.output_mode == 1) // ��������
|
||
{
|
||
loop1_FLAG_PLUSE_IN_F = 1;
|
||
}
|
||
loop1_INCNT = 0;
|
||
LEDA_ON;
|
||
}
|
||
|
||
if(loop1_FLAG_PLUSE_IN_F)
|
||
{
|
||
loop1_INCNT++;
|
||
if(loop1_INCNT > g_loop_out_delay)
|
||
{
|
||
loop1_INCNT = 0;
|
||
loop1_FLAG_PLUSE_IN_F = 0;
|
||
loop1_FLAG_PLUSE_IN = 1;
|
||
}
|
||
}
|
||
|
||
if(loop1_FLAG_PLUSE_IN)
|
||
{
|
||
loop1_INCNT++;
|
||
if(loop1_INCNT > IN_DELAY) // 500ms
|
||
{
|
||
loop1_INCNT = 0;
|
||
loop1_FLAG_PLUSE_IN = 0;
|
||
}
|
||
}
|
||
|
||
|
||
if(loop1_FLAG_OUT) // ���г������ı�־
|
||
{
|
||
loop1_OUTCNT++;
|
||
if(loop1_OUTCNT > g_loop_out_delay)
|
||
{
|
||
loop1_FLAG_OUT = 0;
|
||
loop1_FLAG_PLUSE = 1;
|
||
loop1_OUTCNT = 0;
|
||
LEDA_OFF;
|
||
}
|
||
}
|
||
|
||
if(loop1_FLAG_PLUSE) // Give 500ms
|
||
{
|
||
loop1_OUTCNT++;
|
||
if(loop1_OUTCNT > PLUSE_DELAY)
|
||
{
|
||
loop1_FLAG_PLUSE = 0;
|
||
loop1_OUTCNT = 0;
|
||
}
|
||
}
|
||
|
||
|
||
if(loop1_VD_HOLD)
|
||
{
|
||
Hold_CNT++;
|
||
if(Hold_CNT > g_hold_time) // ����������ʱ��
|
||
{
|
||
loop1_VD_HOLD = 0;
|
||
Hold_CNT = 0;
|
||
if(loop1_VD_FLAG)
|
||
{
|
||
/* �����ڳ�ʱ: �ͷż̵���, ��ѭ��ȫ��λ�ؽ����� */
|
||
loop1_FLAG_HOLD_TIMEOUT = 1;
|
||
RLY1_OFF; RLY2_OFF;
|
||
g_loop_acs_info.relay1_state = 0;
|
||
g_loop_acs_info.relay2_state = 0;
|
||
}
|
||
}
|
||
}
|
||
|
||
if(loop1_LC_HOLD)
|
||
{
|
||
LC_Hold_CNT++;
|
||
if(LC_Hold_CNT > g_safe_max_cnt)
|
||
{
|
||
/* ��ȫ��λ: ���³�ʼ������, �ؽ����� (���� M1H) */
|
||
loop1_LC_Reset = 1;
|
||
loop1_INI_LOOP = 1;
|
||
loop1_LOOP_OK0 = 0;
|
||
g_loop_stable = 0;
|
||
loop1_stable_cnt = 0;
|
||
LC_Hold_CNT = 0;
|
||
loop1_ORG_CNT = 0;
|
||
loop1_ORG_SUM = 0;
|
||
#if USE_FLATNESS_EXIT
|
||
g_exit_state = 0;
|
||
g_max_slope = 0;
|
||
g_max_slope_rate = 0;
|
||
g_slope_flat_cnt = 0;
|
||
g_flat_ok_cnt = 0;
|
||
#endif
|
||
}
|
||
}
|
||
}
|
||
|
||
/*--- ��Ȧ�ز���� & �̵������ˢ�� (ÿ 5ms) ---*/
|
||
if(loop1_RF_FLAG)
|
||
{
|
||
loop1_LOOP_OK = 1;
|
||
loop1_RF_FLAG = 0;
|
||
|
||
if(loop1_LC_Reset == 0)
|
||
{
|
||
switch(g_loop_cng_unit.output_mode)
|
||
{ // �����ʽ
|
||
case 0: // �������
|
||
{
|
||
if(loop1_VD_FLAG | loop1_FLAG_OUT)// ��������ź�
|
||
{
|
||
RLY1_ON;
|
||
g_loop_acs_info.relay1_state = 1;
|
||
RLY2_ON;
|
||
g_loop_acs_info.relay2_state = 1;
|
||
}
|
||
else
|
||
{
|
||
RLY1_OFF;
|
||
g_loop_acs_info.relay1_state = 0;
|
||
RLY2_OFF;
|
||
g_loop_acs_info.relay2_state = 0;
|
||
}
|
||
} break;
|
||
case 1: // ��������
|
||
{
|
||
if(loop1_VD_FLAG) // �̵���2�����ź�
|
||
{
|
||
RLY2_ON;
|
||
g_loop_acs_info.relay2_state = 1;
|
||
}
|
||
else
|
||
{
|
||
RLY2_OFF;
|
||
g_loop_acs_info.relay2_state = 0;
|
||
}
|
||
|
||
if(loop1_FLAG_PLUSE_IN)
|
||
{
|
||
RLY1_ON;
|
||
g_loop_acs_info.relay1_state = 1;
|
||
}
|
||
else
|
||
{
|
||
RLY1_OFF;
|
||
g_loop_acs_info.relay1_state = 0;
|
||
}
|
||
} break;
|
||
case 2: // �뿪����
|
||
{
|
||
if(loop1_VD_FLAG) // �̵���2�����ź�
|
||
{
|
||
RLY2_ON;
|
||
g_loop_acs_info.relay2_state = 1;
|
||
}
|
||
else
|
||
{
|
||
RLY2_OFF;
|
||
g_loop_acs_info.relay2_state = 0;
|
||
}
|
||
|
||
if(loop1_FLAG_PLUSE) // ����뿪����
|
||
{
|
||
RLY1_ON;
|
||
g_loop_acs_info.relay1_state = 1;
|
||
}
|
||
else
|
||
{
|
||
RLY1_OFF;
|
||
g_loop_acs_info.relay1_state = 0;
|
||
}
|
||
} break;
|
||
default: {
|
||
if(loop1_VD_FLAG) // �̵���2�����ź�
|
||
{
|
||
RLY2_ON;
|
||
g_loop_acs_info.relay2_state = 1;
|
||
}
|
||
else
|
||
{
|
||
RLY2_OFF;
|
||
g_loop_acs_info.relay2_state = 0;
|
||
}
|
||
} break;
|
||
}
|
||
}// end if loop1_LC_Reset == 0
|
||
} // end if loop1_RF_FLAG
|
||
else
|
||
{
|
||
loop1_LOOP_OK0 = loop1_LOOP_OK;
|
||
loop1_LOOP_OK = 0;
|
||
}
|
||
|
||
|
||
|
||
TMR1_ClearITFlag(TMR0_3_IT_CYC_END); // ����жϱ�־
|
||
}
|
||
}
|
||
|
||
|
||
|
||
|
||
/******************************** endfile @ main ******************************/
|