MSP430 capture device is simple and practical
[Copy link]
MSP430 timer A capture pulse example [debugging passed, very useful]
Run the code Copy code
/********************************************************************
//Function: Use the capture of timer A to measure the pulse width of the pulse signal
//
//
// MSP430F449
// -----------------
// /|\| XIN|-
// | | | 32kHz
// --|RST XOUT|-
// | |
// | P1.5/ACLK|---+
// | | |
// | P2.0/TA2|<--+
// | |
// | |
//Description: ACLK needs to be divided by 8 (4K) and used as the external pulse to be captured;
//MCLK=SMCLK=8M;
*****************************************************************/
#include <msp430x44x.h>
int pwm_start,pwm_end,pwm_wide=0;
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
P1DIR = 0x20; // P1.5 output
P1SEL = 0x20; // P1.5 output ACLK
P2SEL|=BIT0; //P2.0 CCI2A
SCFI0 |= FN_4;
SCFQCTL = 121; // (121+1) ×32768 *2= 7.99Mhz
FLL_CTL0=DCOPLUS+OSCCAP1; //MCLK=SMCLK=8M
FLL_CTL1 |= FLL_DIV_8; //ACLK needs to be divided by 8, ACLK=4K
TACCTL2 =CAP+CM_3+CCIS_0+SCS+CCIE; //Capture mode, both rising and falling are captured, select CCI2A, synchronous, disconnect during capture
//Capture input select: 0 - CCI2A
TACTL = TASSEL_2 + MC_2 ; //SMCLK=8M,L continuous counting mode_EINT
(); //Enable total interrupt
LPM0;
_NOP();
pwm_wide=pwm_end-pwm_start;
while(1);
}
// Timer_A3 Interrupt Vector (TAIV) handler
#pragma vector=TIMERA1_VECTOR
__interrupt void Timer_A(void)
{
switch(TAIV)
{case 2:break;
case 4:
if(TACCTL2 & CCI)
pwm_start=CCR2;
else
pwm _end=CCR2;
//pwm_wide=pwm_end-pwm_start;
break;
case 10:
break;
}
LPM0_EXIT;
}
Verification method:
In the debugging environment, observe pwm_end and pwm_start in the watch window, and then subtract them!
I calculated it. In the given example, the theoretical value pwm_end-pwm_start=976.
The result of observation is 978, which is still a little bit wrong. It may be due to the value of the crystal oscillator, and the rounding in the calculation is a little bit wrong.
|