1.前言
(1)cyprees芯片主要是可以做一些触摸的检测并实现一些IO输出,使用的工具psoc creater
(2)psoc creater 可以i直接通过GUI的方式配置一些GPIO的状态以及集成的功能模块,编译后,我们可直接通过include 对应的头文件后,可在主函数中直接调用相关的接口,基本不怎么编写代码了
2.工程使用逻辑
-
如下图为工程主界面:
-
- topDesign:可以从右边拖一些关键的功能过来,并配置一些基础参数
-
- PIN: 对一些功能进行功能进行对应的管脚进行对应
- PIN: 对一些功能进行功能进行对应的管脚进行对应
-
对上图进行详细介绍说明的
– 一个capsense 进行触摸检测
– 6个led灯 -
capsense的配置截图如下:
-
led的配置如下: 注意不要勾选HW connection
-
点击生成:
3.主函数逻辑
完成如上配置后,点击编译后会出现自动声场相关功能模块,我们需主要在main函数中进行编辑即可:
#include "CapSense.h"
#include "project.h"
#define LED_ON (0u)
#define LED_OFF (1u)
void LED_Write(uint8 value);
uint8 LED_Read(void);
uint8 LED1_Read(void);
void LED1_Write(uint8 value);
uint8 LED2_Read(void);
void LED2_Write(uint8 value);
uint8 LED3_Read(void);
void LED3_Write(uint8 value);
typedef enum
{
SENSOR_SCAN = 0x01u,
WAIT_FOR_SCAN_COMPLETE = 0x02u,
PROCESS_DATA = 0x03u,
} DEVICE_STATE;
int main(void)
{
CyGlobalIntEnable; /* Enable global interrupts. */
/* Place your initialization/startup code here (e.g. MyInst_Start()) */
/* Start CapSense block - Initializes CapSense Data structure and
performs first scan to set up sensor baselines */
DEVICE_STATE currentState = SENSOR_SCAN;
CapSense_Start();
/* Scan all widgets */
//CapSense_ScanAllWidgets();
for(;;)
{
/* Place your application code here. */
/* Place your application code here. */
/* Do this only when a scan is done */
/* Process all widgets */
CapSense_ProcessAllWidgets();
/* Include Tuner */
// CapSense_RunTuner();
/* Scan result verification */
if (CapSense_IsAnyWidgetActive())
{
/* Add any required functionality
based on scanning result*/
}
/* Start next scan */
switch(currentState)
{
case SENSOR_SCAN:
/* Initiate new scan only if the CapSense block is idle */
if(CapSense_NOT_BUSY == CapSense_IsBusy())
{
#if ENABLE_TUNER
/* Update CapSense parameters set via CapSense tuner before the
beginning of CapSense scan
*/
CapSense_RunTuner();
#endif
/* Scan widget configured by CSDSetupWidget API */
CapSense_ScanAllWidgets();
/* Set next state to WAIT_FOR_SCAN_COMPLETE */
currentState = WAIT_FOR_SCAN_COMPLETE;
}
break;
case WAIT_FOR_SCAN_COMPLETE:
/* Put the device to CPU Sleep until CapSense scanning is complete*/
if(CapSense_NOT_BUSY != CapSense_IsBusy())
{
CySysPmSleep();
}
/* If CapSense scanning is complete, process the CapSense data */
else
{
currentState = PROCESS_DATA;
}
break;
case PROCESS_DATA:
/* Process data on all the enabled widgets */
CapSense_ProcessAllWidgets();
/* Controls LEDs Status based on the result of Widget processing. */
//LED_Control();
LED_Write(CapSense_IsWidgetActive(CapSense_BUTTON0_WDGT_ID ) ? LED_OFF : LED_ON );
LED1_Write(CapSense_IsWidgetActive(CapSense_BUTTON1_WDGT_ID ) ? LED_OFF : LED_ON );
LED2_Write(CapSense_IsWidgetActive(CapSense_BUTTON2_WDGT_ID ) ? LED_OFF : LED_ON );
LED3_Write(CapSense_IsWidgetActive(CapSense_BUTTON3_WDGT_ID ) ? LED_OFF : LED_ON );
LED4_Write(CapSense_IsWidgetActive(CapSense_BUTTON4_WDGT_ID ) ? LED_ON : LED_OFF );
LED5_Write(CapSense_IsWidgetActive(CapSense_BUTTON5_WDGT_ID ) ? LED_ON : LED_OFF );
/* Set the device state to SENSOR_SCAN */
currentState = SENSOR_SCAN;
break;
/*******************************************************************
* Unknown power mode state. Unexpected situation.
******************************************************************/
default:
break;
}
//CyDelay(10);
}
}
4.程序下载
- 编译工程完成HEX文件
- 打开psoc programmer 软件,选择对应的芯片iC,选中hex文件,点击下载:
- 使用效果如下
- 按下去的时候灯光灭掉,松手灯光亮起来
结论
本文通过查找简单的工程代码实现了一个触摸控制灯光亮灭场景。如有任何问题评论区交流。