msp430 single chip microcomputer frequency measurement

Publisher:NanoScribeLatest update time:2016-08-15 Source: eefocusKeywords:msp430 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
/****************************************************** ******

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   //while(endstart
    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;
  } 
}

Keywords:msp430 Reference address:msp430 single chip microcomputer frequency measurement

Previous article:MSP430G2553 test program (long and short key recognition)
Next article:MSP430G2553 test program (serial port program, USCI mode)

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号