⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cts_hal.c

📁 launchpad msp430官方示例
💻 C
📖 第 1 页 / 共 5 页
字号:
 *
 *          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                  +-->TA1CLK                     
 *      \n                  |
 *      \n     element-+-R--+-<-CAOUT                               
 *      \n             |
 *      \n             +------->COMPA- 
 * \n 
 *      \n     The timer counts to TA1CCR0 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_TA1_SW_HAL(const struct Sensor *group, uint16_t *counts)
{ 
    uint8_t i;
    uint16_t j;
    //** Context Save
//  Status Register: 
//  TIMERA0: TA1CTL, TA1CCTL0
//  COMPAp: CACTL1, CACTL2, CAPD
//  Ports: caoutDIR, caoutSel, txclkDIR, txclkSel, caoutSel2, txclkSel2, refout, refdir 
    uint16_t contextSaveTA1CTL,contextSaveTA1CCTL0,contextSaveTA1CCR0;
    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    
    contextSaveTA1CTL = TA1CTL;
    contextSaveTA1CCTL0 = TA1CCTL0;
    contextSaveTA1CCR0 = TA1CCR0;
    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
    TA1CCR0 =(group->accumulationCycles);
    // 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;
        CACTL2= group->refCactl2Bits + (group->arrayPtr[i])->inputBits;
        //**  Setup Gate Timer **************
        // Set duration of sensor measurment
        TA1CTL = TASSEL_0+TACLR+MC_1;     // TA1CLK, Reset, up mode
        TA1CTL &= ~TAIFG;                 // clear IFG        
        while(!(TACTL & TAIFG))
        {
            j++;
        } // end accumulation
        counts[i] = j;   
    }
    // 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    
    TA1CTL = contextSaveTA1CTL;
    TA1CCTL0 = contextSaveTA1CCTL0;
    TA1CCR0 = contextSaveTA1CCR0;
    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

/***************************************************************************//**
 * @brief   RC method capactiance measurement using a Pair of GPIO and TimerA0
 *
 *          Schematic Description of two GPIO forming RC measruement.
 *    \n       <- Output
 *    \n       -> Input
 *    \n       R  Resistor (typically 1Mohms)
 * \n 
 *     \n                   +-<-Px.y (reference)
 *     \n                   |
 *     \n                   R
 *     \n                   |
 *     \n         Element---+-->Pa.b                               
 *     \n      
 *     \n      Charge and Discharge Cycle
 *     \n                       + 
 *     \n                   +    +
 *     \n               +          +
 *     \n            +                +
 *     \n          +                     +
 *     \n      Start Timer                  After n cycles Stop Timer
 *     \n   The TAR reister value is the number of SMCLK periods within n charge
 *          and discharge cycles.  This value is directly proportional to the 
 *          capacitance of the element measured. 'n' is defined by the variable 
 *          accumulation_cycles.
 * 
 * @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
 ******************************************************************************/
#ifdef RC_PAIR_TA0            
void TI_CTS_RC_PAIR_TA0_HAL(const struct Sensor *group,uint16_t *counts)
{
    uint8_t i;
	uint16_t j;
    
    //** Context Save
//  TIMERA0: TA0CTL
//  Port: inputPxout, inputPxdir, referencePxout, referencePxdir

    uint8_t contextSaveinputPxout,contextSaveinputPxdir,contextSavereferencePxout;
    uint8_t contextSavereferencePxdir;

    #ifdef __MSP430_HAS_SFR__
    uint16_t contextSaveTA0CTL,contextSaveTA0CCR0;

    contextSaveTA0CTL = TA0CTL;
    contextSaveTA0CCR0 = TA0CCR0;
    #else
    uint16_t contextSaveTACTL,contextSaveTACCR0;
    
    contextSaveTACTL = TACTL; 
    contextSaveTACCR0 = TACCR0; 
    #endif

//** Setup Measurement timer****************************************************
// Choices are TA0,TA1,TB0,TB1,TD0,TD1 these choices are pushed up into the 
// capacitive touch layer.
    #ifdef __MSP430_HAS_SFR__
    TA0CCR0 = 0xFFFF;                           
    #else
    TACCR0 = 0xFFFF;                           
    #endif
    for (i = 0; i<(group->numElements); i++)
    {
        // Context Save
        contextSaveinputPxout = *((group->arrayPtr[i])->inputPxoutRegister);
        contextSaveinputPxdir = *((group->arrayPtr[i])->inputPxdirRegister);
        contextSavereferencePxout = *((group->arrayPtr[i])->referencePxoutRegister);
        contextSavereferencePxdir = *((group->arrayPtr[i])->referencePxdirRegister);
        j = (group->accumulationCycles);
        #ifdef __MSP430_HAS_SFR__
	    TA0CTL = TASSEL_2+TACLR;                // SMCLK, up mode
        #else
        TACTL = TASSEL_2+TACLR;                // SMCLK, up mode
        #endif
        while(j--)
	    {
        //******************************************************************************
	    // Positive cycle
        //    SENSOR ---+---- Input (low to high)
	    //              R
	    //              +---- Rerefence (high)
            //******************************************************************************
	    // Input low
            *((group->arrayPtr[i])->inputPxoutRegister) &= ~((group->arrayPtr[i])->inputBits);
	    *((group->arrayPtr[i])->inputPxdirRegister) |= (group->arrayPtr[i])->inputBits;
            // Reference High
            *((group->arrayPtr[i])->referencePxdirRegister) |= (group->arrayPtr[i])->referenceBits;
            *((group->arrayPtr[i])->referencePxoutRegister) |= ((group->arrayPtr[i])->referenceBits);
            // Wait until low
	    while((*((group->arrayPtr[i])->inputPxinRegister)) & ((group->arrayPtr[i])->inputBits));
            // Change to an input
            *((group->arrayPtr[i])->inputPxdirRegister) &= ~(group->arrayPtr[i])->inputBits;
	    //**************************************************************************
	    // This mechanism is traditianally an LPM with the ISR calculating the 
	    // delta between when the first snapshot and the ISR event.  If this is
	    // included within the library the entire port ISR would not be available
            // to the calling application.  In this example the polling is done with the
            // CPU at expense of power and MIPS but preserves the port ISR for other 
	    // interruptible functions.
	    //**************************************************************************
            #ifdef __MSP430_HAS_SFR__
            TA0CTL |= MC_1;                     // start timer    
            #else
            TACTL |= MC_1;                     // start timer    
            #endif
	    //wait until voltage reaches Vih of port
	    while(!((*((group->arrayPtr[i])->inputPxinRegister) & (group->arrayPtr[i])->inputBits)));
            #ifdef __MSP430_HAS_SFR__
            TA0CTL &= ~ MC_3;                     // stop timer        
            #else
            TACTL &= ~ MC_3;                     // stop timer        
            #endif
			//******************************************************************************
			// Negative cycle
			//    SENSOR ---+---- Input (high to low)
			//              R
			//              +---- Rerefence (low)
			//******************************************************************************
	    // Input High
            *((group->arrayPtr[i])->inputPxoutRegister) |= ((group->arrayPtr[i])->inputBits);  
            *((group->arrayPtr[i])->inputPxdirRegister) |= (group->arrayPtr[i])->inputBits; 
	    // Reference Low
	    *((group->arrayPtr[i])->referencePxoutRegister) &= ~((group->arrayPtr[i])->referenceBits);
	    // Change to an input
	    *((group->arrayPtr[i])->inputPxdirRegister) &= ~((group->arrayPtr[i])->inputBits);
            #ifdef __MSP430_HAS_SFR__
            TA0CTL |= MC_1;                     // start timer  
            #else
            TACTL |= MC_1;                     // start timer  
            #endif
	    //wait until voltage reaches Vil of port  
	    while((*((group->arrayPtr[i])->inputPxinRegister)) & ((group->arrayPtr[i])->inputBits));
            #ifdef __MSP430_HAS_SFR__
            TA0CTL &= ~ MC_3;                     // stop timer        
            #else
            TACTL &= ~ MC_3;                     // stop timer        
            #endif
        } // END accumulation loop for a single element
        #ifdef __MSP430_HAS_SFR__
        counts[i] = TA0R;    
        #else
        counts[i] = TAR;    
        #endif
        // Context Restore
        *((group->arrayPtr[i])->inputPxoutRegister) = contextSaveinputPxout;
        *((group->arrayPtr[i])->inputPxdirRegister) = contextSaveinputPxdir;     
        *((group->arrayPtr[i])->referencePxoutRegister) = contextSavereferencePxout;
        *((group->arrayPtr[i])->referencePxdirRegister) = contextSavereferencePxdir;             
    } // END FOR loop which cycles through elements within sensor
    
    //** Context Restore
    #ifdef __MSP430_HAS_SFR__
    TA0CTL = contextSaveTA0CTL;
    TA0CCR0 = contextSaveTA0CCR0;
    #else
    TACTL = contextSaveTACTL;
    TACCR0 = contextSaveTACCR0;
    #endif

}
#endif

/***************************************************************************//**
 * @brief   fRO method capactiance measurement using the PinOsc and TimerA0
 *          
 *     \n      Charge and Discharge Cycle
 *     \n                       + 
 *     \n                   +    +
 *     \n               +          +
 *     \n            +                +
 *     \n          +                     +
 *     \n      Start Timer                  After n cycles Stop Timer
 *     \n   The TAR reister value is the number of SW loops (function of MCLK) 
 *          within n charge and discharge cycles.  This value is directly 
 *          proportional to the capacitance of the element measured. 'n' is 
 *          defined by the variable accumulation_cycles.
 * 
 * @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
 ******************************************************************************/
#ifdef fRO_PINOSC_TA0_SW            
void TI_CTS_fRO_PINOSC_TA0_SW_HAL(const struct Sensor *group,uint16_t *counts)
{ 
    uint8_t i;
    uint16_t j;
//** Context Save

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -