Hackster is hosting Hackster Holidays, Ep. 5: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 5 on Friday!
Vignesh J
Published © GPL3+

Theft Alarm K82F TSI_LAUNCHPAD

The Theft alarm is basically an intruder alert system used to prevent theft/robbery and protect one’s premises.It is Build Using FRDM K82F.

BeginnerFull instructions provided10 hours612
Theft Alarm K82F TSI_LAUNCHPAD

Things used in this project

Story

Read more

Schematics

Pin Configuration

Pins

Code

main.c

C/C++
#include <stdio.h>
#include "board.h"
#include "fsl_os_abstraction.h"
#include "fsl_tsi_driver.h"
#include "fsl_debug_console.h"
#include "gpio_pins.h"
//Define Statements
#define TSI_THRESHOLD_SAMPLING      (200u)
#define TSI_INSTANCE                0
#define true                       1
#define false                      0

int main(void)
{
    uint16_t measureResult[BOARD_TSI_ELECTRODE_CNT];
    uint8_t tsiChn[BOARD_TSI_ELECTRODE_CNT];
    uint32_t i, j, avgUntouch = 0;
    uint32_t sumUntouch=0;
    uint32_t avgMeasure;
    tsi_status_t result;
#if (FSL_FEATURE_TSI_VERSION == 2)

    static const tsi_config_t tsiHwConfig =
    {
        .ps       = kTsiElecOscPrescaler_2div,       /*! Prescaler */
        .extchrg  = kTsiExtOscChargeCurrent_10uA,    /*! Electrode charge current */
        .refchrg  = kTsiRefOscChargeCurrent_10uA,    /*! Reference charge current */
        .nscn     = kTsiConsecutiveScansNumber_8time,/*! Number of scans. */
        .lpclks   = kTsiLowPowerInterval_100ms,      /*! Low power clock. */
        .amclks   = kTsiActiveClkSource_BusClock,    /*! Active mode clock source. */
        .ampsc    = kTsiActiveModePrescaler_8div,    /*! Active mode prescaler. */
        .lpscnitv = kTsiLowPowerInterval_100ms,      /*! Low power scan interval. */
        .thresh   = 100u,                            /*! High byte of threshold. */
        .thresl   = 200u,                            /*! Low byte of threshold. */
    };
#elif (FSL_FEATURE_TSI_VERSION == 4)

    static const tsi_config_t tsiHwConfig =
    {
        .ps      = kTsiElecOscPrescaler_2div,        /*! Prescaler */
        .extchrg = kTsiExtOscChargeCurrent_8uA,      /*! Electrode charge current */
        .refchrg = kTsiRefOscChargeCurrent_8uA,      /*! Reference charge current */
        .nscn    = kTsiConsecutiveScansNumber_8time, /*! Number of scans. */
        .mode    = kTsiAnalogModeSel_Capacitive,     /*! TSI analog modes in a TSI instance */
        .dvolt   = kTsiOscVolRails_Dv_103,
        .thresh   = 100,                             /*! High byte of threshold. */
        .thresl   = 200,                             /*! Low byte of threshold. */
    };
#endif
tsi_state_t myTsiState;
 const tsi_user_config_t tsiConfig =
    {
        .config        = (tsi_config_t*)&tsiHwConfig,
        .pCallBackFunc = NULL,
        .usrData       = 0,
    };
    // Get TSI channel.
    tsiChn[0] = BOARD_TSI_ELECTRODE_1;
#if (BOARD_TSI_ELECTRODE_CNT > 1)
    tsiChn[1] = BOARD_TSI_ELECTRODE_2;
#endif
#if (BOARD_TSI_ELECTRODE_CNT > 2)
    tsiChn[2] = BOARD_TSI_ELECTRODE_3;
#endif
#if (BOARD_TSI_ELECTRODE_CNT > 3)
    tsiChn[3] = BOARD_TSI_ELECTRODE_4;
#endif

    // Initialize hardware
    hardware_init();
    OSA_Init();
    // Init LED1 & turn off it.
    LED1_EN;
    LED1_OFF;
  PRINTF("\r\n Touch Sensing input example ");
    // Driver initialization
    result = TSI_DRV_Init(TSI_INSTANCE, &myTsiState, &tsiConfig);
    if(result != kStatus_TSI_Success)
    {
        PRINTF("\r\nThe initialization of TSI driver failed ");
        return -1;
    }
    // Enable electrodes for normal mode
    for(i = 0; i < BOARD_TSI_ELECTRODE_CNT; i++)
    {
        result = TSI_DRV_EnableElectrode(TSI_INSTANCE, tsiChn[i], true);
        if(result != kStatus_TSI_Success)
        {
            PRINTF("\r\nThe initialization of TSI channel %d failed \r\n", tsiChn[i]);
            return -1;
        }
    }
    // Measures average value of untouched state.
    result = TSI_DRV_MeasureBlocking(TSI_INSTANCE);
    if(result != kStatus_TSI_Success)
    {
        PRINTF("\r\nThe measure of TSI failed. ");
        return -1;
    }
    // Measures average value in untouched mode.
    for(i = 0; i<TSI_THRESHOLD_SAMPLING; i++)
    {
        for(j = 0; j < BOARD_TSI_ELECTRODE_CNT; j++)
        {
            result = TSI_DRV_GetCounter(TSI_INSTANCE, tsiChn[j], &measureResult[j]);
            if(result != kStatus_TSI_Success)
            {
                PRINTF("\r\nThe read of TSI channel %d failed.", tsiChn[j]);
                return -1;
            }
            // Calculates sum of average values.
            sumUntouch += measureResult[j];
        }
    }
    avgUntouch = sumUntouch/(TSI_THRESHOLD_SAMPLING * BOARD_TSI_ELECTRODE_CNT);
    PRINTF("\r\nTouching for turning led on \r\n");
    while(1)
    {
        result = TSI_DRV_MeasureBlocking(TSI_INSTANCE);
        if(result != kStatus_TSI_Success)
        {
            PRINTF("\r\nThe measure of TSI failed.");
            return -1;
        }
        avgMeasure = 0;
        for(i = 0; i < BOARD_TSI_ELECTRODE_CNT; i++)
        {
            result = TSI_DRV_GetCounter(TSI_INSTANCE, tsiChn[i], &measureResult[i]);
            if(result != kStatus_TSI_Success)
            {
                PRINTF("\r\nThe read of TSI channel %d failed.", tsiChn[i]);
                return -1;
            }
            avgMeasure += measureResult[i];
        }
        avgMeasure /=BOARD_TSI_ELECTRODE_CNT;
        // Add 10 to remove noise
        if(avgMeasure > avgUntouch+10)
        {
            LED1_OFF;
        }
        else
        {
            LED1_ON;
        }
        // Measures each 100ms.
        OSA_TimeDelay(100u);
    }
}

fsl_tsi_irq.c

C/C++
#include "fsl_tsi_driver.h"



extern void TSI_DRV_IRQHandler(uint32_t instance);
/*******************************************************************************
 * Code
 ******************************************************************************/



void TSI0_IRQHandler(void)
{
    TSI_DRV_IRQHandler(0);
}

Credits

Vignesh J

Vignesh J

4 projects • 7 followers
Hello Friends I am Vignesh ,B.E (Electronics &Communication)Graduate.Intrested in doing IOT Based projects

Comments