📄 cts_hal.c
字号:
/*******************************************************************************
* CTS_HAL.c - Hardware abstraction of various combinations of modules to
* perform a capacitance measurement.
*
* Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the
* distribution.
*
* Neither the name of Texas Instruments Incorporated nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************/
/***************************************************************************//**
* @file CTS_HAL.c
*
* @brief
*
* @par Project:
* MSP430 Capacitive Touch Library
*
* @par Developed using:
* IAR Version : 5.10.6 [Kickstart] (5.10.6.30180)
* CCS Version : 4.2.1.00004, w/support for GCC extensions (--gcc)
*
*
* @version 1.0.0 Initial Release
*
* @par Supported Hardware Configurations:
* - TI_CTS_RO_COMPAp_TA0_WDTp_HAL()
* - TI_CTS_fRO_COMPAp_TA0_SW_HAL()
* - TI_CTS_fRO_COMPAp_SW_TA0_HAL()
* - TI_CTS_RO_COMPAp_TA1_WDTp_HAL()
* - TI_CTS_fRO_COMPAp_TA1_SW_HAL()
* - TI_CTS_RC_PAIR_TA0_HAL()
* - TI_CTS_RO_PINOSC_TA0_WDTp_HAL()
* - TI_CTS_RO_PINOSC_TA0_HAL()
* - TI_CTS_fRO_PINOSC_TA0_SW_HAL()
* - TI_CTS_RO_COMPB_TA0_WDTA_HAL()
* - TI_CTS_RO_COMPB_TA1_WDTA_HAL()
* - TI_CTS_fRO_COMPB_TA0_SW_HAL()
* - TI_CTS_fRO_COMPB_TA1_SW_HAL()
******************************************************************************/
/***************************************************************************//**
* @addtogroup CTS_HAL
* @{
******************************************************************************/
#include "CTS_HAL.h"
#ifdef RO_COMPAp_TA0_WDTp
/***************************************************************************//**
* @brief RO method capactiance measurement using CompA+, TimerA0, and WDT+
*
* \n Schematic Description of CompA+ forming relaxation oscillator and
* coupling (connection) between the relaxation oscillator and TimerA0.
* \n <- Output
* \n -> Input
* \n R Resistor (typically 100Kohms)
* \n
* \n +-<-Px.y (reference)
* \n |
* \n R
* \n |
* \n +---+-->COMPA+
* \n | |
* \n R R
* \n | |
* \n GND |
* \n |
* \n +-->TACLK
* \n |
* \n element-+-R--+-<-CAOUT
* \n |
* \n +------->COMPA-
* \n
* \n The WDT+ interval represents the measurement window. The number of
* counts within the TA0R that have accumulated during the measurement
* window represents the capacitance of the element.
*
* @param group Address of the structure describing the Sensor to be measured
* @param counts Address to where the measurements are to be written
* @return none
******************************************************************************/
void TI_CTS_RO_COMPAp_TA0_WDTp_HAL(const struct Sensor *group, uint16_t *counts)
{
uint8_t i;
//** Context Save
// Status Register:
// WDTp: IE1, WDTCTL
// TIMERA0: TACTL, TACCTL1
// COMPAp: CACTL1, CACTL2, CAPD
// Ports: caoutDIR, caoutSel, txclkDIR, txclkSel, caoutSel2, txclkSel2, refout, refdir
uint8_t contextSaveSR;
uint8_t contextSaveIE1;
uint16_t contextSaveWDTCTL;
uint16_t contextSaveTACTL,contextSaveTACCTL1,contextSaveTACCR1;
uint8_t contextSaveCACTL1,contextSaveCACTL2,contextSaveCAPD;
uint8_t contextSaveCaoutDir,contextSaveCaoutSel;
uint8_t contextSavetxclkDir,contextSavetxclkSel;
uint8_t contextSaveRefDir,contextSaveRefOutSel;
#ifdef SEL2REGISTER
uint8_t contextSaveCaoutSel2,contextSaveTxclkSel2;
contextSaveCaoutSel2 = *(group->caoutSel2Register);
contextSaveTxclkSel2 = *(group->txclkSel2Register);
#endif
contextSaveSR = __get_SR_register();
contextSaveIE1 = IE1;
contextSaveWDTCTL = WDTCTL;
contextSaveWDTCTL &= 0x00FF;
contextSaveWDTCTL |= WDTPW;
contextSaveTACTL = TACTL;
contextSaveTACCTL1 = TACCTL1;
contextSaveTACCR1 = TACCR1;
contextSaveCACTL1 = CACTL1;
contextSaveCACTL2 = CACTL2;
contextSaveCAPD = CAPD;
contextSaveCaoutDir = *(group->caoutDirRegister);
contextSaveCaoutSel = *(group->caoutSelRegister);
contextSavetxclkDir = *(group->txclkDirRegister);
contextSavetxclkSel = *(group->txclkSelRegister);
contextSaveRefDir = *(group->refPxdirRegister);
contextSaveRefOutSel = *(group->refPxoutRegister);
TACTL = TASSEL_0+MC_2; // TACLK, cont mode
TACCTL1 = CM_3+CCIS_2+CAP; // Pos&Neg,GND,Cap
*(group->caoutDirRegister) |= group->caoutBits;
*(group->txclkDirRegister) &= ~group->txclkBits;
*(group->caoutSelRegister) |= group->caoutBits;
*(group->txclkSelRegister) |= group->txclkBits;
#ifdef SEL2REGISTER
*(group->caoutSel2Register) |= group->caoutBits;
*(group->txclkSel2Register) |= group->txclkBits;
#endif
*(group->refPxdirRegister) |= group->refBits;
*(group->refPxoutRegister) |= group->refBits;
CACTL1 |= CAON; // Turn on comparator
CAPD |= (group->capdBits);
IE1 |= WDTIE; // enable WDT interrupt
for (i = 0; i<(group->numElements); i++)
{
CACTL2= group->refCactl2Bits + (group->arrayPtr[i])->inputBits;
//** Setup Gate Timer *****************************************************
// Set duration of sensor measurment
WDTCTL = WDTPW+WDTTMSEL+ group->measGateSource + group->accumulationCycles;
TACTL |= TACLR; // Clear Timer_A TAR
if(group->measGateSource == GATE_WDTp_ACLK)
{
__bis_SR_register(LPM3_bits+GIE); // Wait for WDT interrupt
}
else
{
__bis_SR_register(LPM0_bits+GIE); // Wait for WDT interrupt
}
TACCTL1 ^= CCIS0; // Create SW capture of CCR1
counts[i] = TACCR1; // Save result
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
}
// End Sequence
//** Context Restore
// WDTp: IE1, WDCTL
// TIMERA0: TACTL, TACCTL1
// COMPAp: CACTL1, CACTL2, CAPD
// Ports: caoutDIR, caoutSel, txclkDIR, txclkSel, caoutSel2, txclkSel2, refout, refdir
#ifdef SEL2REGISTER
*(group->caoutSel2Register) = contextSaveCaoutSel2;
*(group->txclkSel2Register) = contextSaveTxclkSel2;
#endif
__bis_SR_register(contextSaveSR);
if(!(contextSaveSR & GIE))
{
__bic_SR_register(GIE); // Wait for WDT interrupt
}
IE1 = contextSaveIE1;
WDTCTL = contextSaveWDTCTL;
TACTL = contextSaveTACTL;
TACCTL1 = contextSaveTACCTL1;
TACCR1 = contextSaveTACCR1;
CACTL1 = contextSaveCACTL1;
CACTL2 = contextSaveCACTL2;
CAPD = contextSaveCAPD;
*(group->caoutDirRegister) = contextSaveCaoutDir;
*(group->caoutSelRegister) = contextSaveCaoutSel;
*(group->txclkDirRegister) = contextSavetxclkDir;
*(group->txclkSelRegister) = contextSavetxclkSel;
*(group->refPxdirRegister) = contextSaveRefDir;
*(group->refPxoutRegister) = contextSaveRefOutSel;
}
#endif
#ifdef fRO_COMPAp_TA0_SW
/***************************************************************************//**
* @brief RO method capactiance measurement using CompA+, TimerA0, and SW loop
*
* \n Schematic Description of CompA+ forming relaxation oscillator.
* \n <- Output
* \n -> Input
* \n R Resistor (typically 100Kohms)
* \n
* \n +-<-Px.y (reference)
* \n |
* \n R
* \n |
* \n +---+-->COMPA+
* \n | |
* \n R R
* \n | |
* \n GND |
* \n |
* \n +-->TACLK
* \n |
* \n element-+-R--+-<-CAOUT
* \n |
* \n +------->COMPA-
* \n
* \n The timer counts to TA0CCR0 representing the measurement window. The
* number of counts within the SW loop that have accumulated during the
* measurement window represents the capacitance of the element.
*
* @param group Address of the structure describing the Sensor to be measured
* @param counts Address to where the measurements are to be written
* @return none
******************************************************************************/
void TI_CTS_fRO_COMPAp_TA0_SW_HAL(const struct Sensor *group, uint16_t *counts)
{
uint8_t i;
uint16_t j;
//** Context Save
// Status Register:
// TIMERA0: TACTL, TACCTL0
// COMPAp: CACTL1, CACTL2, CAPD
// Ports: caoutDIR, caoutSel, txclkDIR, txclkSel, caoutSel2, txclkSel2, refout, refdir
uint16_t contextSaveTACTL,contextSaveTACCTL0,contextSaveTACCR0;
uint8_t contextSaveCACTL1,contextSaveCACTL2,contextSaveCAPD;
uint8_t contextSaveCaoutDir,contextSaveCaoutSel;
uint8_t contextSavetxclkDir,contextSavetxclkSel;
uint8_t contextSaveRefDir,contextSaveRefOutSel;
#ifdef SEL2REGISTER
uint8_t contextSaveCaoutSel2,contextSaveTxclkSel2;
contextSaveCaoutSel2 = *(group->caoutSel2Register);
contextSaveTxclkSel2 = *(group->txclkSel2Register);
#endif
contextSaveTACTL = TACTL;
contextSaveTACCTL0 = TACCTL0;
contextSaveTACCR0 = TACCR0;
contextSaveCACTL1 = CACTL1;
contextSaveCACTL2 = CACTL2;
contextSaveCAPD = CAPD;
contextSaveCaoutDir = *(group->caoutDirRegister);
contextSaveCaoutSel = *(group->caoutSelRegister);
contextSavetxclkDir = *(group->txclkDirRegister);
contextSavetxclkSel = *(group->txclkSelRegister);
contextSaveRefDir = *(group->refPxdirRegister);
contextSaveRefOutSel = *(group->refPxoutRegister);
//** Setup Measurement timer***************************************************
// Configure Timer TA0
TACCR0 =(group->accumulationCycles);
TACCTL0 &= ~CAP;
// setup connections between CAOUT and TA0
*(group->caoutDirRegister) |= group->caoutBits;
*(group->txclkDirRegister) &= ~group->txclkBits;
*(group->caoutSelRegister) |= group->caoutBits;
*(group->txclkSelRegister) |= group->txclkBits;
#ifdef SEL2REGISTER
*(group->caoutSel2Register) |= group->caoutBits;
*(group->txclkSel2Register) |= group->txclkBits;
#endif
// setup reference
*(group->refPxdirRegister) |= group->refBits;
*(group->refPxoutRegister) |= group->refBits;
CACTL1 |= CAON; // Turn on comparator
CAPD |= (group->capdBits);
for (i = 0; i<(group->numElements); i++)
{
j=0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -