================================================== ================================================== =========================
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
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); // Determine whether XT2 is oscillating
BCSCTL2 = SELM_2 + SELS; //Select MCLK=SMCLK for XT2
}
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.
================================================== ==================================
Previous article:How to make Keil MDK v5 support ARM7/9 devices
Next article:Notes on C language programming for MSP430
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- [RVB2601 Creative Application Development] Simulating UART 3 to implement FIFO reception
- Please God save the child
- [AT-START-F403A Evaluation] Part 5 - FreeRTOS system based on IAR environment security library (sLib) function evaluation
- Please tell me how to save the collected data under embedded Linux
- Are you using Python? Be careful of being complained about infringement! [MicroPython Open Source Mutual Aid Alliance Established]
- Modbus RTU master-slave protocol made by dsp
- After 8 days, I finally added a mechanical hard drive to my laptop
- Evaluation Weekly Report 20220124: Yatli AT32F425 and Qinheng CH582 apply for online launch ~ National Technology and other evaluation reports updated
- Shouldn't we buy chips on Taobao?
- [ESP32-S2-Kaluga-1 Review] 3. Getting started with Ubuntu and updating under Ubuntu