Share the problem and solution of using TB to capture pulse width of MSP430

Publisher:Delightful789Latest update time:2017-02-19 Source: eefocusKeywords:MSP430 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

================================================== ================================================== =========================

Hardware  : Microcontroller: MSP430F149 
Crystal: 32K, 8M 
Input signal: 10ms low level and 7.5ms high level received through wireless, 
Input port: P4.0 (TB0) 
Requirements: Capture the pulse width of the low level 
=== ... Initial idea: Use timer TBCCR0 as a capture module to capture external input signals: first set it to capture on the falling edge. If 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, immediately change it to the falling edge, and write down the data of TBCCR0, 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 you mention it, why not just try another crystal oscillator, so I added a section of the program and changed TB to use MCLK (8M):






[cpp]  view plain  copy

  1. void InitSys()   

  2. {   

  3.     unsigned int iq0;   

  4.   

  5.     //Use XT2 oscillator   

  6.     BCSCTL1 &= ~XT2OFF; //Turn on XT2 oscillator   

  7.     do {   

  8.         IFG1 &= ~OFIFG; // Clear oscillator failure flag   

  9.         for (iq0 = 0xFF; iq0 > 0; iq0--); // Delay, wait for XT2 to start oscillating   

  10.     } while ((IFG1 & OFIFG) != 0); // Determine whether XT2 is oscillating   

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

  12. }  


A strange thing happened. The program was stuck in the delay program statement here. What happened? Could it be that the crystal oscillator could not be turned on? Suddenly I thought of checking the hardware and found that one pin of the 8M crystal oscillator was loose#◎¥※@$……
After soldering the 8M crystal oscillator, the program could continue to run. 
5. Another problem was found: although the program could run normally, the data collected by width no longer fluctuated, and began to change regularly around 14500. However, after calculation, 14500*(1/8000000)=1.8125ms, which was inconsistent with the input signal pulse width. The input end measured with an oscilloscope was indeed 10ms? ? ?
6. Suddenly I thought that if the 10ms data was 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 TBR overflow!
7. Now that the problem has been discovered, it's easy to solve. Doesn't the TBCTL of TB have a clock division function? After setting the 1/8 division, the clock is 1M, so the 10ms pulse width should be 10ms/(1000000)=10000! After the
program was modified and run, it worked as expected. Amituofo 

The source program is as follows: 

#include   

  

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

unsigned int i = 0;   

  

void main(void)   

{   

    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); // Determine whether XT2 is oscillating   

  

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

  

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

  

    TBCCTL0 &= ~(CCIS1 + CCIS0); // Capture source is P4.0, which is CCI0A (also CCI0B)   

    TBCCTL0 |= CM_2 + SCS + CAP; // Falling edge capture, synchronous capture, working in capture mode   

    TBCCTL0 |= CCIE; // Allow the capture compare module to make an interrupt request   

    TBCTL |= ID_3;   

    TBCTL |= TBSSEL_2; // Select clock MCLK   

    TBCTL |= TBCLR; // timer clear,   

    //Timer starts counting (continuous counting mode 0~0xFFFF)   

    TBCTL |= MC_2;   

  

    _EINT();   

  

    while(1);   

}   

  

//―――――Interrupt of CCR0 of timer TB: used to detect the rising and falling edges of pulses――――   

#pragma vector = TIMERB0_VECTOR   

__interrupt void TimerB0(void)   

{   

    if(TBCCTL0&CM1) { // Capture the 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 the rising edge   

        width[i++] = TBCCR0; // Record the 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: 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 check with an oscilloscope 

Main program: Use the template you wrote yourself.

2. If you get stuck on a problem, keep refining it until you get to the essence of it. It all 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 about its reflection of the essence.

================================================== ==================================



Keywords:MSP430 Reference address:Share the problem and solution of using TB to capture pulse width of MSP430

Previous article:How to make Keil MDK v5 support ARM7/9 devices
Next article:Notes on C language programming for MSP430

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号