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

📄 cts_hal.c

📁 launchpad msp430官方示例
💻 C
📖 第 1 页 / 共 5 页
字号:
    *(group->cboutTAxDirRegister) = contextSaveCboutDir;
    *(group->cboutTAxSelRegister) = contextSaveCboutSel;  
}
#endif

#ifdef RO_COMPB_TA1_WDTA
/***************************************************************************//**
 * @brief   RO method capactiance measurement using CompB, TimerA1, and WDTA
 *
 *          Schematic Description of CompB forming relaxation oscillator and
 *          coupling (connection) between the relaxation oscillator and TimerA1.
 *          <- Output
 *          -> Input
 *          R  Resistor (typically 100Kohms)
 * 
 *          element---R----<-CBOUT/TA1CLK                               
 * 
 *          The WDTA interval represents the measurement window.  The number of 
 *          counts within the TA1R 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_COMPB_TA1_WDTA_HAL(const struct Sensor *group, uint16_t *counts)
{ 
    uint8_t i=0;
    
    //** Context Save
//  Status Register: 
//  WDTA: IE1, WDTCTL
//  TIMERA1: TA1CTL, TA1CCTL1
//  COMPB: CBCTL0, CBCTL1, CBCTL2, CBCTL3
//  Ports: CboutDIR, CboutSel 
    uint8_t contextSaveSR; 
    uint16_t contextSaveSFRIE1;
    uint16_t contextSaveWDTCTL;
    uint16_t contextSaveTA1CTL,contextSaveTA1CCTL1,contextSaveTA1CCR1;
    uint16_t contextSaveCBCTL0,contextSaveCBCTL1;
    uint16_t contextSaveCBCTL2,contextSaveCBCTL3;
    uint8_t contextSaveCboutDir,contextSaveCboutSel;  

    contextSaveSR = __get_SR_register();
    contextSaveSFRIE1 = SFRIE1;
    contextSaveWDTCTL = WDTCTL;
    contextSaveWDTCTL &= 0x00FF;
    contextSaveWDTCTL |= WDTPW;        
    contextSaveTA1CTL = TA1CTL;
    contextSaveTA1CCTL1 = TA1CCTL1;
    contextSaveTA1CCR1 = TA1CCR1;
    
    contextSaveCBCTL0 = CBCTL0;
    contextSaveCBCTL1 = CBCTL1;
    contextSaveCBCTL2 = CBCTL2;
    contextSaveCBCTL3 = CBCTL3;
    contextSaveCboutDir = *(group->cboutTAxDirRegister);
    contextSaveCboutSel = *(group->cboutTAxSelRegister);  

    //** Setup Measurement timer***************************************************
    // connect CBOUT with TA1
    *(group->cboutTAxDirRegister) |= (group->cboutTAxBits);
    *(group->cboutTAxSelRegister) |= (group->cboutTAxBits);
    // Setup Comparator
    CBCTL2 = CBREF14+CBREF13 + CBREF02;
    // Configure Timer TA1
    TA1CTL = TASSEL_0+MC_2;                // TACLK, cont mode
    TA1CCTL1 = CM_3+CCIS_2+CAP;            // Pos&Neg,GND,Cap 
    // Turn on Comparator
    CBCTL1 = CBON + CBF;                       // Turn on comparator with filter
    CBCTL3 |= (group->cbpdBits);               // set CPD bits to disable 
    SFRIE1 |= WDTIE;                           // enable WDT interrupt
    CBCTL2 |= CBRS_1;                          // Turn on reference
    for (i = 0; i<(group->numElements); i++)
    {
        CBCTL0 = CBIMEN + (group->arrayPtr[i])->inputBits;
        //**  Setup Gate Timer ********************************************************
        // Set duration of sensor measurment
	    WDTCTL = WDTPW+WDTTMSEL+WDTCNTCL+ group->measGateSource + group->accumulationCycles;
	    TA1CTL |= TACLR;                     // Clear Timer_A TAR
        if(group->measGateSource == GATE_WDTA_ACLK)
        {
            __bis_SR_register(LPM3_bits+GIE);   // Wait for WDT interrupt
        }
        else
        {
		    __bis_SR_register(LPM0_bits+GIE);   // Wait for WDT interrupt
        }
	    TA1CCTL1 ^= CCIS0;                   // Create SW capture of CCR1
	    counts[i] = TA1CCR1;                 // Save result
	    WDTCTL = WDTPW + WDTHOLD;           // Stop watchdog timer
	    CBCTL3 &= ~((group->arrayPtr[i])->inputBits);
	}
    // End Sequence
    //** Context Restore
    //  WDTA: IE1, WDCTL
    //  TIMERA0: TACTL, TACCTL1
    //  COMPB: CBCTL0, CBCTL1, CBCTL2, CBCTL3
    //  Ports: CboutDIR, CboutSel 
    __bis_SR_register(contextSaveSR);   
    if(!(contextSaveSR & GIE))
    {
      __bic_SR_register(GIE);   // Wait for WDT interrupt        
    }
    SFRIE1 = contextSaveSFRIE1;
    WDTCTL = contextSaveWDTCTL;
    TA1CTL = contextSaveTA1CTL;
    TA1CCTL1 = contextSaveTA1CCTL1;
    TA1CCR1 = contextSaveTA1CCR1;
    CBCTL0 = contextSaveCBCTL0;
    CBCTL1 = contextSaveCBCTL1;
    CBCTL2 = contextSaveCBCTL2;
    CBCTL3 = contextSaveCBCTL3;
    *(group->cboutTAxDirRegister) = contextSaveCboutDir;
    *(group->cboutTAxSelRegister) = contextSaveCboutSel;  
}
#endif

#ifdef fRO_COMPB_TA0_SW
/***************************************************************************//**
 * @brief   fRO method capactiance measurement using CompB, TimerA0 
 *
 *          Schematic Description of CompB forming relaxation oscillator and
 *          coupling (connection) between the relaxation oscillator and TimerA0.
 *          <- Output
 *          -> Input
 *          R  Resistor (typically 100Kohms)
 * 
 *          element---R----<-CBOUT                               
 * 
 *          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
 ******************************************************************************/
void TI_CTS_fRO_COMPB_TA0_SW_HAL(const struct Sensor *group, uint16_t *counts)
{ 
    uint8_t i;
    uint16_t j;
    
//** Context Save
//  TIMERA0: TA0CTL, TA0CCR1
//  COMPB: CBCTL0, CBCTL1, CBCTL2, CBCTL3
//  Ports: CboutDIR, CboutSel 

    uint16_t contextSaveTA0CTL,contextSaveTA0CCR0;
    uint16_t contextSaveCBCTL0,contextSaveCBCTL1;
    uint16_t contextSaveCBCTL2,contextSaveCBCTL3;
    uint8_t contextSaveCboutDir,contextSaveCboutSel;  

    contextSaveTA0CTL = TA0CTL;
    contextSaveTA0CCR0 = TA0CCR0;
    
    contextSaveCBCTL0 = CBCTL0;
    contextSaveCBCTL1 = CBCTL1;
    contextSaveCBCTL2 = CBCTL2;
    contextSaveCBCTL3 = CBCTL3;
    contextSaveCboutDir = *(group->cboutTAxDirRegister);
    contextSaveCboutSel = *(group->cboutTAxSelRegister);  
    //** Setup Measurement timer***************************************************
    // connect CBOUT with TA0
    *(group->cboutTAxDirRegister) |= (group->cboutTAxBits);
    *(group->cboutTAxSelRegister) |= (group->cboutTAxBits);
    CBCTL2 = CBREF14+CBREF13 + CBREF02;
    // Configure Timer TA0
    TA0CCR0 =(group->accumulationCycles);

    // Turn on Comparator
    CBCTL1 = CBON + CBF;                       // Turn on comparator with filter
                                               // Vcc to resistor ladder
    CBCTL3 |= (group->cbpdBits);               // set CPD bits to disable 
                                               // I/O buffer
    CBCTL2 |= CBRS_1;                          // Turn on reference
    for (i = 0; i<(group->numElements); i++)
    {
        j=0;
        CBCTL0 = CBIMEN + (group->arrayPtr[i])->inputBits;                                         
        //**  Setup Gate Timer ********************************************************
        // Set duration of sensor measurment
        TA0CTL = TASSEL_0+TACLR+MC_1;             // TACLK
        TA0CTL &= ~TAIFG;             // TACLK        
        while(!(TA0CTL & TAIFG))
        {
            j++;
        } // end accumulation
        counts[i] = j;   
    }
    // End Sequence
    //** Context Restore
    //  TIMERA0: TACTL, TACCTL1
    //  COMPB: CBCTL0, CBCTL1, CBCTL2, CBCTL3
    //  Ports: CboutDIR, CboutSel 
    TA0CTL = contextSaveTA0CTL;
    TA0CCR0 = contextSaveTA0CCR0;
    CBCTL0 = contextSaveCBCTL0;
    CBCTL1 = contextSaveCBCTL1;
    CBCTL2 = contextSaveCBCTL2;
    CBCTL3 = contextSaveCBCTL3;
    *(group->cboutTAxDirRegister) = contextSaveCboutDir;
    *(group->cboutTAxSelRegister) = contextSaveCboutSel;  
}
#endif

#ifdef fRO_COMPB_TA1_SW
/***************************************************************************//**
 * @brief   fRO method capactiance measurement using CompB, TimerA1 
 *
 *          Schematic Description of CompB forming relaxation oscillator and
 *          coupling (connection) between the relaxation oscillator and TimerA1.
 *          <- Output
 *          -> Input
 *          R  Resistor (typically 100Kohms)
 * 
 *          element---R----<-CBOUT                               
 * 
 *          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
 ******************************************************************************/
void TI_CTS_fRO_COMPB_TA1_SW_HAL(const struct Sensor *group,uint16_t *counts)
{ 
    uint8_t i;
    uint16_t j;
//** Context Save
//  TIMERA0: TA1CTL, TA1CCTL1
//  COMPB: CBCTL0, CBCTL1, CBCTL2, CBCTL3
//  Ports: CboutDIR, CboutSel 

    uint16_t contextSaveTA1CTL,contextSaveTA1CCR0;
    uint16_t contextSaveCBCTL0,contextSaveCBCTL1;
    uint16_t contextSaveCBCTL2,contextSaveCBCTL3;
    uint8_t contextSaveCboutDir,contextSaveCboutSel;  

    contextSaveTA1CTL = TA1CTL;
    contextSaveTA1CCR0 = TA1CCR0;
    
    contextSaveCBCTL0 = CBCTL0;
    contextSaveCBCTL1 = CBCTL1;
    contextSaveCBCTL2 = CBCTL2;
    contextSaveCBCTL3 = CBCTL3;
    contextSaveCboutDir = *(group->cboutTAxDirRegister);
    contextSaveCboutSel = *(group->cboutTAxSelRegister);  
    //** Setup Measurement timer***************************************************
    // connect CBOUT with TA1
    *(group->cboutTAxDirRegister) |= (group->cboutTAxBits);
    *(group->cboutTAxSelRegister) |= (group->cboutTAxBits);
    CBCTL2 = CBREF14+CBREF13 + CBREF02;
    // Configure Timer TA1
    TA1CCR0 =(group->accumulationCycles);

    // Turn on Comparator
    CBCTL1 = CBON + CBF;                       // Turn on comparator with filter
                                               // Vcc to resistor ladder
    CBCTL3 |= (group->cbpdBits);               // set CPD bits to disable 
                                               // I/O buffer
    CBCTL2 |= CBRS_1;                          // Turn on reference
    for (i = 0; i<(group->numElements); i++)
    {
        j=0;
        CBCTL0 = CBIMEN + (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(!(TA1CTL & TAIFG))
        {
            j++;
        } // end accumulation
        counts[i] = j;   
        //P1SEL &=~BIT4; 
    }
    // End Sequence
    //** Context Restore
    //  TIMERA0: TACTL, TACCTL1
    //  COMPB: CBCTL0, CBCTL1, CBCTL2, CBCTL3
    //  Ports: CboutDIR, CboutSel 
    TA1CTL = contextSaveTA1CTL;
    TA1CCR0 = contextSaveTA1CCR0;
    CBCTL0 = contextSaveCBCTL0;
    CBCTL1 = contextSaveCBCTL1;
    CBCTL2 = contextSaveCBCTL2;
    CBCTL3 = contextSaveCBCTL3;
    *(group->cboutTAxDirRegister) = contextSaveCboutDir;
    *(group->cboutTAxSelRegister) = contextSaveCboutSel;  
}
#endif

#ifdef WDT_GATE
// Watchdog Timer interrupt service routine
#pragma vector=WDT_VECTOR
__interrupt void watchdog_timer(void)
{
    __bic_SR_register_on_exit(LPM3_bits);           // Exit LPM3 on reti
}
#endif

/***************************************************************************//**
 * @}
 ******************************************************************************/

⌨️ 快捷键说明

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