Răsfoiți Sursa

1添加低功耗定时器用于启动采样, 每秒除rtc中断的采样一次外,其他采样由lptim0的中断启动
2. 修改main中设置rtc的中断时间,不再包含0.5秒中断的情况, 计量和静水状态统一用1秒一次, 计量状态一秒3次,静水1秒2次

wxl 2 săptămâni în urmă
părinte
comite
e95e6ef153
5 a modificat fișierele cu 218 adăugiri și 9 ștergeri
  1. 5 2
      Users/main.c
  2. 176 0
      Users/pwm/pwm.c
  3. 23 0
      Users/pwm/pwm.h
  4. 5 0
      Users/rtc_isr.c
  5. 9 7
      Users/tdc_tof/tof_isr.c

+ 5 - 2
Users/main.c

@@ -32,6 +32,7 @@
 #include "rtc_run.h"
 #include "rtc_run.h"
 #include "stat.h"
 #include "stat.h"
 #include "ymodem.h"
 #include "ymodem.h"
+#include "pwm.h"
 /******************************************************************************
 /******************************************************************************
  * Local pre-processor symbols/macros ('#define')
  * Local pre-processor symbols/macros ('#define')
  ******************************************************************************/
  ******************************************************************************/
@@ -143,9 +144,9 @@ void PeriodProc(void) // 
         if (!alarm_status(ERR_BLANK_PIPE)) // 非空管状态
         if (!alarm_status(ERR_BLANK_PIPE)) // 非空管状态
         {
         {
             // 不是空管,设置RTC中断0.5秒1次
             // 不是空管,设置RTC中断0.5秒1次
-            if ((get_rtc_interrupt_time() == 2) || (get_rtc_interrupt_time() == 1))
+            if (get_rtc_interrupt_time() != 1)
             {
             {
-                set_rtc_interrupt_time(0.5); // 0.5
+                set_rtc_interrupt_time(1); // 0.5
 //							  c_step=1;
 //							  c_step=1;
             }
             }
         }
         }
@@ -590,6 +591,8 @@ int32_t main(void)
     LOG("------------TFUW001------------\n");
     LOG("------------TFUW001------------\n");
     ch_num = GetChNum();
     ch_num = GetChNum();
     tof_filter_init(ch_num); // 20260429
     tof_filter_init(ch_num); // 20260429
+		
+		 LpTim0Init(); // 初始化低功耗定时器,用于tdc wxl2026-11-19
     LOG("ch_num     = %d\n", ch_num);
     LOG("ch_num     = %d\n", ch_num);
     //  LOG("TdcItf.ch_num     = %d\n", TdcItf.ch_num);
     //  LOG("TdcItf.ch_num     = %d\n", TdcItf.ch_num);
     //  LOG("pTdcItf->ch_num     = %d\n", pTdcItf->ch_num);
     //  LOG("pTdcItf->ch_num     = %d\n", pTdcItf->ch_num);

+ 176 - 0
Users/pwm/pwm.c

@@ -0,0 +1,176 @@
+#include "board.h"
+#include "tdc_tof.h"
+#include "alarm.h"
+#include <rtthread.h>
+
+/* ========= 全局嵌套引用计数:LPTIM ========= */
+static uint32_t LpTim_ClkNest = 0U;
+uint8_t tdc_remain_int_cnt = 0;
+/**
+ * @brief  请求打开低功耗定时器时钟
+ * @note  嵌套计数,关中断保护,RT-Thread
+ */
+void LpTim_ReqClkOn(void)
+{
+    rt_base_t level;
+    level = rt_hw_interrupt_disable();
+    LpTim_ClkNest++;
+    M0P_SYSCTRL->PERI_CLKEN0_f.LPTIM0 = 1;
+    rt_hw_interrupt_enable(level);
+}
+
+/**
+ * @brief  释放低功耗定时器时钟
+ * @note 计数到0,才真正关闭外设时钟
+ */
+void LpTim_ReqClkOff(void)
+{
+    rt_base_t level;
+    level = rt_hw_interrupt_disable();
+    if (LpTim_ClkNest > 0U)
+    {
+        LpTim_ClkNest--;
+    }
+    if (LpTim_ClkNest == 0U)
+    {
+        M0P_SYSCTRL->PERI_CLKEN0_f.LPTIM0 = 0;
+    }
+    rt_hw_interrupt_enable(level);
+}
+
+/**
+ * @brief LPTIM 初始化,计时周期为10ms,使用外部32.768kHz时钟(PC14)
+ * @note 只做硬件参数初始化,不会启动定时器;调用LpTim_10ms_Start()才开始计时
+ */
+#define LPTIM_PERIOD_500MS (65536 - 16384) // 50ms
+#define LPTIM_PERIOD_50MS (65536 - 1638) // 50ms
+#define LPTIM_PERIOD_45MS (65536 - 1474) // 45ms
+#define LPTIM_PERIOD_40MS (65536 - 1310) // 40ms
+#define LPTIM_PERIOD_30MS (65536 - 983)  // 30ms
+#define LPTIM_PERIOD_20MS (65536 - 655)  // 20ms
+#define LPTIM_PERIOD_10MS (65536 - 327)  // 10ms
+#define LPTIM_PERIOD_1MS (65536 - 32)    // 1ms
+
+#define LPTIM_PREIOD_MS LPTIM_PERIOD_40MS
+void LpTim0Init(void)
+{
+    rt_base_t level;
+
+    /* 1.配置PC14引脚复用为LPTIM的外部时钟输入 */
+    //   GpioInit(PINFUNC_PC14_LPTIM_CLK, 0U);
+
+    /* 2.请求时钟,配置寄存器前保证外设时钟打开 */
+    LpTim_ReqClkOn();
+
+    level = rt_hw_interrupt_disable();
+
+    /* LPTIM_CON: 外部时钟源,向上计数,先关闭计数器 */
+    M0P_LPTIMER0->CR_f.PRS = 0;
+    M0P_LPTIMER0->CR_f.GATE = 0;
+
+    M0P_LPTIMER0->CR_f.TCK_SEL = 2; /* 选择外部时钟 */
+    M0P_LPTIMER0->CR_f.CT = 0;      /* 定时器功能 */
+    M0P_LPTIMER0->CR_f.MD = 1;      /* 无重装载*/
+
+    M0P_LPTIMER0->CR_f.TR = 0; /* 关闭计数器 */
+
+    M0P_LPTIMER0->CNT_f.CNT = 0;               /* CNTER计数初值=0 */
+    M0P_LPTIMER0->ARR_f.ARR = LPTIM_PREIOD_MS; /* PERAR周期寄存器,10ms */
+
+    /* 清除可能残留的中断标志并开启匹配中断 */
+    M0P_LPTIMER0->ICLR_f.TFC = 1; /* 清除中断标志 */
+    M0P_LPTIMER0->CR_f.IE = 1;    /* 使能匹配中断 */
+
+    rt_hw_interrupt_enable(level);
+
+    /* 初始化完成,不运行,释放时钟;后续Start会重新ReqClkOn */
+    IRQMutEnable(LPTIM_0_1_IRQn, NVIC_PRIO_3);
+    // LpTim_ReqClkOff();
+}
+	uint16_t ntm;
+/**
+ * @brief 开启10ms计时,允许中断
+ */
+void LpTim0Start(uint16_t tim)
+{
+	
+
+	if(tim > 16384)ntm=LPTIM_PERIOD_500MS;//最大500ms
+	else if(tim < 1310)ntm = LPTIM_PERIOD_40MS;//最小45ms
+	else  ntm = 65536-tim;
+	
+    rt_base_t level;
+    LpTim_ReqClkOn();
+
+    level = rt_hw_interrupt_disable();
+	   M0P_LPTIMER0->CR_f.TR = 0;
+	  M0P_LPTIMER0->ARR_f.ARR = ntm;
+    M0P_LPTIMER0->CR_f.IE = 1;
+    M0P_LPTIMER0->CR_f.TR = 1;
+		
+    rt_hw_interrupt_enable(level);
+}
+
+/**
+ * @brief 关闭10ms计时
+ * @note 停止计数器,释放LPTIM时钟,PC14切回GPIO高阻输入,降低漏电流
+ */
+void LpTim0Stop(void)
+{
+    rt_base_t level;
+
+    level = rt_hw_interrupt_disable();
+    /* 停止计数器TR=0 */
+    M0P_LPTIMER0->CR_f.IE = 0;
+    // M0P_LPTIMER0->CNT_f.CNT = 0;
+    M0P_LPTIMER0->CR_f.TR = 0;
+
+    rt_hw_interrupt_enable(level);
+
+    /* 释放低功耗定时器时钟 */
+    LpTim_ReqClkOff();
+}
+void set_samp_cnt(int8_t cnt) // 设置每秒中断次数
+{
+    if (cnt <= 0)
+        return;
+    if (tdc_remain_int_cnt == 0)
+    {
+        tdc_remain_int_cnt = cnt;
+    }
+		
+		uint16_t tim_cnt;
+		tim_cnt = (32768-3276)/(tdc_remain_int_cnt+1);
+		
+    LpTim0Start(tim_cnt); // 如果tdc_remain_int_cnt本身不为零, 可能定时器停止了,需要再开启,完成剩余的中断
+}
+uint8_t lptcnt = 0;
+uint8_t lptnum;
+/**
+ * @brief LPTIM 中断服务函数
+ * @note 10ms计时到达中断
+ */
+void LPTIM0_1_IRQHandler(void)
+{
+    rt_base_t level;
+
+    level = rt_hw_interrupt_disable();
+    /* 检查并清除中断标志 */
+    if (M0P_LPTIMER0->IFR_f.TF)
+    {
+        M0P_LPTIMER0->ICLR_f.TFC = 0;
+    }
+    rt_hw_interrupt_enable(level);
+    ///////////////根据剩余采集数启动tdc///////////////////////
+    if (tdc_remain_int_cnt != 0) // 如果还有剩余采集次数
+    {
+        tdc_remain_int_cnt--;
+
+        tdc_start_tof(TDC_CFG_DO_CKENA | TDC_CFG_DO_CKDIS | TDC_CFG_DO_WREG);
+    }
+    else
+    {
+        LpTim0Stop(); // 停止定时器
+    }
+    // lptcnt++;
+}

+ 23 - 0
Users/pwm/pwm.h

@@ -0,0 +1,23 @@
+//pwm.h
+#ifndef __PWM_H__
+#define __PWM_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * @brief LPTIM ³õʼ»¯£¬¼ÆÊ±ÖÜÆÚΪ10ms
+ */
+extern void LpTim0Init(void);
+extern void LpTim0Start(uint16_t);
+extern void LpTim0Stop(void);
+extern void set_samp_cnt(int8_t);
+extern uint8_t lptcnt;
+extern uint8_t lptnum;
+extern uint8_t tdc_remain_int_cnt;
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __PWM_H__ */

+ 5 - 0
Users/rtc_isr.c

@@ -2,6 +2,7 @@
 #include "board.h"
 #include "board.h"
 #include "tdc_tof.h"
 #include "tdc_tof.h"
 #include "alarm.h"
 #include "alarm.h"
+#include "pwm.h"//添加定时器头文件 wxl2026-11-19
 
 
 static rtc_run_t *pRtcRun;
 static rtc_run_t *pRtcRun;
 void rtc_init(rtc_run_t *rtc_run)
 void rtc_init(rtc_run_t *rtc_run)
@@ -271,5 +272,9 @@ void rtc_interrupt_deal_tdc(void)
             tdc_start_tof(TDC_CFG_DO_CKENA | TDC_CFG_DO_CKDIS | TDC_CFG_DO_WREG);
             tdc_start_tof(TDC_CFG_DO_CKENA | TDC_CFG_DO_CKDIS | TDC_CFG_DO_WREG);
             TdcIntStep = 0x03u;
             TdcIntStep = 0x03u;
         }
         }
+				/////////////////启动定时器并设置中断次数//////////////////////
+				extern uint8_t TDC_SAMPLE_TIMES_PER_RTC_INT;
+				set_samp_cnt(TDC_SAMPLE_TIMES_PER_RTC_INT-1);//设置接下来定时器中断次数,等于采样次数-1  wxl2026-11-19
+				////////////////////////////////////
     }
     }
 }
 }

+ 9 - 7
Users/tdc_tof/tof_isr.c

@@ -3,6 +3,8 @@
 #include "alarm.h"
 #include "alarm.h"
 #include "check.h"
 #include "check.h"
 
 
+
+
 #define TOF_PWFW_DIFF ((uint16_t)(0x1620u * 25 / 100))
 #define TOF_PWFW_DIFF ((uint16_t)(0x1620u * 25 / 100))
 #define TOF_PWFW_LOW ((uint16_t)(0x1620u * 40 / 100))
 #define TOF_PWFW_LOW ((uint16_t)(0x1620u * 40 / 100))
 #define TOF_PWFW_HIGH ((uint16_t)(0x1620u * 50 / 100))
 #define TOF_PWFW_HIGH ((uint16_t)(0x1620u * 50 / 100))
@@ -322,7 +324,7 @@ void read_tof_and_convert_to_flow(void)
             }
             }
             else
             else
             {
             {
-                TDC_SAMPLE_TIMES_PER_RTC_INT = 1;//add by yuewei 261015,有水小于始动流量->流采集频次0.5秒1次
+                TDC_SAMPLE_TIMES_PER_RTC_INT = 2;//add by yuewei 261015,有水小于始动流量->流采集频次0.5秒1次
             }
             }
         }
         }
 
 
@@ -385,12 +387,12 @@ void tof_isr_convert_tof_to_flow(void)
             TdcIntStep = 0xFFu;
             TdcIntStep = 0xFFu;
             TDC_SAMPLE_CNT = 0;
             TDC_SAMPLE_CNT = 0;
         }
         }
-        else
-        {
-            tdc_start_tof(TDC_CFG_DO_CKENA | TDC_CFG_DO_CKDIS | TDC_CFG_DO_WREG);
-            //us_delay(500);
-            TdcIntStep = 0x03u;
-        }
+//        else//由定时器启动下一次中断
+//        {
+//            tdc_start_tof(TDC_CFG_DO_CKENA | TDC_CFG_DO_CKDIS | TDC_CFG_DO_WREG);
+//            //us_delay(500);
+//            TdcIntStep = 0x03u;
+//        }
         break;
         break;
 
 
     case (0x80u):
     case (0x80u):