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

下面对生成的代码进行解读。
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)) {}
/* 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


通过quick panel进入CAOSEBSE Tuner

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

选择I2C,其他参数如上。然后依次点击连接、运行,就可以进行调试了。
下面是一些截图,大家自行摸索吧。


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

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

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

取消I2C配置,这个和UART冲突了(P2_2、P2_3)。
将SCB_0配置为UART-1.0

配置一个921.4的时钟用于UART

UART的配置,时钟,RX、TX

修改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

测量农夫山泉的水位

UART打印出结果:
