Cypress(赛普拉斯)电容式感应(CapSense)触摸按键应用笔记(工程配置+功能调校+IIC通讯)

1 篇文章 0 订阅
1 篇文章 0 订阅


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 !

  • 10
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值