打印
[PSOC™]

【英飞凌PSOC 4000T DIY】2、基于CapSense的液位检测实验

[复制链接]
142|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主

一、液位检测DEMO

CY8CPROTO-040T-MS提供了基于CapSense的液位检测应用,可以通过project creater拉取

7.png

下面对生成的代码进行解读。

1. 初始化和设置

/* Initialize the device and board peripherals */
result = cybsp_init();

/* Board init failed. Stop program execution */
if (result != CY_RSLT_SUCCESS)
{
    CY_ASSERT(CY_ASSERT_FAILED);
}
  • 初始化开发板和外围设备
/* Enable global interrupts */
__enable_irq();
  • 启用全局中断
/* Initialize EZI2C */
initialize_capsense_tuner();

/* Initialize MSC CAPSENSE */
initialize_capsense();
  • 初始化EZI2C接口(用于与CapSense Tuner通信)
  • 初始化MSC (Mutual-Sensing Capacitance) CapSense功能

2. 主循环

主循环中执行以下操作:

/* Scan the normal Liquid Level Widget */
Cy_CapSense_ScanWidget(CY_CAPSENSE_LIQUIDLEVEL0_WDGT_ID, &cy_capsense_context);
/* Wait until the scan is finished */
while(Cy_CapSense_IsBusy(&cy_capsense_context)) {}
  • 扫描常规液位部件(widget)
  • 等待扫描完成
/* Scan the Foam Rejection Widget */
Cy_CapSense_ScanWidget(CY_CAPSENSE_LIQUIDLEVEL0_FR_WDGT_ID, &cy_capsense_context);
/* Wait until the scan is finished */
while(Cy_CapSense_IsBusy(&cy_capsense_context)) {}
  • 扫描带泡沫抑制功能的液位部件
  • 等待扫描完成
/* Process all th widgets */
Cy_CapSense_ProcessAllWidgets(&cy_capsense_context);
  • 处理所有部件的数据
/* Send capsense data to the Tuner */
Cy_CapSense_RunTuner(&cy_capsense_context);
  • 将CapSense数据发送到Tuner工具(用于调试和可视化)
/* store the liquid level before and after foam rejection */
level_wo_FR = CY_CAPSENSE_LIQUIDLEVEL0_PTRPOSITION_VALUE->x;
level_w_FR = CY_CAPSENSE_LIQUIDLEVEL0_FR_PTRPOSITION_VALUE->x;
  • 存储泡沫抑制前和泡沫抑制后的液位值

二、使用CAOSEBSE Tuner进行实验观察和调试

为了和CAOSEBSE Tuner通信,SW2需拨到I2C

9.png

8.png

通过quick panel进入CAOSEBSE Tuner

5.png

上面最左边有3个图标,第一是配置,第二个是连接,第三个是运行,点击配置弹出:

4.png

选择I2C,其他参数如上。然后依次点击连接、运行,就可以进行调试了。

下面是一些截图,大家自行摸索吧。2.png

3.png

6.png

下面是校准界面,校准后可以保存到工程中,为下一次编译做准备。

1.png

简单的利用这个工具,进行液位测量

1.jpg

三、增加串口打印液位结果功能

Device Configurator中

10.png

取消I2C配置,这个和UART冲突了(P2_2、P2_3)。

将SCB_0配置为UART-1.0

11.png

配置一个921.4的时钟用于UART

12.png

UART的配置,时钟,RX、TX

13.png

修改main.c

将所有了capsense_tuner 、i2c有关的部分注释掉,不再用TUNER调试了,将P2_2、P2_3分配给UART

改完的main

int main(void)
{
    cy_rslt_t result;
    cy_stc_scb_uart_context_t CYBSP_UART_context;
    char buf[50];

    /* Initialize the device and board peripherals */
    result = cybsp_init();

    /* Board init failed. Stop program execution */
    if (result != CY_RSLT_SUCCESS)
    {
        CY_ASSERT(CY_ASSERT_FAILED);
    }

    /* Configure and enable the UART peripheral */
    Cy_SCB_UART_Init(scb_0_HW, &scb_0_config, &CYBSP_UART_context);
    Cy_SCB_UART_Enable(scb_0_HW);

    /* Enable global interrupts */
    __enable_irq();

    /* Send a string over serial terminal */
    Cy_SCB_UART_PutString(scb_0_HW, "UART INITr\n");

    /* Initialize EZI2C */
    //initialize_capsense_tuner();

    /* Initialize MSC CAPSENSE */
    initialize_capsense();

    for (;;)
    {
        uint32_t level_w_FR, level_wo_FR;

        /* Scan the normal Liquid Level Widget */
        Cy_CapSense_ScanWidget(CY_CAPSENSE_LIQUIDLEVEL0_WDGT_ID, &cy_capsense_context);
        /* Wait until the scan is finished */
        while(Cy_CapSense_IsBusy(&cy_capsense_context)) {}

        /* Scan the Foam Rejection Widget */
        Cy_CapSense_ScanWidget(CY_CAPSENSE_LIQUIDLEVEL0_FR_WDGT_ID, &cy_capsense_context);
        /* Wait until the scan is finished */
        while(Cy_CapSense_IsBusy(&cy_capsense_context)) {}

        /* Process all th widgets */
        Cy_CapSense_ProcessAllWidgets(&cy_capsense_context);

        /* Send capsense data to the Tuner */
        //Cy_CapSense_RunTuner(&cy_capsense_context);

        /* store the liquid level before and after foam rejection */
        level_wo_FR = CY_CAPSENSE_LIQUIDLEVEL0_PTRPOSITION_VALUE->x;
        level_w_FR = CY_CAPSENSE_LIQUIDLEVEL0_FR_PTRPOSITION_VALUE->x;

        sprintf(buf,"level_wo_FR=%ld,level_w_FR=%ld\r\n",level_wo_FR,level_w_FR);
        Cy_SCB_UART_PutString(scb_0_HW, buf);
        /* keep the compiler happy */
        (void)level_wo_FR;
        (void)level_w_FR;

    }
}

其中:

    Cy_SCB_UART_Init(scb_0_HW, &scb_0_config, &CYBSP_UART_context);
    Cy_SCB_UART_Enable(scb_0_HW);

初始化UART

        sprintf(buf,"level_wo_FR=%ld,level_w_FR=%ld\r\n",level_wo_FR,level_w_FR);
        Cy_SCB_UART_PutString(scb_0_HW, buf);

打印常规液位检测和带泡沫抑制的液位检测值。

将SW2拨到UART

14.png

测量农夫山泉的水位

3.jpg

UART打印出结果:

15.png

使用特权

评论回复
沙发
huquanz711| | 2025-4-15 07:29 | 只看该作者
这种检测原理实际应用价值不大,太局限了。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

54

主题

110

帖子

0

粉丝