CY8CKIT-145-40XX PSoC 4000S Prototyping Kit 试用分享(三)

作者: zengxin1
上传时间为: 2018-10-22 05:25 PM
2018-10-22
阅读:

上周分享了电容式触摸感应技术是通过测量面板(传感器)和其环境之间的电容变化(自/互电容感应)来检测触摸界面附近是否有手指,本周分享一下软件部分。

Cypress的官网上有提供本板卡的示例程序,用PSoC Creator软件打开后,里面有2个projet,如图1所示,本次主要分享一下CapSense _Liner_and_Button这个projet。在2个projet的后面附有一个PDF文档,主要是介绍软件的设置、软硬件联调的方式、以及其他的一些资料文档说明,同时,在Documentation里也有该板卡的各组成硬件、固件等相关介绍。

图1

该project中的top design里有主要框架,如图2所示,以CapSense为例,可以选择该模块,在右键中选择Lunch Tuner,可以打开Sense Tuner软件,在其中进行相应的设置,当手指在板卡上进行触碰或滑动时,可以在该软件中显示(模拟)手指在板卡实际的位置,并有详细的示意图和波形图,且可以多个手指同时按下,也能正确显示多个触碰位置,如图3中红框所示,代表3个button都是按下状态,同时滑条的第4段也有手指触碰。

图2

图3

打开main.c文件,整个project从main函数开始执行,代码如下所示。

主要流程是:

①判断是否有使能/打开Tuner,并作出相应设置

②设置上电时,板卡的初始状态、使能全局中断、使能IIC

③触摸功能的软硬件初始化及开始工作

④PWM周期信号设置、板卡上的IO接口初始化设置

⑤无限循环,检测触摸按键和滑条的触摸情况,然后做出相应操作

int main()
{	
    #if !ENABLE_TUNER
        /*Used as loop counter and widget ID*/
        uint8 widgetID = 0;
        
        /*Contains the buttons status, one bit per button
          bit0= BTN1 status, bit1 = BTN2 status, bit2 = BTN3 status*/
        uint8 buttonStatus = 0;    
    #endif
        
    /* Variable to hold the current device state 
    *  State machine starts with sensor scan state after power-up
    */
    DEVICE_STATE currentState = SENSOR_SCAN;  
    
    /* Enable global interrupts. */
    CyGlobalIntEnable; 
    
    /* Start EZI2C block */
    EZI2C_Start();
        
    #if ENABLE_TUNER
        /* Set up I2C communication data buffer with CapSense data structure to be exposed to I2C master on a primary slave address request
        */
        EZI2C_EzI2CSetBuffer1(sizeof(CapSense_dsRam),\
        sizeof(CapSense_dsRam),(uint8 *)&CapSense_dsRam);
    #else
        /*Set up communication data buffer to send CapSense slider centroid position and button status to EZ-BLE on CY8CKIT-145-40XX PSoC 4000S Prototyping Kit*/
        EZI2C_EzI2CSetBuffer1(sizeof(i2cBuffer), sizeof(i2cBuffer),i2cBuffer);
    #endif
        
    /* Start CapSense block */
    CapSense_Start();
    
	/* Start the TCPWM components, TCPWM1 generates PWM signal of frequency of 100Hz TCPWM2 generates PWM signal of frequency of 101Hz*/
    TCPWM1_Start();
    TCPWM2_Start();
    
    /* Start the SmartIO component, the two PWM signals are XORed to get the LED breathing effect of 1Hz*/
    SmartIO_Port2_Start();       
     			
    for(;;)
    {
        /* Switch between SENSOR_SCAN->WAIT_FOR_SCAN_COMPLETE->PROCESS_DATA states */
        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();
                
                #if !ENABLE_TUNER
                    
                    /*If tuner is not enabled send the CapSense slider centroid position and button status to 
                        EZ-BLE PRoC Module on CY8CKIT-145-40XX PSoC 4000S Prototyping Kit  via I2C interface*/
                    
                    /*Update the I2C buffer with slider centroid position*/
                    i2cBuffer[SLIDER_CENTROID_INDEX] = (uint8) CapSense_GetCentroidPos(CapSense_SLD_WDGT_ID);
                    
                    /*Calculate the button staus mask and update the I2C buffer
                        bit0= BTN1 status, bit1 = BTN2 status, bit2 = BTN3 status*/
                    for(widgetID = 0; widgetID < TOTAL_CAPSENSE_BUTTONS ; widgetID++)
                    {
                        if(CapSense_IsWidgetActive(widgetID))
                        {
                            SET_BIT(buttonStatus, widgetID);
                        }
                        else
                        {
                            CLEAR_BIT(buttonStatus, widgetID);
                        }
                    }                     
                    i2cBuffer[BUTTON_STATUS_INDEX1] = buttonStatus;                   
                #endif                
                
                /* Set the device state to SENSOR_SCAN */
                currentState = SENSOR_SCAN;  
                break;  
             
            /*******************************************************************
             * Unknown power mode state. Unexpected situation.
             ******************************************************************/    
            default:
                break;
        } 
    }
}

至于程序中的细节,后面会选取与触摸监控及数据处理等方面相关的部分代码进行分享。

最后,感谢爱板网给我充裕的时间来试用该板卡!

全部评论 ()
条评论
写评论

创建讨论帖子

登录 后参与评论
系统提示