Practical MSP430: TB captures the pulse width of PWM wave

Publisher:数据探险家Latest update time:2014-12-09 Source: laoguKeywords:msp430 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Capturing pulse width with TB

If you want to use TBCCRO to capture the low-level width of the pulse, the idea is: if tbccr0 captures a falling edge interrupt, then write down the value of tbccro and change it to a rising edge trigger; if it captures a rising edge interrupt, then write down the value of tbccro and change it to a falling edge trigger.

Hardware: MCU: MSP430F149
      Crystal: 32K, 8M
Input signal: 10ms low level, 7.5ms high level received via wireless,
Input port: P4.0 (TB0)
Requirement: Capture the pulse width of the low level

Software:
1. Initial idea: Use timer TBCCR0 as a capture module to capture external input signals: first set it to capture on the falling edge. If it is captured, immediately change it to capture on the rising edge, and immediately clear TBR to start counting; if it does not reach the rising edge, write down the data of BCCR0, which is the low-level width of the pulse.
2. Use TI's C language routine to slightly modify the program to run.
3. Problem: The program can capture the rising and falling edges, and the captured width is always large and small, with no rules.
The program has been changed over and over again without any progress, and my head is starting to get bigger~~~~
4. My senior brother came to see why the crystal oscillator was not turned on? I said that I didn't use the 8M one, so I didn't turn on the crystal oscillator specifically~ But since it's mentioned, why not just try another crystal oscillator, so I added a section of the program and changed TB to use MCLK (8M):
   void InitSys()
{ unsigned int iq0;

 //Use XT2 oscillator
   BCSCTL1&=~XT2OFF; //Turn on XT2 oscillator
   do
   {
   IFG1 &= ~OFIFG; //Clear oscillator failure flag
   for (iq0 = 0xFF; iq0 > 0; iq0--); //Delay, wait for XT2 to start oscillating
   }
  while ((IFG1 & OFIFG) != 0); //Judge whether XT2 starts oscillating
  BCSCTL2 =SELM_2+SELS; //Select MCLK=SMCLK for XT2
}
Something strange happened. The program was stuck in the delay program statement here. What happened? Could it be that the crystal oscillator cannot be turned on? Suddenly I thought of checking the hardware and found that a pin of the 8M crystal oscillator was loose#◎¥※@$……
After soldering the 8M crystal oscillator, the program can continue to run.
5. Another problem was found: although the program can run normally, the data collected by width is no longer fluctuating, and it starts to change regularly around 14500. However, after calculation, 14500*(1/8000000)=1.8125ms is inconsistent with the input signal pulse width. The input end is indeed 10ms when measured with an oscilloscope? ? ? ~~
6. Suddenly I thought that if the data of 10ms is collected, it should be 10ms/(1/8000000)=80000, which has long exceeded the value of TBR. Then after TBR overflows, it will restart from 0, and the displayed data should be exactly 65500+14500=80000! ! In other words, the data I got was correct, but I didn't consider the situation of TBR overflow!
7. Now that the problem has been found, it is easy to solve~Doesn't TBCTL of TB have clock division function? After setting the frequency division to 1/8, the clock is 1M, so the pulse width of 10ms should be 10ms/(1000000)=10000!
After the program is modified and run, it turns out that the source program of Amitabha
is as follows:

#include     
 unsigned int width[10]={0,0,0,0,0,0,0,0,0,0};
 unsigned int i=0;

 void main()
{
  WDTCTL=WDTPW+WDTHOLD; //Turn off the watchdog

  P4SEL = BIT0; //P4.0 is used as the input terminal of the capture module to input square wave

   //-------Open crystal oscillator XT2---------

   BCSCTL1&=~XT2OFF; //Turn on XT2 oscillator
   do
   {
   IFG1 &= ~OFIFG; //Clear oscillator failure flag
   for (i=256;i>0;i--); //Delay, wait for XT2 to start oscillating
   }
  while ((IFG1 & OFIFG) != 0); //Judge whether XT2 is oscillating

  BCSCTL2 =SELM_2+SELS; //Select MCLK=SMCLK for XT2

  //-----------------------------

  TBCCTL0&=~(CCIS1+CCIS0); // Capture source is P4.0, i.e. CCI0A (also CCI0B)
  TBCCTL0 =CM_2+SCS+CAP; // Falling edge capture, synchronous capture, working in capture mode
  TBCCTL0 =CCIE; // Allow capture compare module to make interrupt request
  TBCTL =ID_3;
  TBCTL =TBSSEL_2; // Select clock MCLK
  TBCTL =TBCLR; // Timer clears,
  // Timer starts counting (continuous counting mode 0~0xFFFF)
  TBCTL =MC_2;

  _EINT();

 while(1);
}

//―――――Timer TB's CCR0 interrupt: used to detect pulse rising and falling edges――――
#pragma vector=TIMERB0_VECTOR       
__interrupt void TimerB0(void)
{
   if(TBCCTL0&CM1) //Capture falling edge
     {
       TBCTL =TBCLR;
       TBCCTL0=(TBCCTL0&(~CM1)) CM0; //Change to rising edge capture: CM1 is set to zero, CM0 is set to one

     }
   else if(TBCCTL0&CM0) //Capture rising edge
     {
       width[i++]=TBCCR0; //Record end time
       TBCCTL0=(TBCCTL0&(~CM0)) CM1; //Change to falling edge capture: CM0 is set to zero, CM1 is set to one
       if(i==10) i=0;
              
     }

}

Lessons:
1. The modular design of the program is very important. Every time you write a program, it is best to follow the following rules:
turn off the watchdog; WDTCTL = WDTPW + WDTHOLD;
turn on the crystal oscillator: set ACLK = XT1 (32k), MCLK = SMCLK = XT2 (8M); and if you can use 8M, it is better to use 8M, which is more accurate.
Crystal oscillator detection method: XT2 can be realized through the scan flag in the program. Or set P1.4 (SMCLK), P2.0 (ACLK), and then use an oscilloscope to check
the main program: use the template you wrote.
2. If you are stuck on a problem, keep refining it until you touch its essence. It depends on how much you can refine the problem!
3. Any number or information has its implicit essential information, which can directly or indirectly reflect its essence. It depends on whether you can grasp this number and think of its reflection of the essence.

Keywords:msp430 Reference address:Practical MSP430: TB captures the pulse width of PWM wave

Previous article:Digital Temperature Meter Chip HT7500 and Its Application
Next article:Research and Design of UART between TMS320VC5402 and PC

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号