Author: Made by Xura
Date: 2008.8.28Program
description: Use Timer_A to capture pulse width
Use the MSP430 single-chip timer A and the capture/compare function module to measure the pulse width
The program uses the CCI1A port of timer A (P1.2 pin of MSP430F14X) to capture
the pulse level jump of the external input , start, end, two variables to calculate the pulse width
*******************************************************/
#include "msp430x14x.h"
#include "lcd12864.h"
uint start,end;
uint width; //==Used to store pulse width==
uint period; //==Used to store period==
uint frequency; //==Used to store frequency==
uint fy[7]; //==Used to store frequency display data==
uint pd[7]; //==Used to store period display data==
uint wh[6]; //==Used to store pulse width display data==
const unsigned char zhouqi[]={"Period: (us) "};
const unsigned char us[]={"us "};
const unsigned char pinlv[]={"Frequency: (Hz) "};
const unsigned char hz[]={"HZ "};
void process(void); //==Function declaration==
void delay(); //==Delay function==
void InitSys(); //==Initialize clock==
/**********************************************************************
Main function
******************************************************************/
int main( void )
{
WDTCTL = WDTPW + WDTHOLD; //==Turn off the dog== InitSys
(); //==Initialize clock, SMCLK, MCLK are both 8M==
P1DIR&=~BIT2;
P1SEL = BIT2; //==Set P1.2 port as function module, i.e.: capture source==
TACTL = TASSEL_2+ID_3+TACLR+TAIE+MC1; //==Select SMCLK, 8 division for timer A clock signal, and set timer A counting mode to continuous increment mode==
CCTL1 = CM_1+SCS+CAP+CCIE; //==Input rising edge capture, CCI0A is capture signal source==
_EINT(); //==Enable global interrupt==
Ini_Lcd(); //==Initialize LCD==
Clear_GDRAM(); //==Clear screen==
Disp_HZ(0x80,zhouqi,8);
Disp_HZ(0x88,pinlv,8);
while(1)
{
process();
Write_Cmd(0x90);//==Write address==
Write_Data(0x30+pd[6]);
Write_Data(0x30+pd[5]);
Write_Data(0x30+pd[4] );
Write_Data(0x30+pd[3]);
Write_Data(0x30+pd[2]);
Write_Data(0x30+pd[1]);
Write_Data(0x30+pd[0]);
Write_Cmd(0x98);//= =Write address==
Write_Data(0x30+fy[6]);
Write_Data(0x30+fy[5]);
Write_Data(0x30+fy[4]);
Write_Data(0x30+fy[3]);
Write_Data(0x30+fy [2]);
Write_Data(0x30+fy[1]);
Write_Data(0x30+fy[0]);
delay();
}
}
/*******************************************************
Initialize clock
*******************************************************/
void InitSys()
{
unsigned int i;
//--- Use XT2 oscillator---
BCSCTL1&=~XT2OFF; //==Turn on XT2 oscillator==
do
{
IFG1 &= ~OFIFG; //==Clear oscillator failure flag==
for (i = 0xFF; i > 0; i--); //==Delay, wait for XT2 to start oscillating==
}
while ((IFG1 & OFIFG) != 0); //==Judge whether XT2 starts oscillating==
BCSCTL2 =SELM_2+SELS; //==Select MCLK, SMCLK as XT2,8M==
}
/***********************************************************
Delay function
*******************************************************/
void delay()
{
unsigned int i;
unsigned int j=10;
for(i=10;i>0;i--)
{
while(j--);
}
}
/************************************************************************
Data processing
****************************************************************/
void process(void)
{
while(end
width = end-start; //==Calculation of actual pulse width==
period = 2* width;
frequency=1000000/period;
pd[6]=period/1000000;
pd[5]=(period-1000000*pd[6])/100000;
pd[4]=(period-1000000*pd[6]-100000*pd[5])/10000;
pd[3]=(period-1000000*pd[6]-100000*pd[5]-10000*pd[4])/1000;
pd[2]=(period-1000000*pd[6]-100000*pd[5]-10000*pd[4]-1000*pd[3])/100;
pd[ 1]=(period-1000000*pd[6]-100000*pd[5]-10000*pd[4]-1000*pd[3]-100*pd[2])/10;
pd[0]=period%10;
fy[6]=frequency/1000000;
fy[5]=(frequency-1000000*fy[6])/100000;
fy[4]=(frequency-1000000*fy[6]-100000*fy[5])/10000;
fy[3]=(frequency-1000000*fy[6]-100000*fy[5]-10000*fy[4])/ 1000;
fy[2]=(frequency-1000000*fy[6]-100000*fy[5]-10000*fy[4]-1000*fy[3])/100;
fy[1]=(frequency-1000000*fy[6]-100000*fy[5]-10000*fy[4]-1000*fy[3]-100*fy[2])/10;
fy[0]=frequency%10;
}
/***********************************************************************
Interrupt processing function
***************************************************************/
#pragma vector=TIMERA1_VECTOR //==Timer A interrupt processing==
__interrupt void timer_a(void)
{
switch(TAIV) //==Vector query==
{ case 2: //==Capture interrupt==
if(CCTL1&CM0) //==Capture rising edge==
{
CCTL1=(CCTL1&(~CM0))|CM1; //==Change to falling edge trigger==
start=TAR; //==record initial time==
}
else if (CCTL1&CM1) //==capture falling edge==
{
CCTL1=(CCTL1&(~CM1))|CM0; //==change setting to rising edge trigger==
end=TAR; //==calculate pulse width using start, end, overflow==
}
break;
default:
break;
}
}
Previous article:MSP430G2553 test program (long and short key recognition)
Next article:MSP430G2553 test program (serial port program, USCI mode)
- 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
- When the TV is in power standby mode, a large current appears. After testing the TV, I found that the electrical abnormality is original to the TV or is it leakage from the SS210 next to it?
- Today at 10:00 AM, live broadcast with prizes: Keysight Technologies 100G/400G Optical Communication Test Solutions
- Invite you to participate: The 17th Medtec China Exhibition and International Medical Device Design and Manufacturing Technology Exhibition
- Siemens s7-200smart modbus-rtu communication example programming detailed guide
- STM32G474 driver for HDC1080
- Import problem in Ubuntu
- Share the discussion about BQ40Z50-R2 external equalization circuit, are there any better suggestions?
- Please help me with the circuit of low frequency sine wave
- Switched capacitor filter related
- FPGA_100 Days Tour_Phase-Locked Loop