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.
Previous article:Digital Temperature Meter Chip HT7500 and Its Application
Next article:Research and Design of UART between TMS320VC5402 and PC
- Popular Resources
- Popular amplifiers
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- RAQ #223: How to measure and determine soft-start timing without a soft-start equation?
- GigaDevice's full range of automotive-grade SPI NOR Flash GD25/55 wins ISO 26262 ASIL D functional safety certification
- GigaDevice's full range of automotive-grade SPI NOR Flash GD25/55 wins ISO 26262 ASIL D functional safety certification
- New IsoVu™ Isolated Current Probes: Bringing a New Dimension to Current Measurements
- New IsoVu™ Isolated Current Probes: Bringing a New Dimension to Current Measurements
- Infineon Technologies Launches ModusToolbox™ Motor Kit to Simplify Motor Control Development
- Infineon Technologies Launches ModusToolbox™ Motor Kit to Simplify Motor Control Development
- STMicroelectronics IO-Link Actuator Board Brings Turnkey Reference Design to Industrial Monitoring and Equipment Manufacturers
- Melexis uses coreless technology to reduce the size of current sensing devices
- Melexis uses coreless technology to reduce the size of current sensing devices
- Passive Keyless Entry System Design Using PIC16F639 - Chinese Version
- [RVB2601 Creative Application Development] I2C reads ultrasonic pressure sensor data
- Please recommend some good university textbooks on STM32. Thank you.
- LED flashing program, can't enter interrupt
- How to output full load of power module
- Huacheng Yingmo Electric video PPT courseware and video tutorials are arranged for easy learning and printing
- Qinheng MCU CH55X — USB application example source code sharing
- AC-DC adapters and USB PD chargers will be 50% smaller
- Questions about CC8530
- Bluetooth Mesh Design Seminar