使用HC32L130F8UA测量脉冲宽度,测量标准,每过1分钟测量三次脉冲宽度,并计算第2次和第3次平均值储存
时间: 2023-10-08 11:09:27 浏览: 55
实现这个功能需要涉及到HC32L130F8UA的定时器、外部中断和计算平均值的方法。下面是一个简单的代码框架,你可以根据自己的具体需求进行修改和完善。
```c
#include "hc32l130.h"
/* 定义计数变量 */
volatile uint32_t cnt = 0;
/* 定义脉冲宽度变量和标志位 */
volatile uint32_t pulse_width = 0;
volatile uint8_t pulse_flag = 0;
/* 定义存储变量 */
volatile uint32_t pulse_sum = 0;
volatile uint8_t pulse_count = 0;
/* 定义定时器中断处理函数 */
void TIM_IRQHandler(void)
{
/* 判断是否为计数溢出中断 */
if (Set == TMRA_GetFlag(M0P_TMRA_1, TMRA_FLAG_OVF))
{
TMRA_ClearFlag(M0P_TMRA_1, TMRA_FLAG_OVF);
/* 计数器加1 */
cnt++;
}
}
/* 定义外部中断处理函数 */
void EXINT01_IRQHandler(void)
{
/* 判断是否为上升沿触发 */
if (Set == EXINT_GetExIntSrc(EXINT_CH01))
{
/* 获取脉冲宽度 */
pulse_width = TMRA_GetCntVal(M0P_TMRA_2);
/* 清除计数器 */
TMRA_SetCntVal(M0P_TMRA_2, 0);
/* 设置脉冲标志位 */
pulse_flag = 1;
}
/* 清除中断标志位 */
EXINT_ClrExIntSrc(EXINT_CH01);
}
/* 定义计算平均值函数 */
void calc_average(void)
{
/* 判断是否需要进行计算 */
if (pulse_count == 2)
{
/* 计算平均值 */
pulse_sum = (pulse_sum + pulse_width) / 2;
/* 清空计数器和标志位 */
pulse_count = 0;
pulse_flag = 0;
}
else if (pulse_flag == 1)
{
/* 累加脉冲宽度和计数器 */
pulse_sum += pulse_width;
pulse_count++;
pulse_flag = 0;
}
}
int main(void)
{
/* 初始化定时器 */
TMRA_InitTypeDef tmra_init;
tmra_init.u32ClkSrc = TMRA_CLK_SRC_CLK_HCLK;
tmra_init.u32ClkDiv = TMRA_CLK_DIV1;
tmra_init.u32PeriodVal = SystemCoreClock / 1000 - 1;
tmra_init.u16CntMode = TMRA_MODE_SAWTOOTH;
TMRA_Init(M0P_TMRA_1, &tmra_init);
TMRA_EnableIrq(M0P_TMRA_1, TMRA_IRQ_EN_OVF);
/* 初始化外部中断 */
stc_exint_config_t exint_config;
exint_config.u32ExIntCh = EXINT_CH01;
exint_config.u8ExIntFE = EXINT_FILTER_DISABLE;
exint_config.u8ExIntFClk = EXINT_FCLK_HCLK_DIV8;
exint_config.u8ExIntLvl = EXINT_TRIGGER_RISING;
exint_config.pfnExintCallback = EXINT01_IRQHandler;
EXINT_Init(&exint_config);
/* 初始化计数器 */
cnt = 0;
/* 启动定时器 */
TMRA_Start(M0P_TMRA_1);
while (1)
{
/* 判断是否满足计算平均值的条件 */
if (cnt >= 60000)
{
/* 停止定时器和计数器 */
TMRA_Stop(M0P_TMRA_1);
cnt = 0;
/* 计算平均值 */
calc_average();
/* 启动定时器和计数器 */
TMRA_Start(M0P_TMRA_1);
}
}
}
```
这段代码的主要流程是,在定时器中断中进行计数,并在外部中断中获取脉冲宽度。当计数器累计到60秒时,停止定时器和计数器,计算第2次和第3次的平均值。然后再启动定时器和计数器,继续进行下一轮测量。需要注意的是,计算平均值的函数需要在主循环中调用,否则会影响定时器和外部中断的正常运行。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)