Reshmi R
Published © GPL3+

Controlling Brightness of LED on FM4 Using PSoC4 Capsense

This code example demonstrates controlling brightness of an LED on FM4 Kit using Capsense on PSoC4BLE Kit. Here PsoC4 acts as I2C Master.

IntermediateFull instructions provided1 hour1,334
Controlling Brightness of LED on FM4 Using PSoC4 Capsense

Things used in this project

Hardware components

PSoC BLE Pioneer Kit
Cypress PSoC BLE Pioneer Kit
×1
Cypress FM4-U120-9B560 Kit
×1
Resistor 2.21k ohm
Resistor 2.21k ohm
×2

Software apps and online services

PSoC Creator
Cypress PSoC Creator
IAR Embedded Workbench for ARM

Story

Read more

Code

PSoC4BLE Capsense and I2C master Project

C/C++
/*****************************************************************************
* File Name: main.c
*
* Version: 1.10
*
* Description:  This example code demonstrates the PSoC4 CapSense Linear Slider
* operation.
*
* Related Document: Code example CE210289.pdf 
*
* Hardware Dependency: See code example CE210289
*
******************************************************************************
* Copyright (2016), Cypress Semiconductor Corporation.
******************************************************************************
* This software is owned by Cypress Semiconductor Corporation (Cypress) and is
* protected by and subject to worldwide patent protection (United States and
* foreign), United States copyright laws and international treaty provisions.
* Cypress hereby grants to licensee a personal, non-exclusive, non-transferable
* license to copy, use, modify, create derivative works of, and compile the
* Cypress Source Code and derivative works for the sole purpose of creating
* custom software in support of licensee product to be used only in conjunction
* with a Cypress integrated circuit as specified in the applicable agreement.
* Any reproduction, modification, translation, compilation, or representation of
* this software except as specified above is prohibited without the express
* written permission of Cypress.
*
* Disclaimer: CYPRESS MAKES NO WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, WITH
* REGARD TO THIS MATERIAL, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
* Cypress reserves the right to make changes without further notice to the
* materials described herein. Cypress does not assume any liability arising out
* of the application or use of any product or circuit described herein. Cypress
* does not authorize its products for use as critical components in life-support
* systems where a malfunction or failure may reasonably be expected to result in
* significant injury to the user. The inclusion of Cypress' product in a life-
* support systems application implies that the manufacturer assumes all risk of
* such use and in doing so indemnifies Cypress against all charges. Use may be
* limited by and subject to the applicable Cypress software license agreement.
*****************************************************************************/

#include "project.h"
#include "main.h"


volatile uint8 Slider_pos=0;

/*******************************************************************************
* Function Name: main
********************************************************************************
*
* Summary:
*  main() performs the following functions:
*  1. Initializes CapSense and SCB blocks
*  2. Scans & processes slider sensors
*
*******************************************************************************/
int main ()
{    
    
    
    /* Enable Global interrupts for CapSense operation */
    CyGlobalIntEnable;
    
    I2CM_Start();
    /* 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*/
                Slider_pos= CapSense_GetCentroidPos(CapSense_LINEARSLIDER0_WDGT_ID);
                WriteCommandPacket(Slider_pos);
              }
              /* Start next scan */
            
              CapSense_ScanAllWidgets();
            
           
        }
        
        
    }
}

/*******************************************************************************
* Function Name: WriteCommandPacket
********************************************************************************
* Summary:
*  Master initiates the transfer to write command packet into the slave.
*  The status of the transfer is returned.
*
* Parameters:
*  cmd: command to execute. Available commands:
*   - CMD_SET_RED:   set red color of the LED.
*   - CMD_SET_GREEN: set green color of the LED.
*   - CMD_SET_BLUE:  set blue color of the LED.
*   - CMD_SET_OFF:   turn off the LED.
*
* Return:
*  Returns status of the transfer. There are two statuses
*  - TRANSFER_CMPLT: transfer completed successfully.
*  - TRANSFER_ERROR: the error occurred while transfer.
*
*******************************************************************************/
uint32 WriteCommandPacket(uint8 cmd)
{
    uint8  buffer[BUFFER_SIZE];
    uint32 status = TRANSFER_ERROR;

    /* Initialize buffer with packet */
    buffer[PACKET_SOP_POS] = PACKET_SOP;
    buffer[PACKET_CMD_POS] = cmd;
    buffer[PACKET_EOP_POS] = PACKET_EOP;

    (void) I2CM_I2CMasterWriteBuf(I2C_SLAVE_ADDR, buffer, PACKET_SIZE, \
                                  I2CM_I2C_MODE_COMPLETE_XFER);

    /* Waits until master completes write transfer */
    while (0u == (I2CM_I2CMasterStatus() & I2CM_I2C_MSTAT_WR_CMPLT))
    {
    }

    /* Displays transfer status */
    if (0u == (I2CM_I2C_MSTAT_ERR_XFER & I2CM_I2CMasterStatus()))
    {
        /* Check if all bytes was written */
        if (I2CM_I2CMasterGetWriteBufSize() == BUFFER_SIZE)
        {
            RGB_LED_ON_GREEN;
            status = TRANSFER_CMPLT;
        }
    }
    else
    {
        RGB_LED_ON_RED;
    }

    (void) I2CM_I2CMasterClearStatus();

    return (status);
}



/* [] END OF FILE */

FM4 I2C slave project

C/C++
No preview (download only).

Credits

Reshmi R
1 project • 0 followers
Contact

Comments

Please log in or sign up to comment.