
Cypress(赛普拉斯)电容式感应(CapSense)触摸按键应用笔记(工程配置+功能调校+IIC通讯)
文章目录前言一、CapSense技术1.基本原理2.感应方式(1)自电容感应(CSD)(2)互电容感应(CSX)3.信噪比4.CapSense Widget5.防水特性二、CapSense CSD 感应方式1.引入库2.读入数据总结前言电容式触摸感应技术(CapSense)正逐步进入移动电话、个人电脑、消费类电子产品、汽车和白色家电等产品的工业设计里。与传统的机械按键相比,CapSense具有更加
Contents
Abstract
CapSense technology is more and more popular in the product design of mobile devices、personal computer、consumer electronics、auto industry and major appliance. It is more reliable and easily use than traditional Mechanical keys.Meanwhile, Cypress provide a series of solution, you can get started quickly,Using flexible, reduce development cycles Significantly.Get more details information through Cypress website.
In this article, you can get methods that the configuration of PSoC project、CapSense parameters calibration、establish communication to GD32.
Key words: Create PSoC Project 、PSoC Programmer 、Tuner、GD32 、IIC
一、Development tools
1.Chip:CY8C4014LQI-422
2.Downloader:CYPRESS MiniProg3
3.Compiler:PSoC Creator 4.4
4.Download Software:PSoC Programmer 3.29.1
二、Create PSoC Project
1.Launch PSoC Creator 4.4, 【File】 — 【New】 — 【Project】
2.Select project type, 【PSoC 4】 — 【CY8C4014LQI-422】 — 【Next】
3.Select project template, 【Enpty schematic】 — 【Next】
4.Create Project,【Finish】
三、PSoC Project Configuration
1.Add 【EZI2C Slave (SCB mode) [v4.0]】, select and drag it to 【TopDesign.cysch】 work space:
2.Configure the EZI2C, double-click the EZI2C module and Configure it as follow picture:
Note: You can get more details about parameters through Datasheet
3.Add CapSense [v7.0], select and drag it out to 【TopDesign.cysch】 work space:
4.Configure CapSense, double-click the CapSense module and Configure it as follow picture:
【Basic】
【Advanced 】 — 【General 】
【Advanced 】 — 【CSD Settings】
【Advanced 】 — 【Widget Details】
Sense clock frequency:It is recommended to set maximum value.
Scan resolution:The higher the value, the more sensitive it is.Set the value fallow the system need.
Finger threshold:It is recommended to set the Finger threshold parameter value equal to the 80% of the touch signal.
Noise threshold:It is recommended to set the noise threshold parameter value equal to 2x noise in the raw count or the 40% of the signal.
Negative noise threshold:It is recommended to set the negative noise threshold parameter value equal to the Noise threshold parameter value.
Low baseline reset:The recommended value is 30, which works for most designs.
Hysteresis:The recommend value for the hysteresis is the 10% Finger threshold.
ON debounce:The recommended value for the Debounce parameter is 3 for reliable sensor status detection.
5.Configure Pins & System
【Design Wide Resources】 — 【Pins】
【Design Wide Resources】 — 【System】 — 【Debug Select】 — 【GPIO】
6.Build project
7.Add your code in【main.c】
int main ()
{
/* Enable Global interrupts for CapSense operation */
CyGlobalIntEnable;
/* Start EZI2C block */
EZI2C_Start();
/* Set up communication data buffer to CapSense data structure to be exposed to
I2C master at primary slave address request. */
EZI2C_EzI2CSetBuffer1(sizeof(CapSense_dsRam), sizeof(CapSense_dsRam),
(uint8 *)&CapSense_dsRam);
/* Start CapSense block - Initializes CapSense Data structure and
performs first scan to set up sensor baselines */
CapSense_Start();
/* Scan all widgets */
CapSense_ScanAllWidgets();
for(;;)
{
/* Do this only when a scan is done */
if(CapSense_NOT_BUSY == CapSense_IsBusy())
{
/* 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 */
CapSense_ScanAllWidgets();
}
}
}
8.Clean and build the program, plug MiniProg3
Note: When I Connected PSoc 4000 with MiniProg3,it would fail to Program if I don’t exchange the wire of SDAT and SCLK
四、PSoC Programmer
1.Launch PSoC Programmer 3.29.1
2.Select 【Device Family】 and 【Device】
3.Load HEX file, HEX file relative path: \CortexM0\ARM_GCC_541\Debug
4.Connect
5.Programming Mode, Select 【Reset】 if you use external power, Select 【Power Cycle】 if you use MiniProg3 to provide power
6.Programming
7.Disconnect, Release I2C
五、Tuner Communication
1.Launch Tuner
2.Tuner Communication Setup
3.【Connect】 — 【Start】
4.【xxx FT】、【xxx NT】、【xxx H】 Calibration
Note: touch the CapSense , observe the response of signal, change this value for your need.
5.Acquire Noise & Signal
Note: make sure SNR is more than 5:1 that is optimal.
六、Communication Between GD32F303 And PSoC4000
(1)PSoC4000 Configuration
1.Open above project,Configure 【EZI2C】
Note: Primary slave address (7-bits) is the 7-bit right-justified slave address and does not include the R/W bit. You can refer to Datasheet in page 64.
2.【Design Wide Resources】 — 【System】 — 【Debug Select】 — 【SWD(serial wire debug)】
3.【Design Wide Resources】 — 【Pins】, select the IIC port to GD32
4.Build project
5.Add your code in【main.c】
#define buffer_rw_boundary (0x04u)
static ezi2c_t Data_buf;
static uint8_t ButtonStatus = 0;
int main ()
{
/* Enable Global interrupts for CapSense operation */
CyGlobalIntEnable;
/* Start EZI2C block */
EZI2C_Start();
/* Set up communication data buffer to CapSense data structure to be exposed to
I2C master at primary slave address request. */
EZI2C_EzI2CSetBuffer1(sizeof(Data_buf),buffer_rw_boundary ,(uint8*)&Data_buf);
/* Start CapSense block - Initializes CapSense Data structure and
performs first scan to set up sensor baselines */
CapSense_Start();
/* Scan all widgets */
CapSense_ScanAllWidgets();
for(;;)
{
/* Do this only when a scan is done */
if(CapSense_NOT_BUSY == CapSense_IsBusy())
{
/* Process all widgets */
CapSense_ProcessAllWidgets();
ButtonStatus = 0;
//ID refer to CapSense_Configuration.h
if(CapSense_IsWidgetActive(CapSense_ESPRESSO_WDGT_ID))
{
ButtonStatus |= (1 << 0);
}
if(CapSense_IsWidgetActive(CapSense_LUNGO_WDGT_ID))
{
ButtonStatus |= (1 << 1);
}
if(CapSense_IsWidgetActive(CapSense_X_LUNGO_WDGT_ID))
{
ButtonStatus |= (1 << 2);
}
if(CapSense_IsWidgetActive(CapSense_CAPPUCINO_WDGT_ID))
{
ButtonStatus |= (1 << 3);
}
if(CapSense_IsWidgetActive(CapSense_LATTE_WDGT_ID))
{
ButtonStatus |= (1 << 4);
}
if(CapSense_IsWidgetActive(CapSense_HOT_WATER_WDGT_ID))
{
ButtonStatus |= (1 << 5);
}
if(CapSense_IsWidgetActive(CapSense_RINGE_WDGT_ID))
{
ButtonStatus |= (1 << 6);
}
/* Start next scan */
CapSense_ScanAllWidgets();
}
}
}
6.Clean and build the program, use 【PSoC Programmer 3.29.1 】to download the program
(2)GD32F303 Configuration
1.Download 【GD32F30x Firmware Library】【GD32F30x User Manual】
2.This Firmware is builded by Keil4, you must change the project name before open it with Keil5. Change 【Project.uvproj】to 【Project.uvprojx】
3.Select your device
4.Select debug mode.I use GD-Link to debug, so I select CMSIS-DAP Debugger
5.Add programming Algorithm
6.Build Project
7.Remap I2C0. I2C0 default in PB6 and PB7, refer to GD32F30x User Manual Page 177 and 190, So we must remap it.
void clock_init(void)
{
rcu_periph_clock_enable(RCU_GPIOB);
rcu_periph_clock_enable(RCU_I2C0);
rcu_periph_clock_enable(RCU_TIMER0);
rcu_periph_clock_enable(RCU_AF);
gpio_pin_remap_config(GPIO_I2C0_REMAP,ENABLE);
}
8.Init I2C0
void I2C0_INIT(void)
{
gpio_init(GPIOB, GPIO_MODE_AF_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_8 | GPIO_PIN_9);
i2c_clock_config(I2C0, 100000, I2C_DTCY_2);
i2c_mode_addr_config(I2C0, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, slave_add);
i2c_enable(I2C0);
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
}
slave_add is PSoC4000 address, refer to 【 PSoC4000 Configuration 】,you must move 1 bit to the right
9.I2C0 receive function
uint8_t Read_Touch_signal(uint16_t SlaveAdd,uint8_t *ReadByte)
{
uint8_t err = 1;
/* wait until I2C bus is idle */
while(i2c_flag_get(I2C0, I2C_FLAG_I2CBSY));
/* send a start condition to I2C bus */
i2c_start_on_bus(I2C0);
/* wait until SBSEND bit is set */
while(!i2c_flag_get(I2C0, I2C_FLAG_SBSEND));
/* send slave address to I2C bus */
i2c_master_addressing(I2C0, SlaveAdd, I2C_RECEIVER);
/* wait until ADDSEND bit is set */
while(!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND));
/* clear ADDSEND bit */
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
/* wait until the second last data byte is received into the shift register */
while(!i2c_flag_get(I2C0, I2C_FLAG_BTC));
/* disable acknowledge */
i2c_ack_config(I2C0, I2C_ACK_DISABLE);
/* wait until the RBNE bit is set */
while(!i2c_flag_get(I2C0, I2C_FLAG_RBNE));
/* read a data from I2C_DATA */
*ReadByte = i2c_data_receive(I2C0);
/* send a stop condition to I2C bus */
i2c_stop_on_bus(I2C0);
/* wait until stop condition generate */
while(I2C_CTL0(I2C0)&0x0200);
/* enable acknowledge */
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
err = 0;
return err;
}
Note: You can program function refer to firmware example
Summary
It is my first time to write a blog with English, you can leave a message to inform me if there are any error or unclear expression.Thanks a lot !
更多推荐
所有评论(0)